-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Single object multiple mappings
PostPosted: Tue Nov 01, 2005 12:37 pm 
Newbie

Joined: Sat Apr 23, 2005 6:06 pm
Posts: 6
Hibernate version:
3.0

Hi Guys,

Can anyone advise how I would map a single object to two different tables. I have the situation where i store data in a temp table (1) then later copy this data into a concrete table (2).

Other than creating two different objects and two different mapping files I dont know how to do this!

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 01, 2005 4:18 pm 
Regular
Regular

Joined: Thu Oct 27, 2005 8:06 am
Posts: 55
Location: München, Germany
As far as I can see, there can't be an easy solution for this. Even if Hibernate mapping files allowed you to describe two different mappings, there are so many situations in which Hibernate, explicitly or implicitly, has to write objects of that class -- so how should it decide which table to use? Your requirement to first use table1, then table2, can only be resolved by dynamic code, not by a static declarative mapping.

Using subclasses and mapping them as "table per concrete class" might be of use. But it neither reduces your mapping effort, nor does it relieve you to use two objects for storing, as one object can't change the subclass it belongs to during runtime in Java. It will, however, relieve you from declaring the attributes twice in your POJOs. That's useful, as it relieves you, in your further development, to always keep the attribute lists of both classes in sync.

There might be an adventurous way to go. "Hibernate in Action", in section 3.3.4, shortly describes dynamic manipulation of mapping data, using PersistentClass. Browsing PersistentClass in the Javadoc didn't reveal to me a way to change the table the mapping refers to. The Hibernate manual doesn't even mention this mechanism. But I didn't search too thoroughly, so maybe this, or a related mechanism can be used for that.

The reason I didn't search too thoroughly is that I wouldn't bother using such a sophisticated and dangerous mechanism for such a simple function. The algorithm has to tell Hibernate somehow which table to write to, so why not take that subclass approach and express the location change by copying from one object to the other?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 01, 2005 5:31 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
i assume you want single *class* mapped to different tables and be able to use them in different scenarios.

entity-name is your friend. is in the docs.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 02, 2005 4:58 am 
Newbie

Joined: Sat Apr 23, 2005 6:06 pm
Posts: 6
Can anybody direct me to the docs for this or show an example. I searched but could not find.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 02, 2005 5:13 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
there are at least 7 occurences of entity-name in the reference docs.

_________________
Max
Don't forget to rate


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


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

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.