File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes Hibernate Design Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate Design" Watch "Hibernate Design" New topic

Hibernate Design

Padma Prasad
Ranch Hand

Joined: Sep 16, 2002
Posts: 76
I am into trouble.
In my application, I have to show information from multiple tables into a table format. I make a initial query and get some results. The number of rows fetched could be in hundreds and each row fetched contain some id's. now, I have to pick id of each column and from different other table, pull the name of this id. i.e., if I have a row with 7 columns, each column contain a id and the id name has to be fetched from a different table. If i have 7 columns, I have to get the names from 7 tables. This I have to do for all the rows. finally I have to show all the names in a single table.
I saw some where that without writing a query, the data is fetched. like calling the name function on the object for that id. is this possible? or everytime I need to make a query and call the function on the object?
I cannot imagine calling DB multiple times (infact if i have 7 columns and 100 rows fetched, I have to make 700 calls?)
Help me........
eammon bannon
Ranch Hand

Joined: Mar 16, 2004
Posts: 140
You want outer join fetching. This is a process whereby the relationships from a row can be recovered as a graph of objects from one single sql query. In hibernate, you can configure outer join fething globally, plus the depth of outer join fetching, with "hibernate.use_outer_join=true" and "hibernate.max_fetch_depth=<whatever>" in It is in fact recommended by the Hibernate people to do this.
(Please note: not all DBs implement outer join fetching - so best check your docs before trying to use it)
Ariel Araza

Joined: Apr 15, 2004
Posts: 4
Hi Padma,
Are you using collections in your mappings? If you use one-to-many relationships in a mapping file (I'm assuminng you use one-table-one-mapping-file approach), you'll get a class which has among it's properties, a Set of objects which represents the matching records in the related table. From that object graph you can access all properties of the other table/s,
Padma Prasad
Ranch Hand

Joined: Sep 16, 2002
Posts: 76
Hi Ariel,
Yeah. You are right. I got to know this later.
Thanks for the help.
Frederico Melo

Joined: Mar 15, 2004
Posts: 24

It's not the best design you can provide, but if it's still not good on performance, you can put your query on a Database View and map your hibernate class containing the complete data to this View. The problem is that you put some code on database, creating a high coupling between database and system, but the gains in performance can compensate it.


I agree. Here's the link:
subject: Hibernate Design
It's not a secret anymore!