java 验证在休眠中成功执行创建/更新/删除

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12695268/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 09:51:49  来源:igfitidea点击:

verify create/update/delete successfully executed in hibernate

javahibernate

提问by Atul

In hibernate how to confirm/verify whether session.save(entity), session.delete(entity) and session.update(entity) operations have been executed successfully w/o making another call to db?

在休眠中如何确认/验证 session.save(entity)、session.delete(entity) 和 session.update(entity) 操作是否已成功执行而无需再次调用 db?

I mean if you delete an entity how do you confirm its deleted. can it be done w/o making another call to the database?

我的意思是,如果您删除一个实体,您如何确认其已删除。可以在不再次调用数据库的情况下完成吗?

回答by Vikdor

You should watch out for HibernateException, a sub-class of RuntimeExceptionif you want to catch failures of save/update/delete calls with entities on the session object. If the exception is not thrown, then it means the operation is successful.

您应该注意HibernateExceptionRuntimeException如果您想捕获会话对象上实体的 save/update/delete 调用失败的子类。如果没有抛出异常,则说明操作成功。

If you use JPA semantics, then PersistenceExceptionis the counter-part of HibernateExceptionthat you should watch out to handle such failure scenarios.

如果您使用 JPA 语义,那么您应该注意处理此类故障场景PersistenceException的反面HibernateException

回答by Subhrajyoti Majumder

session.delete,session.save and session.updatemethods throws unchecked HibernateExceptionon failure.

session.delete,session.save and session.update方法HibernateException在失败时抛出未经检查。

回答by Sumit Desai

Hibernate exception will be thrown if the specified operation fails. So, you should have a catch block for HibernateException. If control comes in this block, then you should return false to indicate that the operation is unsuccessful.

如果指定的操作失败,将抛出 Hibernate 异常。因此,您应该为 HibernateException 设置一个 catch 块。如果控制权在此块中,则应返回 false 以指示操作不成功。