-->
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.  [ 1 post ] 
Author Message
 Post subject: Mapping a map without a join table
PostPosted: Mon Dec 24, 2012 11:25 pm 
Newbie

Joined: Mon Dec 24, 2012 11:06 pm
Posts: 1
According to the documentation, to map a <String,Object> map without using a join table, you need to use the @MapKey annotation. You set the name attribute of @MapKey to a field in the value class, and that will be used for the key of the map, like this:

Code:
@Entity
public class BaseClass {
    @Id
    private int Id;

    @OneToMany(mappedBy="base")
    @MapKey(name="key")
    private Map<String, ValueClass> myMap;
}

@Entity
public class ValueClass {
    @ManyToOne
    @JoinColumn(name="base_id")
    private BaseClass base;

    private String key;
{


This works, but now In Java you're storing the key twice, once as the key of the map, and again as a field in the value class. In my case, I'm getting my data from a JSON web service and generating my POJOs with the Jackson JSON parser. Before I save this data to my database with Hibernate, I have to add code that will copy all the keys of all maps to the corresponding key field in the value objects.

This is ugly IMO. Is there a way I can make hibernate automatically add a column for the key in the value class without actually adding a field for it in the POJO? If I don't add the @MapKey annotation, hibernate actually adds a valueclass_KEY column, but it's value is always null.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.