JPA检查实体是否可以删除

JPA检查实体是否可以删除

问题描述:

如何检查JPA中的实体是否可以删除,并且不会引发数据完整性异常?我想到的唯一方法是逐个检查所有引用的实体,或尝试在事务中删除然后回滚,但还有其他更简单的方法吗?

How can I check if entity in JPA can be deleted and data integrity exception won't be thrown? Only way that came to my mind is to check all referenced entities one by one or try to delete in transaction and then rollback but is there some other simpler way?

如果通过可以删除您的意思是如果实体存在于持久化上下文中,您已经回答了您的问题:

If by "can be deleted" you mean "if entity exists in persistence context", you already answered your question:


尝试在事务中删除然后回滚

try to delete in transaction and then rollback

如果在事务期间发生错误,则回滚

If something went wrong during transaction then rollback