-->
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.  [ 2 posts ] 
Author Message
 Post subject: How to create mapping for a Key-Value kinda Table ?
PostPosted: Thu Dec 04, 2008 4:54 pm 
Newbie

Joined: Wed Sep 03, 2008 11:39 am
Posts: 4
Hibernate version:3.2.6

Hi,

I have a database table EMP_TABLE like :
EMP_ID | KEY | VALUE
100 | EMP_NAME | John Scott
100 | CITY | NEW YORK
100 | COUNTRY | USA
240 | EMP_NAME | Shane Watt
240 | COUNTRY | CAN
300 | COUNTRY | USA

How should I write hibernate mappings for this table so that I can get the employee details & also save/update it, using following class ?
Code:
class Employee {
private int empId;
private String name;
private String city;
private String country;
.....getters & setters -----

}


Mapping :
Code:
<class name="Employee" table="EMP_TABLE">
   <id name="empId" column="EMP_ID">
      <generator class="assigned" />
   </id>
<!-- Need help to get the city, country & name from the  VALUE column using KEY column ----   ???????? ---- Please help -->


</class>




Any kind of clue, help is appreciated.
Code:


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 05, 2008 7:42 pm 
Newbie

Joined: Fri Dec 05, 2008 6:58 pm
Posts: 7
I don't know if there is any direct way of doing what you want.

Instead you could declare a Map<String,String> in your mapping file.

Using annotations this would be as follows -

@CollectionOfElements(fetch=FetchType.EAGER)
@JoinTable(name = "METADATA", joinColumns = @JoinColumn(name = "EMP_ID"))
@MapKey(columns={@Column(name="KEY", nullable = false)})
@Column(name = "VALUE", nullable = false)
private Map<String, String> getMetaData() {
return _metaData;
}


After defining this mapping - you can use getters and setters for city,name, etc to directly get or set properties from this map.
eg -
publci String getCity() {
return _metaData.get("City");
}

Of-course disadvantage would be that you are fetching entire map and not just the few properties that you are interested in and there is an extra join being done with a separate map table.


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