Hibernate的saveOrUpdate方法不会更新数据

6

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

然而,它无法更新该行并尝试通过生成插入语句来保存它。由于我的DB中有一些非空字段,这个插入操作失败了。

在DAO层,我能够检索到要保存的对象的ID,因此我不明白为什么它不能只是更新DB表中对应的行。

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

public class EmployeeMasterBean extends BaseEntityBean {

/**
 * 
 */
private static final long serialVersionUID = 1L;

@Column(name = "FirstName", nullable = false)
private String firstName;

@Column(name = "LastName", nullable = false)
private String lastName;

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "Dob", insertable = true, updatable = true, nullable = false)
private Date dateOfBirth;

@Column(name = "Email", length = 100)
private String email;

@Column(name = "PhoneNumber", nullable = false)
private String phoneNumber;

@Column(name = "Address1", nullable = false)
private String address1;

@Column(name = "Type", nullable = false)
private Short employeeType;

@Column(name = "Gender", nullable = false)
private Short gender;


/**
 * @return the firstName
 */
public final String getFirstName() {
    return firstName;
}

/**
 * @param firstName the firstName to set
 */
public final void setFirstName(String firstName) {
    this.firstName = firstName;
}

/**
 * @return the lastName
 */
public final String getLastName() {
    return lastName;
}

/**
 * @param lastName the lastName to set
 */
public final void setLastName(String lastName) {
    this.lastName = lastName;
}

/**
 * @return the dateOfBirth
 */
public final Date getDateOfBirth() {
    return dateOfBirth;
}

/**
 * @param dateOfBirth the dateOfBirth to set
 */
public final void setDateOfBirth(Date dateOfBirth) {
    this.dateOfBirth = dateOfBirth;
}

/**
 * @return the email
 */
public final String getEmail() {
    return email;
}

/**
 * @param email the email to set
 */
public final void setEmail(String email) {
    this.email = email;
}

/**
 * @return the phoneNumber
 */
public final String getPhoneNumber() {
    return phoneNumber;
}

/**
 * @param phoneNumber the phoneNumber to set
 */
public final void setPhoneNumber(String phoneNumber) {
    this.phoneNumber = phoneNumber;
}

/**
 * @return the address1
 */
public final String getAddress1() {
    return address1;
}

/**
 * @param address1 the address1 to set
 */
public final void setAddress1(String address1) {
    this.address1 = address1;
}

/**
 * @return the employeeType
 */
public final Short getEmployeeType() {
    return employeeType;
}

/**
 * @param employeeType the employeeType to set
 */
public final void setEmployeeType(Short employeeType) {
    this.employeeType = employeeType;
}


/**
 * @return the gender
 */
public final Short getGender() {
    return gender;
}

/**
 * @param gender the gender to set
 */
public final void setGender(Short gender) {
    this.gender = gender;
}
} 

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

employeeMasterBean.id 是空的吗? - M. Abbas
employeeMasterBeancreatedBy 已设置吗? - Ved
好像您正在传递一个分离的实体 ;) - Thomas
它在Bean中为空,但在数据库表中设置了。 - Neel Borooah
也许你应该尝试使用 merge 而不是 saveOrUpdate - proskor
合并也不起作用。与saveOrUpdate相同的异常。我认为我的对象状态有问题。 - Neel Borooah
4个回答

1
正如错误信息所说,数据库有一个名为createdby的列,它不能为空。
当您调用saveOrUpdate()时,有人将此属性设置为null,因此无法进行更新。

1
我认为在数据库表中,CreatedBy列作为notnull存在,但您的bean没有将此列映射,因此在执行saveOrUpdate时发送了一个null值,导致抛出上述异常。
您可以在bean中添加对CreatedBy的映射,并设置一些默认值,然后让触发器等更新默认值。或者如果您可以更改数据库中的列以允许为空。

1
豆类没有设置rowVersion属性(用于乐观锁定),因此默认值为null。因此,Hibernate将其解释为新记录并继续保存。
我通过在尝试保存或更新任何记录时将行版本存储在我的值对象和相应的Bean中来修复它。

0
我们可以在JSP表单中添加以下内容,以与实体的主键(id)匹配。
<form:hidden path="id" />

这并没有真正回答问题。如果您有不同的问题,可以通过点击提问来提出。如果您想在此问题获得新的答案时得到通知,您可以关注此问题。一旦您拥有足够的声望,您还可以添加悬赏以吸引更多关注。- 来自审核 - Alexey Veleshko

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接