aspose file tools*
The moose likes Object Relational Mapping and the fly likes One Mapping for two tables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "One Mapping for two tables" Watch "One Mapping for two tables" New topic
Author

One Mapping for two tables

Krishna Radha
Greenhorn

Joined: Aug 30, 2001
Posts: 25
If we use middlegen, it is always generating one hbm.xml file for each table in database. Is it possible to create one class for more than one table? Can we map one java object to more than one table?
Lavern Matthew
Ranch Hand

Joined: Jun 05, 2004
Posts: 38
Using middlegen does complicate this... but you could always make a view that combimes the two tables.


Matthew Wilson<br />-http://valuelist.sourceforge.net
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Of course the view would be read only...

You can map two tables as one in Hibernate, but it is frowned upon. Which is probably why there is no way to do it in middlegen. So your only option is to hand craft the mapping file and POJO from the four files middlegen will generate.
[ June 09, 2004: Message edited by: Paul Sturrock ]

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Krishna Radha
Greenhorn

Joined: Aug 30, 2001
Posts: 25
I feel middlegen generates only one mapping file for each table. And the POJO code is generated by hibernate's hbm2java.
How can we manually create one mapping file for two tables together to generate one class file?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Create the file [table_name].hbm.xml, open it with a text editor and write the mapping.

Once you have the mapping, you can run hbm2java.
Krishna Radha
Greenhorn

Joined: Aug 30, 2001
Posts: 25
Create the file [table_name].hbm.xml, open it with a text editor and write the mapping.

Once you have the mapping, you can run hbm2java.


Paul

I know we need to open the file in text editor and manually edit the file. But what is the syntax like creating one map (one class mapped to two tables)?



Thanks for all your help.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

You map the tables to the same class name. You could map the whole schema this to one POJO if you wanted. There's an example in the Hibernate FAQs.
[ June 10, 2004: Message edited by: Paul Sturrock ]
Lavern Matthew
Ranch Hand

Joined: Jun 05, 2004
Posts: 38
Originally posted by Paul Sturrock:
Of course the view would be read only...


No it wouldn't. In Oracle you can:




And it updates the original common_user table.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

That is a view on one table - which you can update. However in SQL there are a whole bunch of restrictions on views if you want to be able to update the data. For example you cannot update a normal view which includes: GROUP functions, a GROUP BY clause, DISTINCT, subquery, DISTINCT, joins accross multiple tables etc.

In Oracle, you can write an updatable materialized view or a writable materialized view if you have configured Oracle to use advanced replication.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: One Mapping for two tables