-->
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: How to map a Map
PostPosted: Thu Nov 19, 2009 5:43 am 
Newbie

Joined: Thu Nov 19, 2009 5:38 am
Posts: 4
Hi all.
I'm new to hibernate, and have found a problem when mapping a map using annotations.

I've been using hbm files, but want to move to annotations.

Let me explain myself :

Entity PLAN
<id name="plan_id"> <-- auto generated pk
...
...
<map name="browsing_thresholds" table="DATAPLAN_BROWSING_THRESHOLDS" lazy="true" cascade="delete" >
<key column="PLAN_ID" />
<map-key column="THRESHOLD" type="double" />
<element column="MESSAGE" type="string" length="160" />
</map>

This, maps to the following in MySql ..
+-----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| PLAN_ID | bigint(20) | NO | PRI | NULL | |
| MESSAGE | varchar(160) | YES | | NULL | |
| THRESHOLD | double | NO | PRI | NULL | |
+-----------+--------------+------+-----+---------+-------+


But i do not know how to use it in annotations.
All i get is :

@OneToMany(mappedBy="dataplan")
@MapKey(name="THRESHOLD")
private Map <Double, String> vol_thresholds = new HashMap<Double,String>();

But do not know how to map the table name , or the MESSAGE column.

Please help me :)


Top
 Profile  
 
 Post subject: Re: How to map a Map
PostPosted: Thu Nov 19, 2009 6:51 am 
Senior
Senior

Joined: Mon Feb 25, 2008 1:48 am
Posts: 191
Location: India
This is just an example available in the API docs. Something like this would do:
@Entity
public class Software {
@OneToMany(mappedBy="software")
@MapKey(name="codeName")
public Map<String, Version> getVersions() {
return versions;
}
...
}

@Entity
@Table(name="tbl_version")
public class Version {
public String getCodeName() {...}

@ManyToOne
public Software getSoftware() { ... }
...
}

In your scenario, your message column will be part of another entity that represents the element to be stored in your map. You must have two entities as described above

_________________
Sukirtha


Top
 Profile  
 
 Post subject: Re: How to map a Map
PostPosted: Thu Nov 19, 2009 6:54 am 
Newbie

Joined: Thu Nov 19, 2009 5:38 am
Posts: 4
Ok, so it was what i was afraid of .. two entities ...
Thanks a lot :-(


Top
 Profile  
 
 Post subject: Re: How to map a Map
PostPosted: Thu Nov 19, 2009 7:26 am 
Senior
Senior

Joined: Mon Feb 25, 2008 1:48 am
Posts: 191
Location: India
Yep, it is similar to your hbm concept. Even your hbm would define 2 entities. Nothing changes except the way you represent. All rules of hbm hold good for annotations as well.

_________________
Sukirtha


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.