2013-08-30 163 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等更新默認值。或者如果您可以將列更改爲空數據庫