【问题标题】:Hibernate check if updating a record succeeded休眠检查更新记录是否成功
【发布时间】:2016-08-22 13:06:39
【问题描述】:

我正在使用 Hibernate 创建 Jersey Web 服务。可用的方法之一是更新数据库中的记录,我正在这样做:

private Boolean updateSingleRecord(Session session, Person updatedRecordData)
{
    try
    {
        short id = updatedRecordData.getPersonId();
        Person per = (Person) session.load(Person.class, id);
        per.setPersonEmail(updatedRecordData.getPersonEmail());
        per.setPersonMobile(per.getPersonMobile());
        transaction.commit(); //was defined earlier in the code
        return true;
    }
    catch(Exception e)
    {
        return false;
    }
}

这些字段的定义使得手机需要有一个唯一的值。如果我将对象更新为已经存在的值,我不会收到任何错误,但该值根本不会改变。如何在不比较初始值和新值的情况下检测提交后是否进行了更改?

【问题讨论】:

    标签: java hibernate jersey


    【解决方案1】:

    Hibernate 拦截器在创建、修改和更新等事件中充当触发器。 http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/events.html 所以检查实体是否被修改和持久化。

    您可以在 Spring 的 Hibernate 4 实现中创建一个新的会话范围拦截器

     Session session = s.withOptions().interceptor(new MyInterceptor().openSession();
    

    【讨论】:

    • 说明情况了吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-11
    • 1970-01-01
    • 2018-08-03
    • 2018-08-29
    相关资源
    最近更新 更多