我遇到了一个非常奇怪的情况。当我尝试在后面立即删除一组实体&时尝试添加另一组实体,这些实体可能也可能不再有相同的元素,我似乎正在获得以下例外。整个堆栈跟踪不是必需的,因为它不会说太多。解决JPA/Hibernate EntityNotFoundException
javax.persistence.EntityNotFoundException: deleted entity passed to persist [com.test.MyEntity#<null>]
为了让自己更清楚,让我给出更详细的解释。
这是我的实体。
@Entity
@Table(name = "MY_ENTITY")
@SequenceGenerator(name = "my_enty_seq", sequenceName = "MY_ENTITY_SEQ")
public class MyEntity {
private long id;
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "my_enty_seq")
@Column(name = "ID", unique = true, nullable = false, precision = 12, scale = 0)
public long getId() {
return this.id;
}
// Other methods & variables which are not relevant here.
}
这是玩实体的类。
public class MyEntityServiceImpl implements MyEntityService {
public void updateMyEntities(List<MyEntity> newEntities) {
// Delete the Old Items
List<MyEntity> oldEntities = myDAO.getAllEntities();
myDAO.delete(oldEntities); // myDAO extends GenericDaoJpaWithoutJpaTemplate, hence, the DELETE is available in it.
// Add the New Items
myDAO.store(newEntities); // This is where I get the mentioned Exception.
}
// Other non-relevant stuffs
}
从哪里获得的store()
方法所提到的例外的情况是有一些共同的MyEntity
对象,同时存在于oldEntities和newEntities名单。
我已经尝试了很多事情来完成这项工作,但没有任何东西可以帮助我解决问题。添加FetchType.EAGER
& CascaseType.ALL, CascaseType.PERSIST
也没有工作。 jpa EntityManager
似乎也没有commit()
方法,我可以在store()
方法之前的delete()
&之后使用。我试过与交换store()
(虽然店内也叫persist()
)!
注意: -需要删除旧列表并插入新列表,因为比较元素并只添加新元素是很繁琐的(列表可能有时非常庞大)。
一个提交只是在实际提交事务之前进行刷新。 – 2013-05-13 05:52:47