aspose file tools*
The moose likes Object Relational Mapping and the fly likes hibernate one HashMap with different types of values. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "hibernate one HashMap with different types of values." Watch "hibernate one HashMap with different types of values." New topic
Author

hibernate one HashMap with different types of values.

Repala Madhu
Greenhorn

Joined: Jun 12, 2007
Posts: 14
Hi all,

I want to hibernate one HashMap, which can contain String as key and value can be any type (Integer, String , BitSet or Long).

How to configure map in mapping file.


Can anybody please help me in this regard.

Thanks in advance.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

That is a weird relational model. Can you describe the tables you are mapping? Is this an association with four tables? Or four differernt fields on the one table?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Repala Madhu
Greenhorn

Joined: Jun 12, 2007
Posts: 14
Table name: supportproperty

id name

__________________________________________

Table name: properties

id property_value property_name

__________________________________________



SupportProperty.hbm.xml



<hibernate-mapping auto-import="true" package="java.util.BitSet">
<class name="com.SupportProperty" table="supportproperty">
<id name="id">
<generator class="native"/>
</id>

<map name="properties">
<key column="id"/>
<index column="property_name" type="string"/>
<element column="property_value" type="string"/>
</map>
<property name="name" type="string"/>
</class>
</hibernate-mapping>






SupportProperty sp = new SupportProperty();
Transaction tx = session.beginTransaction();
sp.setName("Joe");

BitSet bs = new BitSet();
bs.set(4);
HashMap p = new HashMap();
p.put("color", "blue");
p.put("lnf", "mac");
p.put("bitSet", bs);
sp.setProperties(p);

session.save(sp);
session.flush();
tx.commit();



see above
There i want to put java.util.BitSet in the HashMap

it is giving exception as followis

org.hibernate.MappingException: Unknown entity: bitSet
Repala Madhu
Greenhorn

Joined: Jun 12, 2007
Posts: 14
Oh sorry paul
My hibernate mapping file is as follows.

<hibernate-mapping>
<class name="com.SupportProperty" table="supportproperty">
<id name="id">
<generator class="native"/>
</id>

<map name="properties">
<key column="id"/>
<index column="property_name" type="string"/>
<element column="property_value" type="string"/>
</map>
<property name="name" type="string"/>
</class>
</hibernate-mapping>
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

That looks like you have a Map<Integer, String>, rather than a Map<Integer, Object>. If you want it to behave as you ask you probably need to either define a custom type that parsed your string data in to the object you want, or use the table per class hierarchy pattern and add property value fields for each type you support (plus a disciminator).
 
jQuery in Action, 2nd edition
 
subject: hibernate one HashMap with different types of values.