繁体   English   中英

休眠设置布尔值

[英]Hibernate setting boolean value

我是JSF和Hibernate的新手,所以如果这个问题在这里很简单,我感到抱歉。

我正在使用MySQL数据库,并且我的users表中有一个布尔列。 Hibernate将hbm.xml中的值映射为

<property name="verified" type="boolean">
    <column name="verified" not-null="true"/>
</property>

当我在NetBeans中使用HQL查询窗口运行查询时

update Users set verified = false where email = 'mail@mail.com'

它完美地将布尔值设置为true和false。 但是在我的助手类中,我有这种方法来设置值

public void setIsVerified(Users user, Boolean isVerified){
    Transaction tx = null;

    try{
        tx = session.beginTransaction();
        Query q = session.createQuery("update Users set verified = :verified where email = :email");
        q.setParameter("verified", isVerified);
        q.setParameter("email", user.getEmail());
        q.executeUpdate();
    }catch(RuntimeException e){
        if(tx != null)
            tx.rollback();
        throw e;
    }
}

但是它没有设置值。 我尝试将值手动设置为q.setParameter("verified", true); 它也没有用。 (电子邮件是唯一字段,我也尝试使用ID字段作为主键。)

有趣的是,当我使用JUnit运行此方法时,它不会产生任何错误消息或警告。 看来它可以完美运行,但不会更改数据库中的值。

任何提示都将不胜感激,因为我没有收到不知道在哪里搜索的消息。 我不知道背景发生了什么。 提前致谢..

由于您在方法的开头明确地调用beginTransaction,因此必须在完成工作单元之前调用tx.commit()。

使用休眠的优点之一是您可以声明性地进行事务管理http://docs.spring.io/spring/docs/current/spring-framework-reference/html/transaction.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM