File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

One Mapping for two tables

 
Krishna Radha
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using middlegen does complicate this... but you could always make a view that combimes the two tables.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Krishna Radha
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic