【问题标题】:Hibernate saveOrUpdate does not update休眠 saveOrUpdate 不更新
【发布时间】:2013-08-30 12:37:57
【问题描述】:

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

但是,它无法更新行并尝试通过生成插入语句来保存它。由于我的数据库中有一些不可为空的字段,此插入不起作用。

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

Bean Class:(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 是否为空?
  • employeeMasterBean has createdBy set ?
  • 听起来你正在传递一个分离的实体;)
  • 在Bean中为null,在DB表中设置。
  • 也许你应该试试merge 而不是saveOrUpdate

标签: java hibernate


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    要么在你的bean中添加一个到CreatedBy的映射,并使用一些默认值,然后让触发器等可以更新默认值。或者,如果您可以在数据库中将列更改为可为空

    【讨论】:

      【解决方案3】:

      该 bean 没有设置 rowVersion 属性(用于乐观锁定),因此默认情况下它为 null。因此 Hibernate 将其解释为一个新记录,并不断保存它。

      每当我尝试保存或更新任何记录时,我都会通过将行版本存储在我的值对象和相应的 Bean 中来修复它。

      【讨论】:

        【解决方案4】:

        我们可以在jsp表单中添加以下内容来匹配实体PK(id),

        <form:hidden path="id" />
        

        【讨论】:

        • 这并不能真正回答问题。如果您有其他问题,可以点击 进行提问。要在此问题有新答案时收到通知,您可以follow this question。一旦你有足够的reputation,你也可以add a bounty 来引起对这个问题的更多关注。 - From Review
        猜你喜欢
        • 2014-09-20
        • 1970-01-01
        • 2017-03-18
        • 1970-01-01
        • 2014-09-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-25
        相关资源
        最近更新 更多