我有一个特殊的问题。每次,我在会话上调用合并,Hibernate 都会保留一个全新的对象。我在 Spring MVC 应用程序中使用 Hibernate 3.6。
请在下面找到我的代码:
我的 hibernate.cfg.xml
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.Oracle10gDialect </property>
<!-- this will show us all sql statements -->
<property name="hibernate.show_sql"> true </property>
<property name="connection.pool_size">1</property>
<!-- <property name="hbm2ddl.auto">create</property>-->
<!-- mapping files -->
<mapping resource="com/hibernate/hbm/employee.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>
我的员工.hbm.xml
<hibernate-mapping default-lazy="true">
<class name="com.spring.model.Employee" table="employee">
<id name="empId" type="long" column="empId" unsaved-value="null">
<generator class="sequence">
<param name="sequence">hibernate_sequence</param>
</generator>
</id>
<version name="version" column="version" unsaved-value="null"
type="long" />
<component name="identity" class="com.spring.model.Identity">
<property name="firstname" column="firstname" not-null="true" />
<property name="lastname" column="lastname" not-null="true" />
<property name="email" column="emailid" not-null="true" />
</component>
<!-- <property name="birthday" column="birthday"/> -->
<property name="fileDataBytes" column="filedata" />
<property name="fileName" column="fileName" />
<property name="fileContentType" column="fileContentType" />
</class>
</hibernate-mapping>
我的模型课
public class Employee extends BaseModel{
private CommonsMultipartFile fileData;
private byte[] fileDataBytes;
private String fileName;
private String fileContentType;
private Identity identity;
private long empId;
//getters,setters /equals() on empId field
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (int) (empId ^ (empId >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Employee other = (Employee) obj;
if (empId != other.empId)
return false;
return true;
}
public class BaseModel implements Serializable{
private Long version;
//gettes,setters
public class Identity {
protected String firstname;
protected String lastname;
protected String email;
//getters.setters
我的employeeDAOImpl.java 的save方法
public long saveEmployee(Employee employee) throws Exception {
public Employee getEmployeeById(long empId) {
// TODO Auto-generated method stub
return (Employee) getSessionFactory().getCurrentSession().load(Employee.class,empId);
}
}
// TODO Auto-generated method stub
if(employee.getEmpId() == 0){
return (Long)getSessionFactory().getCurrentSession().save(employee);
}else{
Employee empInSession = getEmployeeById(employee.getEmpId());
getSessionFactory().getCurrentSession().merge(employee);
return employee.getEmpId();
}
}
注意:我已经在 GET 循环中加载了对象,但是为了确保将对象加载到缓存中,我仍然在调用 merge() 之前加载它。理想情况下,根本不需要合并,随着对象变得持久。为什么会发生这种情况?Hibernate 使用更改的属性保存一个新对象并保存它。它不应该通过equals检查中的empId字段检查吗?