Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Hibernate saveOrUpdate不更新_Java_Hibernate - Fatal编程技术网

Java Hibernate saveOrUpdate不更新

Java Hibernate saveOrUpdate不更新,java,hibernate,Java,Hibernate,我正在尝试使用Hibernate中的session.saveOrUpdate()方法更新表行 但是,它无法更新该行,并试图通过生成insert语句来保存该行。由于my DB中有几个不可为空的字段,此插入无法工作 我能够检索要保存在DAO层的对象的Id,因此我无法理解为什么它不更新DB表中的相应行 Bean类:(BaseEntityBean具有Id、CreatedBy等) DAO方法: public EmployeeMasterBean saveOrUpdateEmployee(EmployeeM

我正在尝试使用Hibernate中的session.saveOrUpdate()方法更新表行

但是,它无法更新该行,并试图通过生成insert语句来保存该行。由于my DB中有几个不可为空的字段,此插入无法工作

我能够检索要保存在DAO层的对象的Id,因此我无法理解为什么它不更新DB表中的相应行

Bean类:(BaseEntityBean具有Id、CreatedBy等)

DAO方法:

public EmployeeMasterBean saveOrUpdateEmployee(EmployeeMasterBean employeeMasterBean)  throws Exception{
    Session session = null;
    Transaction tx = null;

    try {
        session = HibernateUtil.getSessionFactory().openSession();
        tx = session.beginTransaction();

        session.saveOrUpdate(employeeMasterBean);

        tx.commit();
    } finally {
        session.close();
    }
    return employeeMasterBean;
}
引发的Eclipse调试器异常包括:

could not insert: [com.indven.gpil.hrd.entity.EmployeeMasterBean]
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'CreatedBy' cannot be null

正如错误消息所说,数据库有一个列
createdby
,该列不能为空


调用
saveOrUpdate()
时,有人已将此属性设置为
null
,因此无法进行更新。

我认为
CreatedBy
列在DB表中以
notnull
的形式存在,但您的bean没有映射此列,因此在执行
saveOrUpdate
时会发送
null
值,这将导致抛出上述异常


在bean中添加一个映射到
CreatedBy
,并使用一些默认值,然后让trigger etc更新默认值。或者,如果可以将数据库中的列更改为可为null,则bean没有设置rowVersion属性(用于乐观锁定),因此默认情况下为null。Hibernate因此将其解释为一条新记录,并一直保存它


每当我试图保存或更新任何记录时,我都将行版本存储在我的值对象和相应的Bean中,从而解决了这个问题

是否
employeeMasterBean.id
null?
employeeMasterBean
已通过设置创建?听起来您正在传递分离的实体;)它在Bean中为null,但在DB表中设置。也许您应该尝试
merge
,而不是
saveOrUpdate
could not insert: [com.indven.gpil.hrd.entity.EmployeeMasterBean]
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'CreatedBy' cannot be null