-->
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: Map with Entity as key
PostPosted: Mon Dec 17, 2007 3:08 pm 
Newbie

Joined: Tue May 01, 2007 9:11 am
Posts: 4
Location: Rochester, NY
Hi -- I have a persistent many- to-many map where the key is an entity. Is this supported? I have found _no_ examples of this, but I don't get mapping errors so my initial assumption was that it would be possible.

The parent entity:
Code:
@Entity
public class Portfolio {
   
   @Id @GeneratedValue
   private Long ID;
   
   @ManyToMany(cascade=CascadeType.ALL) @OrderBy
   private Map<Profile,ProfileWeight> profiles = new LinkedHashMap<Profile,ProfileWeight>();
}


The "key" child (Profile) is just another @Entity, I don't think it really matters what is in that. And the ProfileWeight is just a Double wrapped in an @Entity POJO.

Really, what this is modelling is a set of performance profiles, each with a corresponding weight. So a single profile can be in many Portfolios, with different weights.

Hibernate generates the tables correctly when the app starts up, but when I add values to the map and try to save the Portfolio, I get the following exception:
Code:
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: Profile

which is generated after I call entityManager.persist(portfolio) and entityManager.flush(). As far as I can tell this shouldn't be happening because I have the cascasde set to ALL

If I manually try to save the Profile keys first, I don't get any exception but it looks like the map values are not persisted.

Here is the ProfileWeight class:
Code:
@Entity
@Table(name="profile_weight")
public class ProfileWeight {
   
   @Id @GeneratedValue
   private Integer ID;
   @Version
   private Integer version;
   
   private double weight;
   
   @Override
   public boolean equals(Object obj) {
      return super.equals(obj) ||
      (obj instanceof ProfileWeight && ((ProfileWeight)obj).ID == this.ID );
   }
   
   @Override
   public int hashCode() {
      if( ID != null ) return ID;
      return super.hashCode();
   }
   
   public ProfileWeight(double weight) {
      this.weight = weight;
   }

   public double getWeight() {
      return weight;
   }

   public void setWeight(double weight) {
      this.weight = weight;
   }
}


Hibernate is auto-generating the actual DB schema. Any help would be appreciated -- thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 22, 2007 5:09 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Apparently you did not look as far as the reference documentation :)
Check 2.4.6.2.2. MapKeyManyToMany
http://www.hibernate.org/hib_docs/annotations/reference/en/html_single/#entity-hibspec-collection-extratype

_________________
Emmanuel


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.