gpt4 book ai didi

java - Hibernate saveOrUpdate 不更新

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:32:59 24 4
gpt4 key购买 nike

我正在尝试使用 Hibernate 中的 session.saveOrUpdate() 方法更新表格行。

但是,它无法更新该行并尝试通过生成插入语句来保存它。由于我的数据库中有一些不可为 null 的字段,此插入不起作用。

我能够检索要保存在 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

最佳答案

如错误消息所述,数据库中有一列 createdby 不能为空。

当您调用 saveOrUpdate() 时,有人将此属性设置为 null,因此无法进行更新。

关于java - Hibernate saveOrUpdate 不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18533126/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com