-->
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.  [ 7 posts ] 
Author Message
 Post subject: CollectionOfElements and MapKey annotations
PostPosted: Fri May 22, 2009 6:47 am 
Newbie

Joined: Fri Oct 21, 2005 9:27 am
Posts: 13
Hi,

I have a small problem when mapping a Map.
I can't force Hibernate to rename the foreign key column which is used as the key for the map.

Here's the code :
Code:
// ...
@CollectionOfElements
@JoinTable(name = "ISSUE_CONTENTS", joinColumns = @JoinColumn(name = "ISSUE_ID_FK"))
@Column(name = "CONTENT")
@org.hibernate.annotations.MapKey(columns = {@Column(name = "LANG_ID")}, targetElement = Language.class)
@Lob
private Map<Language,String> contents = new HashMap<Language, String>();
//...


Where Language is an entity.

When I try to output the schema Hibernate generates, I get this
Code:
create table ISSUES (ISSUE_ID int8 not null, LOCKED bool, NUMBER int8 not null, PUBLICATION_DATE timestamp, NEWSLETTER_ID int8, primary key (ISSUE_ID))
create table ISSUE_CONTENTS (ISSUE_ID_FK int8 not null, CONTENT text, mapkey_LANG_ID int8 not null, primary key (ISSUE_ID_FK, mapkey_LANG_ID))
create table LANGUAGES (LANG_ID int8 not null, CODE varchar(255) unique, NAME varchar(255) unique, primary key (LANG_ID))
[...]
alter table ISSUE_CONTENTS add constraint FK95478BE0D350322E foreign key (mapkey_LANG_ID) references LANGUAGES
alter table ISSUE_CONTENTS add constraint FK95478BE095456796 foreign key (ISSUE_ID_FK) references ISSUES
[...]


So, instead of having the foreign key being called LANG_ID, it is called mapkey_LANG_ID.
It is misleading though, even if I would have put @Column(name = "FOO"), it would have outputted the same generated SQL, with mapkey_LANG_ID as the foreign key name.

My question is : I'm quite sure I missed something in my mapping. What did I do wrong ?


Top
 Profile  
 
 Post subject: Re: CollectionOfElements and MapKey annotations
PostPosted: Sat May 23, 2009 5:04 am 
Newbie

Joined: Sat May 23, 2009 4:59 am
Posts: 3
I've exactly the same problem, but with the generated select sentences. I've a legacy database and can't change the name of the fields, so it's very important for me not having the "mapkey_" before the real name of the field.


Top
 Profile  
 
 Post subject: Re: CollectionOfElements and MapKey annotations
PostPosted: Sat May 23, 2009 11:23 am 
Newbie

Joined: Fri Oct 21, 2005 9:27 am
Posts: 13
If our mapping is correct, then this behaviour is a bug and it should be reported.
I'll submit a bug report.


Top
 Profile  
 
 Post subject: Re: CollectionOfElements and MapKey annotations
PostPosted: Sun May 31, 2009 10:08 am 
Newbie

Joined: Sat May 23, 2009 4:59 am
Posts: 3
There is no bug here, at least for my part: my mapping was wrong. Finally I found the problem thanks to the Hibernate documentation for annotations (version 3.4.0, the last one).
I'll describe my case and hope it can help.
I've an entity called State that have a Map of Key and values. Key can't be anything, but I've a table called Keys that contains all the possible values it can take. It corresponds to an entity called Key that I use as keys in the Map. In that documentation, it is said that "you can use @org.hibernate.annotations.MapKeyManyToMany if your key is an entity".
I tried to change the Mapkey annotation with the MapKeyManyToMany one, modifying the attributes, and it worked correctly.
The related source code is as following:

Code:
@Entity
@Table(name="KEYS")
public class Key {
   @Id
   @Column(name="KEY_ID")
   private String keyId;
...
}

@Entity
@Table(name="STATES")
public class State {
   @Id
   @GeneratedValue(strategy=GenerationType.AUTO)
   @Column(name="STATE_ID")
   private Long stateId;

   @CollectionOfElements(targetElement=java.lang.String.class)
   @JoinTable(
         name="STATE_KEY_VALUES",
         joinColumns=@JoinColumn(name="STATE_ID")
   )
   @MapKeyManyToMany(
         targetEntity=Key.class,
         joinColumns=@JoinColumn(name="KEY_ID")
   )
   @Column(name="VALUE")
   private Map<Key, String> stateKeys = new HashMap<Key, String>();
...
}


Top
 Profile  
 
 Post subject: Re: CollectionOfElements and MapKey annotations
PostPosted: Tue Jun 02, 2009 5:36 am 
Newbie

Joined: Fri Oct 21, 2005 9:27 am
Posts: 13
Thanks for the hint, but even with that annotation, it's still the same for me :

Code:
// ...
@CollectionOfElements
@JoinTable(name = "SUBST_TRANS", joinColumns = @JoinColumn(name = "SUBST_ID_FK"))
@Column(name = "TRANSLATION")
@MapKeyManyToMany(joinColumns = {@JoinColumn(name = "LANG_ID")})
private Map<Language, String> translations = new HashMap<Language, String>();
// ...


It gives me the same output as before :
Code:
# ...
create table SUBST_TRANS (SUBST_ID_FK int8 not null, TRANSLATION varchar(255), mapkey_LANG_ID int8 not null, primary key (SUBST_ID_FK, mapkey_LANG_ID))
alter table SUBST_TRANS add constraint FK4C9C7D4AD350322E foreign key (mapkey_LANG_ID) references LANGUAGES
# ...


I still don't get what I'm doing wrong :-/.


Top
 Profile  
 
 Post subject: Re: CollectionOfElements and MapKey annotations
PostPosted: Tue Jun 02, 2009 6:49 am 
Newbie

Joined: Sat May 23, 2009 4:59 am
Posts: 3
Try to specify the "targetEntity" attribute of the MapKeyManyToMany annotation with your Language entity.
It should work without it, but maybe Hibernate can't determine your entity class automatically.


Top
 Profile  
 
 Post subject: Re: CollectionOfElements and MapKey annotations
PostPosted: Tue Jun 02, 2009 9:16 am 
Newbie

Joined: Fri Oct 21, 2005 9:27 am
Posts: 13
eternay wrote:
Try to specify the "targetEntity" attribute of the MapKeyManyToMany annotation with your Language entity.
It should work without it, but maybe Hibernate can't determine your entity class automatically.


No, unfortunately that doesn't work either.


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