日期:2014-05-20 浏览次数:20948 次
public void delete(Object entity,LockMode lockMode)
public void delete(final Object entity, final LockMode lockMode) throws DataAccessException { executeWithNativeSession(new HibernateCallback<Object>() { public Object doInHibernate(Session session) throws HibernateException { checkWriteOperationAllowed(session); if (lockMode != null) { session.lock(entity, lockMode); } session.delete(entity); return null; } }); }
------解决方案--------------------
一般书籍中将对象的状态分为三中,但在《Hibernate in Action 2th》分为了四种
多出来的一种原文摘录如下:
Removed objects
You can delete an entity instance in several ways: For example, you can remove it
with an explicit operation of the persistence manager. It may also become available
for deletion if you remove all references to it, a feature available only in
Hibernate or in Java Persistence with a Hibernate extension setting (orphan deletion
for entities).
An object is in the removed state if it has been scheduled for deletion at the end
of a unit of work, but it’s still managed by the persistence context until the unit of
work completes. In other words, a removed object shouldn’t be reused because it
will be deleted from the database as soon as the unit of work completes. You
should also discard any references you may hold to it in the application (of
course, after you finish working with it—for example, after you’ve rendered the
removal-confirmation screen your users see).