Hibernate mapping directly to a HashMap or subclass ...

N

nospawn

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
 
R

rojkov

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top