Hibernate saveOrUpdate does not update

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; /** * @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 exception exceptions:

 could not insert: [com.indven.gpil.hrd.entity.EmployeeMasterBean] com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'CreatedBy' cannot be null 
+6
source share
3 answers

The rowVersion property was not set in the bean (for optimistic locking), and by default it was null. Thus, Hibernate interpreted this as a new record and saved it.

I fixed this by storing the version of the string in my Value object and the corresponding bean whenever I tried to save or update records.

+1
source

As the error message says, the database has a createdby column, which cannot be null.

When you called saveOrUpdate() , someone set this property to null , so updating is not possible.

0
source

I think the CreatedBy column is present in the DB table as notnull , but your bean does not display this column, so the null value is sent when you execute saveOrUpdate , which raises the exception above to be thrown.

Or add the mapping in CreatedBy to the bean with some default value, and let the trigger, etc. can update the default value. Or, if you can change a column with a null value in the database

0
source

Source: https://habr.com/ru/post/952831/


All Articles