-->
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: Design issues - lazy/eager mapping
PostPosted: Mon Feb 18, 2008 2:08 pm 
Newbie

Joined: Sun Apr 29, 2007 4:27 am
Posts: 10
Location: Israel
Hey,

Let’s say I have an ‘Hotel’ with one to one relation to ‘Room’ and ‘Room’ has one to one relation to ‘RoomDetails’.

When I ask for hotel - the roomDetails are useless, so the relation between room to roomDetails should be lazy.
Yet, when I ask for room, the roomDetails are needed, so the relation should be eager.

This example is just a simple one - in my application I have lot of references that refer to more references and etc.
I’ve examined the query hibernate execute when it tries to load object AAA and found that it has 31 left outer joins, most of them are irrelevant when loading AAA , yet I cant map them as lazy since they’re needed when loading BBB or CCC that are referenced from AAAA.

Any advice will be welcome


Top
 Profile  
 
 Post subject: Re: Design issues - lazy/eager mapping
PostPosted: Mon Feb 18, 2008 2:16 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
You could use HQL or Criteria objects to fetch a network of objects. With either one, you have the option to force loading lazy relations or leave it for a later time. This also applies to eagerly fetched relations.




Fatzad-


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 20, 2008 9:36 am 
Newbie

Joined: Sun Apr 29, 2007 4:27 am
Posts: 10
Location: Israel
I know about the fetch strategy.
My facade is generic and my object graph is not as simple like as in the example :)
so i don’t have special queries i just run entitymanager.find
I don’t want to write for each entity in my application special loading.


Top
 Profile  
 
 Post subject: Re: Design issues - lazy/eager mapping
PostPosted: Wed Feb 20, 2008 12:41 pm 
Newbie

Joined: Wed Feb 20, 2008 12:16 pm
Posts: 1
Hi,

I have another alternative which is worth exploring.

Taking your example where you have a Hotel which contains Rooms which i turn contains RoomDetails. When you retrieve Hotels you want them to include Rooms but not Room Details. When you retrieve Rooms you want them to include RoomDetails.

As you are aware, you cannot specify this with the fetch attribute for each relationship.

What I do in this case is create a RefRoom, or RoomSummary entity which maps to the same table as Room but only includes the information required when I am retrieving Hotels. i.e. in this case it would not map the RoomDetails relationship, and in practice may not include a number of other attributes of the Room entity.

In this way you are then free to specify EAGER fetch for your relationships without worrying about it cascading out of control. It also means that you can use Session.get() to retrieve Hotel by its primary key without having to create HQL or queries with explicit fetches each time.

Hope this helps.

Regards,
Colin E.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 31, 2008 9:07 am 
Newbie

Joined: Wed May 09, 2007 7:06 am
Posts: 12
Hi ColinEberhardt ,

I have exactly the same problem.

Can you give me an example how to implement this:
Code:
What I do in this case is create a RefRoom, or RoomSummary entity which maps to the same table as Room but only includes the information required when I am retrieving Hotels


I hava the following
Code:
@Entity
@Name("shortQueryResult")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="QUERYTYPE", discriminatorType=DiscriminatorType.STRING)
public class ShortQueryResult extends StandardEntity implements Serializable, Comparable<ShortQueryResult> {
   
   
   protected BaseCategory query;
   public void setQuery(BaseCategory query) {
      this.anfrage = query;
   }


and
Code:
@Entity(name="ShortQueryResultRef")
@Name("shortQueryResultRef")
public class ShortQueryResultRef extends ShortQueryResult {

   @OneToOne(cascade={CascadeType.REMOVE, CascadeType.ALL}, fetch=FetchType.EAGER)
   public BaseCategory getQuery() {
      return query;
   }
   
}


In my case I don't need the field query at all times. When I leave the field complete in ShortQueryResult class with no Inheritance and LAZY loading some fields in query are not availeble.

I hope you can give me an example.
Thanks
Andi

_________________
Feivel Frey


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.