I am trying to update a table row using session.saveOrUpdate () method in Hibernate.
However, it cannot update the row and tries to save it by creating an insert statement. This insertion does not work due to several fields that cannot be nullified in my database.
I can get the identifier of the object that will be saved at the DAO level, so I canβt understand why it is not just updating the corresponding row in the DB table.
Bean Class: (BaseEntityBean has 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; public final String getFirstName() { return firstName; } public final void setFirstName(String firstName) { this.firstName = firstName; } public final String getLastName() { return lastName; } public final void setLastName(String lastName) { this.lastName = lastName; } public final Date getDateOfBirth() { return dateOfBirth; } public final void setDateOfBirth(Date dateOfBirth) { this.dateOfBirth = dateOfBirth; } public final String getEmail() { return email; } public final void setEmail(String email) { this.email = email; } public final String getPhoneNumber() { return phoneNumber; } public final void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; } public final String getAddress1() { return address1; } public final void setAddress1(String address1) { this.address1 = address1; } public final Short getEmployeeType() { return employeeType; } public final void setEmployeeType(Short employeeType) { this.employeeType = employeeType; } public final Short getGender() { return gender; } 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 exception exceptions:
could not insert: [com.indven.gpil.hrd.entity.EmployeeMasterBean] com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'CreatedBy' cannot be null
source share