hibernate - Is "flush" of session must called in this case? -
i defined "booking" entity "part" entity ,their relationship 1 many.now in case had such operation
step 1:retrieve 1 booking fk,assume under booking,it has 3 part,then show in ui
step 2:in ui,i add 1 new part,then mark original 3 saved part deleted part,finally save booking again.
now in back-end,i handle saving logic following
first,my booking action's save function following
public string save() throws exception{ booking = presave(booking); booking = dosave(booking); booking = postsave(booking); return success; }
the dosave function following
@transactional(propagation=propagation.required,isolation=isolation.default) private booking dosave(booking booking) throws exception{ logger.debug("save booking start,will delete deleted/update/save record in db"); //step 1:get booking dao spring context bookingdao dao = (bookingdao) daofactory.getdao("booking"); //step 2:get deleted object booking tree,as usual,the deleted object marked in ui list tobedeleted = bookingutil.handledeletedobj(booking); logger.debug("the deleted object ["+tobedeleted+"]"); //step 3:if deleted object not empty,invoke booking dao's delete function delete them if(tobedeleted!=null && tobedeleted.size()>0){ dao.delete(tobedeleted); } //step 4:invoke booking dao's save function save/update obj booking = (booking) dao.save(booking); return booking; }
delete function in dao
@override @transactional(propagation=propagation.required,isolation=isolation.default) public object delete(object object) { // todo auto-generated method stub logger.debug("delete deleted object ["+object+"]"); if(object == null){ logger.error("there no deleted object found"); return null; } session session = sf.opensession(); if(object instanceof list){ list list = (list) object; for(object obj:list){ session.delete(obj); } //session.flush(); return list; } return object; }
save function in booking dao
@override
@transactional(propagation=propagation.required,isolation=isolation.default)
public object save(object object) { logger.debug("save() start,save booking["+object+"] db"); booking booking = (booking)object; string bookingno = null; //step 1:check if booking no empty,if empty,generate booking no customer first if(booking.getbookingno() == null || booking.getbookingno().trim().isempty()){ logger.debug("save(),find booking no empty,will generate booking no first"); bookingno = (string) generatebookingno(booking.getcustomer()); //set generated booking no booking booking.setbookingno(bookingno); } //step 2:set part's fk bookingno list <part>parts = booking.getparts(); if(parts!=null){ for(part part:parts){ if(part.getbookingno() == null || part.getbookingno().isempty()){ part.setbookingno(booking.getbookingno()); } } } //step 3:set todoitem's fk list<todoitem>todoitems = booking.gettodoitems(); if(todoitems!=null){ for(todoitem todoitem:todoitems){ if(todoitem.getbookingno() == null || todoitem.getbookingno().isempty()){ todoitem.setbookingno(booking.getbookingno()); } } } //step 4:save/update booking session session = sf.getcurrentsession(); session.saveorupdate(booking); session.flush(); return booking; }
as see,my logic quite simple:delete stuff , save stuff,beside point,they should in 1 transaction,but when test,i found after call save action's save(),the parts didn't been deleted @ all,the log following:
17:37:36,143 debug bookingdao:122 - delete deleted object [[com.chailie.booking.model.booking.part@712e3058, com.chailie.booking.model.booking.part@f681b75, com.chailie.booking.model.booking.part@7be2a639]] 17:37:36,144 debug sessionimpl:220 - opened session @ timestamp: 13676602561 17:37:36,147 debug defaultdeleteeventlistener:65 - entity not persistent in delete processing 17:37:36,149 debug versionvalue:44 - version unsaved-value strategy undefined 17:37:36,150 debug identifiervalue:104 - id unsaved-value: null 17:37:36,153 debug defaultdeleteeventlistener:180 - deleting [com.chailie.booking.model.booking.part#2] 17:37:36,154 debug sessionimpl:1308 - setting cache mode to: 17:37:36,155 debug sessionimpl:1308 - setting cache mode to: normal 17:37:36,160 debug sessionimpl:1308 - setting cache mode to: 17:37:36,162 debug sessionimpl:1308 - setting cache mode to: normal 17:37:36,162 debug defaultdeleteeventlistener:65 - entity not persistent in delete processing 17:37:36,163 debug versionvalue:44 - version unsaved-value strategy undefined 17:37:36,163 debug identifiervalue:104 - id unsaved-value: null 17:37:36,164 debug defaultdeleteeventlistener:180 - deleting [com.chailie.booking.model.booking.part#3] 17:37:36,165 debug sessionimpl:1308 - setting cache mode to: 17:37:36,165 debug sessionimpl:1308 - setting cache mode to: normal 17:37:36,166 debug sessionimpl:1308 - setting cache mode to: 17:37:36,166 debug sessionimpl:1308 - setting cache mode to: normal 17:37:36,167 debug defaultdeleteeventlistener:65 - entity not persistent in delete processing 17:37:36,168 debug versionvalue:44 - version unsaved-value strategy undefined 17:37:36,168 debug identifiervalue:104 - id unsaved-value: null 17:37:36,169 debug defaultdeleteeventlistener:180 - deleting [com.chailie.booking.model.booking.part#4] 17:37:36,170 debug sessionimpl:1308 - setting cache mode to: 17:37:36,171 debug sessionimpl:1308 - setting cache mode to: normal 17:37:36,171 debug sessionimpl:1308 - setting cache mode to: 17:37:36,172 debug sessionimpl:1308 - setting cache mode to: normal 17:37:36,173 debug jdbctransaction:103 - commit 17:37:36,174 debug sessionimpl:337 - automatically flushing session 17:37:36,174 debug jdbccontext:201 - before transaction completion 17:37:36,175 debug sessionimpl:393 - before transaction completion 17:37:36,176 debug jdbctransaction:193 - re-enabling autocommit 17:37:36,177 debug jdbctransaction:116 - committed jdbc connection 17:37:36,178 debug jdbccontext:215 - after transaction completion 17:37:36,178 debug connectionmanager:296 - transaction completed on session on_close connection release mode; sure close session release jdbc resources! 17:37:36,179 debug sessionimpl:422 - after transaction completion 17:37:36,179 debug sessionimpl:353 - automatically closing session 17:37:36,180 debug sessionimpl:273 - closing session 17:37:36,180 debug connectionmanager:374 - performing cleanup 17:37:36,181 debug connectionmanager:435 - releasing jdbc connection [ (open preparedstatements: 0, globally: 0) (open resultsets: 0, globally: 0)] 17:37:36,182 debug jdbccontext:215 - after transaction completion 17:37:36,182 debug connectionmanager:296 - transaction completed on session on_close connection release mode; sure close session release jdbc resources! 17:37:36,183 debug sessionimpl:422 - after transaction completion 17:37:36,184 debug sessionimpl:273 - closing session
but when add session.flush() in end of booking dao's delete(),it delete part in db successful.
so question is: when didn't have flush() in dao's delete function(),why part can't deleted?
ps:my session factory spring configuration following:
bean id="sessionfactory" class="org.springframework.orm.hibernate3.annotation.annotationsessionfactorybean"> <property name="datasource"> <ref bean="datasource1" /> </property> <property name="hibernateproperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.mysqldialect </prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.transaction.flush_before_completion">true</prop> <prop key="hibernate.transaction.auto_close_session">true</prop> <prop key="hibernate.connection.release_mode">auto</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="format_sql">true</prop> </props> </property> <property name="packagestoscan"> <list> <value>com.chailie.booking.model.*</value> </list> </property>
Comments
Post a Comment