| 
					
						 Hi 
 
 I have a problem using maps with Hibernate v3 that is best explained using the Department – Employee one to many scenario.
 
 My dept.hbn.xml mapping would be:
 
 [code]
 <hibernate-mapping>
 	<class name="com.unibasex.wom.to.DeptTO" table="DEPT" schema="WOM" lazy="false">
 		<id name="deptId" column="DEPT_ID" type="java.lang.Long" unsaved-value="null">
 		        <generator class="seqhilo">
         			<param name="sequence">DEPT_SEQ</param>
             			<param name="max_lo">100</param>
         		</generator>
 		</id>
 		<property name="deptName" column="DEPT_NAME" type="java.lang.Byte" length="1" />
 	
 		<!-- associations -->
     		<map name="employees" lazy="false" inverse="true" cascade="save-update">
       			<key column="DEPT_ID"/>
       			<index column="EMP_ID" type="long"/> 
       			<one-to-many class="com.unibasex.wom.to.EmpTO"/>
     		</map>
 	</class>
 </hibernate-mapping>
 [/code]
 
 And my emp.hbn.xml would be:
 
 [code]
 <hibernate-mapping>
 	<class name="com.unibasex.wom.to.EmpTO" table="EMP" schema="WOM" lazy="false">
 		<id name="empId" column="EMP_ID" type="java.lang.Long" unsaved-value="null">
 		        <generator class="seqhilo">
         			<param name="sequence">EMP_SEQ</param>
             			<param name="max_lo">100</param>
         		</generator>
 		</id>
 
 		<property name="firstName" column="FIRST_NAME" type="java.lang.String" length="50" />
 		<property name="lastNAme" column="LAST_NAME" type="java.lang.String" length="50" />
 
 		<!-- associations -->
 		<many-to-one name="Department" class="com.unibasex.wom.to.DeptTO">
 		        <column name="DEPT_ID" />
     		</many-to-one>
 	</class>
 </hibernate-mapping>
 [/code]
 
 Now the problem is when I want to create a department and say 2 employees and then persist it in one transaction.  When I add the 2 employees to a hashMap I need to use arbitary keys for the hashMap.  It all works fine in that these keys are obviously not used when persisting and if I later have to load this data then the hashMap will contain the employee id as the keys of the hashMap.  But staright after I have created the data the dept’s employee hashMap is incorrect as it contains the arbituary values as the keys.
 
 Example Code:
 
 [code]
 //Create department
 DeptTO deptTO = new DeptTO();
 deptTO.setName(“Accounting”);
 
 //Create 2 employees
 EmpTO emp1TO = newEmpTO();
 emp1TO.setFirstName(“John”);
 emp1TO.setLastName(“Smith”);
 emp1TO.setDepartment(deptTO);
 EmpTO emp2TO = newEmpTO();
 emp2TO.setFirstName(“Jane”);
 emp2TO.setLastName(“Doe”);
 emp2TO.setDepartment(deptTO);
 
 //Assign employees to the new department
 HashMap empMap = new HashMAp();
 empMap.put(new Long(1),emp1);
 empMap.put(new Long(2),emp2);
 deptTO.setEmployees(empMap);
 [/code]
 
 The only way I can see to get around this is to code the department manager so that after it has persisted the data it reads the employee hashMap and re-creates it using the employee id’s as the hashMap keys.
 
 Example Code:
 
 [code]
 HashMap newEmpMap = new HashMap();
 for (Iterator it = deptTO.getEmployees().values().iterator(); it.hasNext();) {
 EmpTO empTO = (EmpTO) it.next();
 newEmpMap.put(empTO.getEmpId(), empTO);
 }
 deptTO.setEmployees(newEmpMap);
 [/code]
 
 Any help and/or advice would be greatly appreciated. 
					
  
						
					 |