-->
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.  [ 7 posts ] 
Author Message
 Post subject: Query / vs model best practice
PostPosted: Wed Oct 06, 2004 4:34 am 
Beginner
Beginner

Joined: Tue Oct 05, 2004 5:15 am
Posts: 23
In my model, lets say I've got an Order which can contain multiple OrderItems.
Further, none of my use cases currently involve the need to retrieve the detail of OrderItems (I.e, there's no direct need to have a getOrderItems on Order).
Then, suppose I add some business logic: Lets say, I cant "confirm" and Order unless I have more than two OrderItems.

Im wondering what the best approach is here. The first (and more obvious) way would be to model an Order as having OrderItems, and add the business logic to Order (it just checks for the number of OrderItems which have been restored using Hibernate).
The other way is to not map this relationship at all, and add a new query to, say, my DAO: "countOrderItemsFor(Order)". This second approach would see my business processes consulting the DAO to ask questions about the model... Hmmm.

The more I think about this, the more the first way seems the better (and more OO?) approach, but could mean more overhead (i.e, we could have several thousand OrderItems for an order).

Does any one have any opinions on this?

Many thanks,

Dave


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 06, 2004 10:05 am 
Senior
Senior

Joined: Sun Jan 04, 2004 2:46 pm
Posts: 147
Probably the best way to get the number of order items is to do...

Code:
select count( elements(order.OrderItems) ) from Order order


rather than doing a getOrderItems().size() as this will load ALL the order items which as you say may be quite a few. So maybe the second solution doing something like the above might be better for your particular use case?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 06, 2004 10:58 am 
Beginner
Beginner

Joined: Tue Oct 05, 2004 5:15 am
Posts: 23
Thanks for the reply,

Yeah, I was thinking that maybe a count query would be better.
The only thing that puts me off about it is that I feel like the business rule belongs in the model - and I dont really want my model objects using Hibernate (or a DAO) directly.

However, Im wondering whether lazy loading would be an option....
Does anyone know whether using lazy loading I can request a collections size without triggering a full load? Im guessing this isn't the case - but it would be very cool if it were possible.


Top
 Profile  
 
 Post subject: Re: Query / vs model best practice
PostPosted: Wed Oct 06, 2004 11:24 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
irvingd wrote:

The more I think about this, the more the first way seems the better (and more OO?) approach, but could mean more overhead (i.e, we could have several thousand OrderItems for an order).



Things become very trivial if you will drop statement "more OO is better".


Top
 Profile  
 
 Post subject: Re: Query / vs model best practice
PostPosted: Wed Oct 06, 2004 12:02 pm 
Beginner
Beginner

Joined: Tue Oct 05, 2004 5:15 am
Posts: 23
baliukas wrote:
Things become very trivial if you will drop statement "more OO is better".


Well, I'd like to put my business logic in my model - or else I end up just using Hibernate as an old style DAO, shovelling mindless DTOs around, with all my business logic in "services".
So ideally, I'd like to ask my order "areYouValid".
This could be done efficiently in the model (with a size query) if the model contacted Hibernate (/DAO) directly, but there is a large amount of evidence against using that as an approach on this forum.

So I guess Im just confused as what the best practise is in this sort of case


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 06, 2004 12:04 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
http://forum.hibernate.org/viewtopic.ph ... 76#2217876

Christian explain it

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 07, 2004 4:21 am 
Beginner
Beginner

Joined: Tue Oct 05, 2004 5:15 am
Posts: 23
Thanks Anthony, that was a useful link.
Im going to continue the discusson on that thread as I think it better sums up the problem (with a good blog link too).

Cheers

Dave


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