-->
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.  [ 2 posts ] 
Author Message
 Post subject: Best desing choice for derived properties
PostPosted: Wed Jul 11, 2007 9:10 am 
Newbie

Joined: Fri Mar 11, 2005 4:38 am
Posts: 9
Location: Ukraine, Kiev
I've got a question on quite common practical problem - how to best represent derived fields?

Suppose I have a domain object Book which has one-to-many association to Rating object which represents 5-star rating value for the Book given by clients (much like in Amazon). I'd like to show Book info to clients along with associated average rating value. And here I have few options which I'd like to discuss.

1. Getting average rating for a particular book from DAO and storing it in presentation-only bean.
Book class doesn't have dedicated field for average rating and there is a method getAverageRating(Book) in BookDAO class which returns average rating for Book as result of HQL execution.
I don't like this approach as I have to introduce additional bean class to hold book along with its average rating to be used in presentation logic. Besides I feel like I'm violating rule that all data should be where it's processed.

2. Getting average rating for a particular book from DAO inside Book domain object.
As in previous option I have dedicated method in DAO and read only property (getter method) in Book class which delegates work to DAO.
With this option I'm getting rid of additional presentation-only bean but introduce dependency in domain object on DAO layer which I can't consider to be a best option (think of testing problems).

3. Injecting average rating to Book while getting it from DAO.
Method findBook() in DAO could return Book instance filled with average rating using setter method on bean.
I think it's quite viable option but maybe you have some thoughts on why it could be not.

4. Using derived property configured in mapping.
I can use "formula" element in mapping configuration for Book class to represent average rating property.
This is an option I'd rather use but as in previous case there could be nuances I don't know about.

So, dear Hibernate gurus, could you help me resolve my doubts?

P.S. I'd very appreciate any links on common Hibernate design patterns as I'm quite new with it.

Thank you, Oleg.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 11, 2007 5:42 pm 
Newbie

Joined: Thu Nov 09, 2006 9:42 pm
Posts: 13
I'd recommend a hybrid of your last two approaches. Add an averageRating attribute to the Book class, mark it as transient and use something like this to lookup a Book instance with JPQL (I'm sure HQL supports something similar):

Code:
SELECT new Book( b, AVG( r)) FROM Book b IN( b.ratings) r


With a constuctor on Book to copy the persistent properties out.

This doesn't pollute your code with a bunch of helper objects and methods that only really care about the average rating, only requiring Book to initialize itself from another Book instance.

--
Tim


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