Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hibernate one HashMap with different types of values.

 
Repala Madhu
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Repala Madhu
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic