我将“预订”实体定义为“部分”实体,他们的关系是一对多。现在我的情况下我有这样的操作
步骤1:通过FK检索一个预订,假设在此预订下,它有三个部分,然后我将在UI中显示
步骤2:在UI中,我将添加一个新零件,然后将原始三个保存零件标记为已删除零件,最后再次保存此预订。
现在在我的后端,我将按照以下
处理保存逻辑首先,我的预订操作的保存功能如下
public String save() throws Exception{
booking = preSave(booking);
booking = doSave(booking);
booking = postSave(booking);
return SUCCESS;
}
doSave功能如下
@Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.DEFAULT)
private Booking doSave(Booking booking) throws Exception{
logger.debug("save booking start,will delete to be deleted/update/save record in DB");
//Step 1:Get booking DAO from spring context
BookingDAO dao = (BookingDAO) DAOFactory.getDAO("Booking");
//Step 2:Get the to be deleted object from booking tree,as usual,the to be deleted object is marked in UI
List toBeDeleted = BookingUtil.handleDeletedObj(booking);
logger.debug("The to be deleted object is ["+toBeDeleted+"]");
//Step 3:If to be deleted object is not empty,invoke booking DAO's delete function to delete them
if(toBeDeleted!=null && toBeDeleted.size()>0){
dao.delete(toBeDeleted);
}
//Step 4:Invoke booking DAO's save function to save/update obj
booking = (Booking) dao.save(booking);
return booking;
}
删除DAO中的功能
@Override
@Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.DEFAULT)
public Object delete(Object object) {
// TODO Auto-generated method stub
logger.debug("delete to be deleted object ["+object+"]");
if(object == null){
logger.error("there is no to be 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;
}
在预订DAO中保存功能
@覆盖
@Transactional(传播= Propagation.REQUIRED,隔离= Isolation.DEFAULT)
public Object save(Object object) {
logger.debug("save() start,save booking["+object+"] to DB");
Booking booking = (Booking)object;
String bookingNo = null;
//Step 1:Check if booking no is empty,if empty,generate booking no by customer first
if(booking.getBookingNo() == null || booking.getBookingNo().trim().isEmpty()){
logger.debug("save(),find booking no is empty,will generate booking no first");
bookingNo = (String) generateBookingNo(booking.getCustomer());
//Set generated booking no to 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;
}
如你所见,我的逻辑非常简单:删除东西并保存东西,除此之外,它们应该在一个事务中,但是当我进行测试时,我发现在调用后保存动作的save(),部分没有'根本没有删除,日志如下:
17:37:36,143 DEBUG BookingDAO:122 - delete to be 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 at timestamp: 13676602561
17:37:36,147 DEBUG DefaultDeleteEventListener:65 - entity was 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: GET
17:37:36,155 DEBUG SessionImpl:1308 - setting cache mode to: NORMAL
17:37:36,160 DEBUG SessionImpl:1308 - setting cache mode to: GET
17:37:36,162 DEBUG SessionImpl:1308 - setting cache mode to: NORMAL
17:37:36,162 DEBUG DefaultDeleteEventListener:65 - entity was 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: GET
17:37:36,165 DEBUG SessionImpl:1308 - setting cache mode to: NORMAL
17:37:36,166 DEBUG SessionImpl:1308 - setting cache mode to: GET
17:37:36,166 DEBUG SessionImpl:1308 - setting cache mode to: NORMAL
17:37:36,167 DEBUG DefaultDeleteEventListener:65 - entity was 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: GET
17:37:36,171 DEBUG SessionImpl:1308 - setting cache mode to: NORMAL
17:37:36,171 DEBUG SessionImpl:1308 - setting cache mode to: GET
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 with on_close connection release mode; be sure to close the session to 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 with on_close connection release mode; be sure to close the session to release JDBC resources!
17:37:36,183 DEBUG SessionImpl:422 - after transaction completion
17:37:36,184 DEBUG SessionImpl:273 - closing session
但是当我在预订DAO的delete()时添加session.flush(),它可以删除数据库中的部分成功。
所以我的问题是:当我在DAO的删除函数()中没有flush()时,为什么不能删除该部分?
Ps:我的会话工厂弹簧配置如下:
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>