AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Hibernate mapping directly to a HashMap or subclass ...

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java and Databases
View previous topic :: View next topic  
Author Message
nospawn
Guest





PostPosted: Wed Aug 16, 2006 10:50 pm    Post subject: Hibernate mapping directly to a HashMap or subclass ... Reply with quote



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
Back to top
Guest






PostPosted: Thu Aug 17, 2006 4:31 am    Post subject: Re: Hibernate mapping directly to a HashMap or subclass ... Reply with quote



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
Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java and Databases All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.