I am trying to update a table row using the session.saveOrUpdate() method in Hibernate.
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.
Bean Class: (BaseEntityBean has the Id, CreatedBy, etc.)
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:
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:
could not insert: [com.indven.gpil.hrd.entity.EmployeeMasterBean]
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'CreatedBy' cannot be null
Difference between save and saveOrUpdate in Hibernate The main difference between save and saveOrUpdate method is that save() generates a new identifier and INSERT record into the database while saveOrUpdate can either INSERT or UPDATE based upon the existence of a record.
saveOrUpdate() Hibernate will check if the object is transient (it has no identifier property) and if so it will make it persistent by generating it the identifier and assigning it to session. If the object has an identifier already it will perform . update() .
save Save method stores an object into the database. That means it insert an entry if the identifier doesn't exist, else it will throw error. If the primary key already present in the table, it cannot be inserted. update Update method in the hibernate is used for updating the object using identifier.
Hibernate saveOrUpdate results into insert or update queries based on the provided data. If the data is present in the database, update query is executed. We can use saveOrUpdate() without transaction also, but again you will face the issues with mapped objects not getting saved if session is not flushed.
As the error message say, the database has a column createdby
which can't be null.
When you called saveOrUpdate()
someone has set this property to null
so the update isn't possible.
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.
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
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With