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
[/code]

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;
        }
}
[/code]

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
}
[/code]

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;

        }
}
[/code]

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;
        }
[/code]

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);
}
[/code]

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);
}[/code]

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>

[/code]

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>
[/code]

Let me know if you any refernce of other things.

Any help would be really helpful

Thanks in advance

Reply via email to