• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

One Mapping for two tables

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using middlegen does complicate this... but you could always make a view that combimes the two tables.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic