-->
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.  [ 4 posts ] 
Author Message
 Post subject: object vs. id
PostPosted: Fri Jun 02, 2006 6:08 am 
Newbie

Joined: Thu May 18, 2006 11:30 pm
Posts: 9
As an example, suppose we are dealing with a simple problem of persons' favorite books. So I have a Person table, a Book table and a Favorite table containing the (PersonId, BookId) pairs.

To manage the favorite, i.e., add a book to a person's favorite, remove a book from a person's favorite etc., I can see two solutions:

1. Have two classes Person and Book, and add a favorites collection attributes to the Person class, so the add/remove will be operations on the collection.
The part that I don't like about this approach is that now we need to create the full Book objects for the add/remove, although they only need to work against the Favorite table where ids are enough. This could be a performance hit if the Book class is complicated and involves other tables etc.

2. Have three classes Person, Book and Favorite, so that the add/remove can work with Favorite directly.
The problem with this is that the Favorite table will generate a composite-id containing the id pairs. To support the operations such as getting the list of ids of a person's favorite books, it seems native SQL queries are needed, unless a surrogate key is added to the Favorite table, which I really don't like.

Did I miss anything in the above solutions? Are there any better solutions to the problem?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 02, 2006 6:53 am 
Regular
Regular

Joined: Thu Sep 22, 2005 1:53 pm
Posts: 88
Location: Rio de Janeiro
Hi,

In option 2 for listing a person´s favorite books no native SQL is needed everything can be done with HQL and Criterias for Example

Code:

   From Person p
       join fetch p.favorits f
       Where p = :personObject



Hope this helps....

_________________
Don´t forget to rate!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 02, 2006 7:00 am 
Regular
Regular

Joined: Mon May 08, 2006 6:00 am
Posts: 53
Location: India
Hi,

Your relation within tables sounds like Many-To-Many to me. Like, many person will have many favourate books. Going by this relation, you need to have a link(intermediate) intermediate table, which in your case is Favourate.

Cosidering this your solution 1 will not be applicable and solution 2 is the best choice, which you can implement using association as well as HQL.

Sudhir


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 12, 2006 12:45 pm 
Newbie

Joined: Thu May 18, 2006 11:30 pm
Posts: 9
I guess I'm still trying to resolve in my head the difference between objects and db entity/relations.

Think about an object called process that has an attribute called status. In the object world, you'll have Process which contains a property of type Status that you can get/set. In the relational world, you'll have a process table, with a FK reference (status_id) to a status table. Now I want to set the status of the Process object to a particular status corresponds to a status_id value. It seems to me that it will have to first load the Status object using the status_id, and then set the Process object's status attribute to this Status object. This would seem to require both a load from db (to get the full Status object) and an update to save the new status, while what's really needed for the underlying db is a simple update of the FK reference column.

Is this performance penalty expected, or am I missing something here?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.