-->
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.  [ 4 posts ] 
Author Message
 Post subject: MapKey problems
PostPosted: Wed Sep 16, 2009 11:44 am 
Newbie

Joined: Wed Jun 13, 2007 5:44 am
Posts: 16
Hi!

I'm trying to map a Collection correctly, but somehow can't do it.

Here's my main entity.
I have a collection here constructed as a map. I'd like the map key to be a String, the stringId property of Directory, which is a property of Entry.
But it doesn't work
Code:
@Entity
public class EntryOwner {

   @OneToMany(mappedBy = "entryOwner")
   @MapKey(name = "directory.stringId")
   private Map<String, Entry> entries = new HashMap<String, Entry>();
}


Code:
@Entity
public class Entry {
   @NotNull
   @ManyToOne
   private Directory directory;
   @OneToOne
   private EntryOwner entryOwner;
}

Code:
@Entity
public class Directory implements Serializable {
   @OneToMany(cascade = CascadeType.ALL, mappedBy = "directory")
   private List<Entry> entries;
   private String stringId;
}


But, as stated at the beginning of my post, it doesn't work, as I got this error:
Code:
Map key property not found: com.lrb.modules.directory.Entry.directory.stringId


How to map that correctly?


Top
 Profile  
 
 Post subject: Re: MapKey problems
PostPosted: Wed Sep 16, 2009 12:35 pm 
Senior
Senior

Joined: Mon Jul 07, 2008 4:35 pm
Posts: 141
Location: Berlin
Hi Zerg-Spirit,

Hibernate is looking for the specified map key in Entry and does not find it. There is no field/column of that name.

Create a back reference in Entry to Directory.stringID and use that for the map key. That might help.

CU
Froestel

_________________
Have you tried turning it off and on again? [Roy]


Top
 Profile  
 
 Post subject: Re: MapKey problems
PostPosted: Thu Sep 17, 2009 5:03 am 
Newbie

Joined: Wed Jun 13, 2007 5:44 am
Posts: 16
Thank you.

How would you do that back reference. It would need to be a property Hibernate would fetch from the directory automatically, not a useless field in the database.


Top
 Profile  
 
 Post subject: Re: MapKey problems
PostPosted: Thu Sep 17, 2009 6:43 am 
Senior
Senior

Joined: Mon Jul 07, 2008 4:35 pm
Posts: 141
Location: Berlin
Hi Zerg-Spirit,

in http://docs.jboss.org/hibernate/stable/annotations/reference/en/html/entity.html#entity-mapping-association-collections the Hibernate Annotations docs say
Quote:
EJB3 allows you to map Maps using as a key one of the target entity property using @MapKey(name="myProperty") (myProperty is a property name in the target entity). When using @MapKey (without property name), the target entity primary key is used. The map key uses the same column as the property pointed out: there is no additional column defined to hold the map key, and it does make sense since the map key actually represent a target property.

Therefore, you will need a column in the same table (a.k.a. a field in the same entity) for Hibernate to refer to.

I guess the fastest - but actually not very nice - way to do it, is having a field in Entity that receives the stringID value as soon as Entity's Directory is set. That field should be mapped to the Entity table. This is not very nice because DB integrity cannot be guaranteed as long as you do not "connect" the stringID of Entity and that of Directory on DB level with a constraint. It also produces redundant data. You don't want that.

See, i.e., http://docs.jboss.org/hibernate/stable/annotations/reference/en/html/entity.html#d0e2273 from the Hibernate Annotations documentation about the @Formula annotation. This can probably help you out by creating a virtual column to refer to. I don't have so much experience with annotations but when using mapping files the <map-key> element has an optional attribute formula that is probably exactly made for this purpose.

CU
Froestel

_________________
Have you tried turning it off and on again? [Roy]


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