-->
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.  [ 9 posts ] 
Author Message
 Post subject: newb question on how to use query inside domain object
PostPosted: Thu Sep 20, 2007 10:44 pm 
Newbie

Joined: Sat Jun 09, 2007 10:54 am
Posts: 8
I have my business/domain objects, lets say Customer. I have a CustomerDAO interface and an implementation of it. Now inside my Category object I want a method called 'getOrders' which executes a Hibernate Query to retreive certain 'Order' objects. I don't want to use a mapped field for this, I want to execute a Query each time, as the orders returned depend on many parameters.

My question is, what is the best way to design this? I need access to the session to do a query, or I need to pass in an OrderDao object to the Customer object and put the method in there. Is that the best thing to do? I thought it was best to keep domain unaware of the persistance layer.

Any advice gratefully received.


Top
 Profile  
 
 Post subject: Re: newb question on how to use query inside domain object
PostPosted: Fri Sep 21, 2007 4:10 am 
Newbie

Joined: Fri Sep 21, 2007 3:32 am
Posts: 6
Hi
The requirement is to have a getOrders() method, that retrieve the orders. And the getOrders() depends somehow on the Customer object. The correct way of working is to create a
Code:
getOrders(Customer customer, AnyOtherParam param)
in the OrderDao object.

Regards.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 21, 2007 7:06 am 
Newbie

Joined: Sat Jun 09, 2007 10:54 am
Posts: 8
But if I have a customer object and I want to execute a method on the customer object, that requires use of this fictional getOrders method then where does the orderDao come from. You are making the customer object aware of the OrderDao. Does that make sense ?

Ie,

lets say I want to do something like

Customer customer = .....
customer.cancelOutstandingOrders();

....

public void cancelOutstandingOrders() {

// get all orders, iterate through cancelling outstanding ones
for (Order order : getOrders()) {

....
}

}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 21, 2007 8:43 am 
Newbie

Joined: Fri Sep 21, 2007 3:32 am
Posts: 6
The point is 'what is the nature of Customer ?'.
Customer is a Domain object (or at least sounds like). In a distributed environment, Customer will be serialized and used in another JVM (or in case of WS only Customer data will be used in a .net program).
That's why it's usually not a good idea to add behavior on the domain object, and to let it in a business class (service).

Code would look like:
Code:
CustomerService custService = ... // usually retrieved through Spring or JNDI
Customer cust =  custService.find("John Smith");
custService.cancelOutstandingOrders(cust);


In this case CustomerService uses
* CustomerDao in method find(...),
* and OrderDao to process cancelOutstandingOrders(...)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 21, 2007 10:05 am 
Newbie

Joined: Sat Jun 09, 2007 10:54 am
Posts: 8
I see, so the CustomerService object is aware of the various DAOs and of the various domain objects ?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 21, 2007 10:36 am 
Newbie

Joined: Fri Sep 21, 2007 3:32 am
Posts: 6
sksamuel wrote:
I see, so the CustomerService object is aware of the various DAOs and of the various domain objects ?

Yep.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 21, 2007 12:34 pm 
Newbie

Joined: Sat Jun 09, 2007 10:54 am
Posts: 8
Thanks for your help.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 21, 2007 1:20 pm 
Newbie

Joined: Sat Jun 09, 2007 10:54 am
Posts: 8
Is this a popular or common way of doing things ?
I found this blog arguing against it (and to be honest I seem to agree with this blogger!).
http://martinfowler.com/bliki/AnemicDomainModel.html

Please convince me otherwise!


Top
 Profile  
 
 Post subject: Use 0 as null value for assocation
PostPosted: Sat Sep 29, 2007 12:48 am 
Newbie

Joined: Sat Jun 09, 2007 10:54 am
Posts: 8
deleted


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