-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Error in Bulk Insert of data
PostPosted: Fri Oct 31, 2003 2:44 am 
Newbie

Joined: Tue Oct 28, 2003 2:20 am
Posts: 9
Location: Bangalore, India
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>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 31, 2003 3:57 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Presumably there are two different instances of Employee, both with an id value of 962, somewhere in your object graph.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.