Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Databases / August 2006

Tip: Looking for answers? Try searching our database.

Hibernate mapping directly to a HashMap or subclass ...

Thread view: 
nospawn - 16 Aug 2006 18:50 GMT
Hi all,

I am new to Hibernate and need some directions here ...

I basically need Hibernate to allow me mapping any table to some
generic type i.e. Map or HashMap-derived class. I can not rely on a
strongly typed DAO not even a generic one. I need Hibernate to do the
mapping from any given table.

I would like to do something like:

class MapsToAny extends HashMap<String, Object> {
}

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping
   PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
   <class name="MapsToAny" table="order_new">
       <map name="this?">
           <key column="order_id"/>
           <index column="order_id" type="string"/>
           <element column="order_desc" type="string"/>
       </map>
   </class>
   <class name="MapsToAny" table="order_old">
       <map name="this?">
           <key column="order_id"/>
           <index column="order_id" type="string"/>
           <element column="order_desc" type="string"/>
       </map>
   </class>
</hibernate-mapping>

At most I would like to retrieve the data into a Collection, so I can
manipulate it later on but be able to specify outside code what the
query should be.

Any ideas?

TIA,
Best Regards,
Giovanni
rojkov@gmail.com - 17 Aug 2006 00:31 GMT
You should be able to do it this way:
<hibernate-mapping>

   <class entity-name="Customer">

       <id name="id"
           type="long"
           column="ID">
           <generator class="sequence"/>
       </id>

       <property name="name"
           column="NAME"
           type="string"/>

       <property name="address"
           column="ADDRESS"
           type="string"/>

       <many-to-one name="organization"
           column="ORGANIZATION_ID"
           class="Organization"/>

       <bag name="orders"
           inverse="true"
           lazy="false"
           cascade="all">
           <key column="CUSTOMER_ID"/>
           <one-to-many class="Order"/>
       </bag>

   </class>

</hibernate-mapping>

also, set the hibernate.default_entity_mode property to dynamic-map

Session s = openSession();
Transaction tx = s.beginTransaction();
Session s = openSession();

// Create a customer
Map david = new HashMap();
david.put("name", "David");

// Create an organization
Map foobar = new HashMap();
foobar.put("name", "Foobar Inc.");

// Link both
david.put("organization", foobar);

// Save both
s.save("Customer", david);
s.save("Organization", foobar);

tx.commit();
s.close();

Good Luck

http://www.jdbcpersistence.org - fast persistence


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.