Java 休眠 saveOrUpdate 不更新
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18533126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hibernate saveOrUpdate does not update
提问by Neel Borooah
I am trying to update a table row using the session.saveOrUpdate() method in Hibernate.
我正在尝试使用 Hibernate 中的 session.saveOrUpdate() 方法更新表行。
However, it is unable to update the row and tries to save it by producing an insert statement. This insert does not work due to a few non-nullable fields in my DB.
但是,它无法更新该行并尝试通过生成插入语句来保存它。由于我的数据库中有一些不可为空的字段,因此此插入不起作用。
I am able to retrieve the Id of the object to be saved at the DAO layer, so I am not able to understand why it doesn't just update the corresponding row in the DB table.
我能够检索要保存在 DAO 层的对象的 Id,所以我无法理解为什么它不只是更新 DB 表中的相应行。
Bean Class: (BaseEntityBean has the Id, CreatedBy, etc.)
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 Method:
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 debugger exceptions thrown are:
抛出的 Eclipse 调试器异常是:
could not insert: [com.indven.gpil.hrd.entity.EmployeeMasterBean]
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'CreatedBy' cannot be null
采纳答案by Neel Borooah
The bean did not have the rowVersion property (for optimistic locking) set, and hence by default it was null. Hibernate thus interpreted this as a new record, and kept saving it.
bean 没有设置 rowVersion 属性(用于乐观锁定),因此默认情况下它为空。因此,Hibernate 将此解释为一个新记录,并不断保存它。
I fixed it by storing the row version in my Value Object and the corresponding Bean whenever I attempted to save or update any records.
每当我尝试保存或更新任何记录时,我都会通过将行版本存储在我的值对象和相应的 Bean 中来修复它。
回答by Aaron Digulla
As the error message say, the database has a column createdby
which can't be null.
正如错误消息所说,数据库有一列createdby
不能为空。
When you called saveOrUpdate()
someone has set this property to null
so the update isn't possible.
当您呼叫saveOrUpdate()
某人时,已将此属性设置为,null
因此无法进行更新。
回答by Prashant Bhate
I think CreatedBy
column is present in DB table as notnull
but your bean does not have this column mapped, hence a null
value is sent when you do a saveOrUpdate
, which causes Above exception to be thrown.
我认为CreatedBy
列存在于 DB 表中,notnull
但是您的 bean 没有映射此列,因此null
当您执行 a 时会发送一个值saveOrUpdate
,这会导致抛出上述异常。
Either add a mapping to CreatedBy
in your bean with some default value and let trigger etc can update the default value. Or if you can change the column to be nullable in Database
要么CreatedBy
在你的 bean 中添加一个具有一些默认值的映射 ,让触发器等可以更新默认值。或者,如果您可以将列更改为可以在数据库中为空