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.  [ 3 posts ] 
Author Message
 Post subject: Mapping a Map<@Entity, enum> with @ManyToMany
PostPosted: Mon Mar 08, 2010 5:11 am 
Newbie

Joined: Sun Feb 14, 2010 10:13 am
Posts: 17
Hello everybody

I have a question concerning a mapping of Map<@Entity, enum>.

My mapping is specified as:

Code:
public class MyEntityClass {

   private Map<OtherEntityClass, RelationEnum> relatedEntities = new HashMap<OtherEntityClass, RelationEnum>();
   ...
   
   @ManyToMany
   @JoinTable(name="TBL_ASSOCIATED_ENTITIES",
         joinColumns=@JoinColumn(name="MY_ENTITY_ID"),
         inverseJoinColumns={@JoinColumn(name="OTHER_ENTITY_ID")})
   @MapKeyManyToMany(joinColumns={@JoinColumn(name="OTHER_ENTITY_ID")})   
   @Column(name="RELATION_ENUM_ID")
   public Map<OtherEntityClass, RelationEnum> getRelatedEntities () {
      return this.relatedEntities;
   }

}


If I use @ManyToMany (the appropriate association with the OtherEntityClass), I get an exception:


Quote:
org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: ...OtherEntityClass[RelationEnum]
....


Looks like Hibernate expects an @Entity as the map value too, not only for the key. I couldn't find a way to declare the enum such that it will persist to a column under @ManyToMany, at least not via annotations.

The enum is describing the type of relation between the two entities so there is no reason why to create a new entity just for that.

One way to workaround this is using @CollectionOfElements (omitting the inverseJoinColumn attr in @JoinTable). While this works, I don't think it's the proper solution because @CollectionOfElements relates mainly to embedded fields, not separate Entities and there are several implications - for example, I can't use the mappedBy attr.


Is there a way to directly persist a Map<@Entity, enum> using @ManyToMany?

Thanks,
Yuval


Top
 Profile  
 
 Post subject: Re: Mapping a Map<@Entity, enum> with @ManyToMany
PostPosted: Mon Mar 08, 2010 5:59 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
That what you want to do is actually known as 'Adding columns to join tables'
(book Java Persistence with Hibernate chapter 7.2.3).
You may find some related information searching by this therm.


Top
 Profile  
 
 Post subject: Re: Mapping a Map<@Entity, enum> with @ManyToMany
PostPosted: Mon Mar 08, 2010 3:04 pm 
Newbie

Joined: Sun Feb 14, 2010 10:13 am
Posts: 17
I reviewed that section, thank you. I actually saw that section before but found the following 7.2.4 "Mapping Maps" even more related. The thing is, IMHO all these approaches don't really answer this case. They all have one thing in common: they force you to create a new entity/value type even if it's not needed - thus imposing a concrete design, just to fit the DB schema and satisfy the persistence layer. Isn't that something that Hibernate supposed to completely avoid?

For example, if following the business domain in the book, what if I have an Item and Bids in a Many-to-Many association (each bid can have many items too), describing also if a particular bid did or didn't win a particular item. In the database it would indeed translate to: ITEMS -(n:m)- ITEM_BIDS -(n:m)- BIDS , where ITEM_BIDS will hold the boolean column ITEM_BID_IS_WINNING.

But in the object domain, you don't need to express this relation by creating a new type (either entity or value) - you can also do it in class Item: Map<Bid, Boolean> or in class Bid: Map<Item, Boolean>. If we take the solution of the book, it would require us to create a new type ItemBid (extends Bid?) just for containing that Boolean.

Although one can argue that ItemBid should be created even for one column (can grow etc') I think Hibernate should be able to handle this case too - supporting @OneToMany or @ManyToMany with Maps containing non-mapped values and persisting them to a column in the joinTable.


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