• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

hibernate one HashMap with different types of values.

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
I will suppress my every urge. But not this shameless plug:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic