-->
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.  [ 1 post ] 
Author Message
 Post subject: Flex + Java + Hibernate
PostPosted: Tue May 26, 2009 9:07 am 
Newbie

Joined: Thu Nov 20, 2008 6:41 am
Posts: 3
Hi All,

I am facing a peculiar problem while saving the data. Below is the detailed explanation what I am doing. I always get the problem as "a different object with the same identifier value was already associated with the session"

But the above error does not come when I try to save the same object from java.

Below is the Parent Class in java
Code:
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* Employee entity. @author MyEclipse Persistence Tools
*/

public class Employee implements java.io.Serializable {

   private Integer employeeId;
   private String name;
   private List communicationTypes = new ArrayList();

// getters and setters follows


Below is the Parent class in Flex
Code:
package manageCustomList.employee
{
   import mx.collections.ArrayCollection;
   
   [Bindable]
   [RemoteClass(alias="com.config.Employee")]
   public class Employee
   {
      public function Employee()
      {
      }

      public var employeeId:int;
      
      public var name:String;

      public var communicationTypes:Array;
   }
}


Below is the Child Class in java
Code:
public class CommunicationType extends Employee implements java.io.Serializable {
   private Integer comTypeId;
   private Integer typeId;
   private String typeName;
// getters and setters follows
}


Below is the child Class in Flex
Code:
package manageCustomList.employee
{
   import mx.controls.List;
   
   [Bindable]
   [RemoteClass(alias="com.config.CommunicationType")]
   public class CommunicationType extends Employee
   {
      public function CommunicationType()
      {
      }

      public var comTypeId:int;

      public var typeId:int;

      public var typeName:String;

   }
}


Below is the method to save the object in the database
Code:
public static Employee create(Employee employee) throws DAOException
   {
      Session session = HibernateSessionFactory.getSession();
      try
      {
         session.beginTransaction();
         session.save(employee);
         session.getTransaction().commit();
         session.flush();
         session.evict(employee);
      }
       catch (HibernateException exp) {
          exp.printStackTrace();
      } finally {
         session.close();
      }
      return employee;
   }


Below is my mxml
Code:
private function save():void {
   employee = new Employee();

   employee.listId = Number(listId.text);
   employee.name = empName.text;

   var childList:Array = new Array();

   for(var i:int=0;i<checkList.selectedIndices.length;i++){
      comType = new CommunicationType();
      comType.typeId = checkList.selectedIndices[i];
      comType.typeName = checkList.dataProvider[checkList.selectedIndices[i]].label;
      childList.push(comType);
   }

   employee.communicationTypes = childList;

   emp.create(employee);
}


when I do the above i get the error as "a different object with the same identifier value was already associated with the session"

But when I try to save the employee from java as below. It gets saved without any problem.
Code:
public static void main(String[] args) {
   Employee emp = new Employee();
   emp.setName("fresh from java only 2");

   CommunicationType comType = new CommunicationType();
   comType.setTypeId(1);
   comType.setTypeName("typeName");

   List comSet = new ArrayList();
   comSet.add(comType);

   emp.setCommunicationTypes(comSet);
   create(emp);
}


below are my hbm files
Employee.hbm.xml
Code:
<hibernate-mapping>
    <class name="com.config.Employee" table="EMPLOYEE" schema="DISLIST">
        <id name="employeeId" type="java.lang.Integer" unsaved-value="-1">
            <column name="EMPLOYEE_ID" />
            <generator class="identity" />
        </id>
      <property name="listId" type="java.lang.Integer">
            <column name="LIST_ID" length="4" />
        </property>       
        <property name="name" type="java.lang.String">
            <column name="EMP_NAME" length="50" not-null="true" />
        </property>
        <bag name="communicationTypes" cascade="all" lazy="false">
      <key column="EMPLOYEE_ID"/>
      <one-to-many class="com.ibm.dlm.config.CommunicationType"/>
   </bag>
    </class>
</hibernate-mapping>



CommunicationType.hbm.xml
Code:
<hibernate-mapping>
    <class name="com.config.CommunicationType" table="COMMUNICATION_TYPE" schema="DISLIST">
        <id name="comTypeId" type="java.lang.Integer" unsaved-value="-1" >
            <column name="COM_TYPE_ID" />
            <generator class="identity" />
        </id>
        <property name="typeId" type="java.lang.Integer">
            <column name="TYPE_ID" not-null="true" />
        </property>
        <property name="typeName" type="java.lang.String">
            <column name="TYPE_NAME" length="40" not-null="true" />
        </property>
    </class>
</hibernate-mapping>


Let me know if you any refernce of other things.

Any help would be really helpful

Thanks in advance


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

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.