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: Hibernate Mapping (HashMap) Query/Problem
PostPosted: Wed Nov 02, 2005 9:21 am 
Newbie

Joined: Wed Nov 02, 2005 9:15 am
Posts: 2
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.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 03, 2005 1:58 am 
Newbie

Joined: Wed Nov 02, 2005 9:15 am
Posts: 2
Is this not a bug as Hibernate should, after persisting, update the map's key values with the object's primary key (ie Employee Id)?

Andrew


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.