2013-08-30 119 views
6

我想在Hibernate中使用session.saveOrUpdate()方法更新表行。休眠saveOrUpdate不更新

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

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

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 
+0

'employeeMasterBean.id' null? – mabbas

+0

'employeeMasterBean'已创建设置? – Ved

+0

听起来就像传递了一个分离的实体;) – Thomas

回答

1

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

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

0

作为错误消息说,数据库有一列createdby不能为空。

当您拨打saveOrUpdate()时,某人已将此属性设置为null,因此无法进行更新。

0

我觉得CreatedBy列存在于数据库表为notnull,但你的bean没有这个列映射,因此,当你做一个saveOrUpdate一个null值发送,这将导致上面的异常被抛出。

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