-->
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.  [ 5 posts ] 
Author Message
 Post subject: Hashmap with Entity as Keys
PostPosted: Mon Feb 16, 2009 8:26 pm 
Newbie

Joined: Tue Jul 29, 2008 7:01 am
Posts: 9
There are a number of posts in the forum regarding problems when using Entities as Map keys, none of which have had any responses.

See here for an explanation of the issue:

http://forum.hibernate.org/viewtopic.ph ... entity+key


Is this a known limitation? Is there a workaround?

I am using Hibernate 3.3 but this seems to be a long standing issue.

Thanks.


Alan


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 16, 2009 11:42 pm 
Red Hat Associate
Red Hat Associate

Joined: Mon Aug 16, 2004 11:14 am
Posts: 253
Location: Raleigh, NC
It sounds like the linked post explains the issue. Have you tried making the Map not lazy? I would not expect a HashMap to work well on uninitialized proxies.

_________________
Chris Bredesen
Senior Software Maintenance Engineer, JBoss


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 17, 2009 2:40 pm 
Newbie

Joined: Tue Jul 29, 2008 7:01 am
Posts: 9
Tried a few things without success.

Code:
public class Course
{   
   @OneToMany(cascade=CascadeType.ALL)
   @JoinColumn(name="course_id", nullable=false)
   @ForeignKey(name="course_tees_courses_fk")
   @MapKey(name="teeType")
   private Map<TeeType, CourseTee> courseTees;
}


Code:
public class CourseTee
{
   @ManyToOne(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
   //@LazyToOne(value=LazyToOneOption.FALSE)
   @JoinColumn(name="tee_type", nullable=false)
   @ForeignKey(name="course_tees_tee_types_fk")
   private TeeType teeType;
}



TeeType equals and hashcode implemented correctly however map.contains(TeeType.X) always returns false.

Any ideas? Ditch the Map?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 17, 2009 4:05 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
TeeType equals and hashcode implemented correctly however map.contains(TeeType.X) always returns false.


Sounds like there might be some problem with the implementation. We are using map:s with entities as keys without any problem. We have based the hashCode() and equals() on the ID only and we never (I hope) mix transient and persistent items in the same map.

It shouldn't be a problem to have proxies as keys in a map, since if the hashCode() or equals() method is implemented by the entity class the proxy will be initialized when any of those methods are called.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 18, 2009 5:51 am 
Newbie

Joined: Tue Jul 29, 2008 7:01 am
Posts: 9
Unit testing would suggest Equals and Hashcode implementations are valid and that there are no issues getting values out of the Map for non-Hibernate managed objects.

When I run the test using an entity retrieved form the database via Hibernate, then:

TeeType nonHibernate = a;
TeeType fromHibernate = b;

[1] a.equals(b) is true
[2] a.hasCode() == b.hashCode() is true
[3] map.containsKey(a) = false;

equals() and hashcode() are not using the ID but some other field.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.