Hi All,
I have a one-to-many entity (Department-has-Employees) and I want to create department in bulk like this
Code:
public void bulkInsert(Collection departments) {
Session session = null;
try {
session = sf.openSession();
int j = 0;
Iterator i = departments.iterator();
while(i.hasNext()) {
Department u = (Department)i.next();
System.out.println("Saving " + j++ + " departments ");
session.save(u);
}
} catch (Exception e){
System.out.println("Error in bulk insert of dept "+e.getMessage());
e.printStackTrace();
} finally {
try {
session.flush();
session.close();
} catch (Exception e) {
System.out.println("Hibernate Exception " + e.getMessage());
e.printStackTrace();
}
}
}
A hibernate exception is thrown while trying to save the second department with the following message
Quote:
Another object was associated with this id (the object with the given id was already loaded): [com.infosys.j2ee.cmptest.model.Employee#962]
When I try bulk insert for a single entity without relationships it works for the same kind of DAO code.
What could be the problem ?
I have attached the mapping file
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="com.infosys.j2ee.cmptest.model.Department" table="DepartmentEJBTable">
<id name="primaryKey">
<generator class="sequence">
<param name="sequence">test2_seq</param>
</generator>
</id>
<property name="name">
<column name="NAME"/>
</property>
<property name="location">
<column name="LOCATION"/>
</property>
<bag name="employees" inverse="true" outer-join="true" cascade="all">
<key column="departmentid"/>
<one-to-many class="com.infosys.j2ee.cmptest.model.Employee"/>
</bag>
</class>
<class name="com.infosys.j2ee.cmptest.model.Employee" table="EmployeeEJBTable">
<id name="primaryKey">
<generator class="sequence">
<param name="sequence">test2_seq</param>
</generator>
</id>
<property name="name">
<column name="NAME"/>
</property>
<property name="employeeNumber">
<column name="EMPLOYEENUMBER"/>
</property>
<property name="hireDate">
<column name="HIREDATE"/>
</property>
<property name="salary">
<column name="SALARY"/>
</property>
<many-to-one name="department" class="com.infosys.j2ee.cmptest.model.Department" column="departmentid" cascade="all"/>
</class>
</hibernate-mapping>