-->
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.  [ 36 posts ]  Go to page 1, 2, 3  Next
Author Message
 Post subject: Hibernate, DAOs, Domain model...
PostPosted: Thu Jul 08, 2004 11:58 am 
Newbie

Joined: Fri Jul 02, 2004 11:18 am
Posts: 5
<Beware: long post>

Hello. I'm currently evaluating Hibernate as a replacement of Entity beans in EJB-based applications, and I'm quite pleased with it. I have some architecture questions, though.

Hibernate has the same advantage as entity beans: it offers a way to deal with a persistent domain model. Once a POJO is loaded, Hibernate will do everything needed to load the related POJOs and collections, enabling me to navigate into the domain model object graph. This is only true if the objects are not detached from the session that was used to load them, though.

So, in order for the session beans to be able to use POJOs as automatically-persistent domain model objects, the best thing to do IMO is to associate a session with the current JTA transaction. This can be done by using, for example, the Spring SessionFactoryUtils class.

Everything is great until we add DAOs to the picture.
DAOs, IMO, have two goals: separate the data access logic from the business logic, and be able to switch to another data access technology (plain JDBC, or JDO for example) or database easily.

The first goal is easily reached by moving all the code that deals with the session into DAOs.
The second goal, however is trickier to reach. Indeed, if the session stays open during the whole transaction, a lot of database access code is done implicitely by Hibernate (updates of POJOs, navigation into the object graph, etc.) This means that when I'll switch to another database access technology, I'll have to add lots of data access methods to my DAOs, and to call these new methods from my business logic code.
If, on the contrary, a session is opened and closed in each DAO method, I'll lose performance and ease of development, and I'll lose many of the advantages that Hibernate (or entity beans) offer over JDBC.

What's your way of doing?

My personal choice would be use DAOs but still tie my code to Hibernate: I would let the session opened for the whole JTA transaction, benefit for all the advantages Hibernate offers, and would still reach the first goal of the DAO pattern (and still be able to change the underlying database without modifying my business code). After all, this would lead to a situation that isn't worse than an entity-bean-based one. This solution would also be compatible with a future migration to EJB 3.0.

What would be your choice?

JB.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 09, 2004 11:46 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Quote:
The second goal, however is trickier to reach.

The way I have typically seen this done is that DAOs are actually interfaces and application code retreives a DAO from a factory. The factory is reposible for determining whether to retrun a Hibernate-based DAO implementation or, say, a JDBC-based impl.

Isn't that the whole point behind DAOs? Saving an entity is the same from the application perspective whether using JDBC or some O/R solution. The DAO hides all those details, and provides a simplified facade to its clients.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 10, 2004 6:46 am 
Newbie

Joined: Fri Jul 02, 2004 11:18 am
Posts: 5
I agree with your points, and this would be true if Hibernate DAO methods used their own session and returned detached objects. But if the Hibernate session is tied to the transaction (by using, for example, the Spring SessionFactoryUtils), the objects returned by DAOs are attached objects, similar to entity beans. This means that all the changes I could make on the returned objects will be persisted transparently in the database (which would not be the case with JDBC-based DAOs). This also means that navigating into the object graph will work with Hibernate-based DAOs, because Hibernate will load the associated objects lazily, which will not happen with JDBC-based objects.
So, there are two solutions:
- make Hibernate-based DAOs that return detached objects, as would JDBC-based DAOs. This makes me lose performance, ease of development, but allows me to migrate to any other DAO implementation
- make Hibernate-based DAOs that return attached objects, similar to entity beans, which allow me to use Hibernate to its full power and makes development much easier, but doesn't allow me to easily switch to another DAO implementation.

To make it clearer, the second solution allows me to do this to associate a new group to a user:

Code:
User user = userDAO.load(userId);
Group group = groupDAO.load(groupId);
user.getGroups().add(group);


whereas with the first solution, I would have to do:

Code:
User user = userDAO.load(userId);
Group group = groupDAO.load(groupId);
Set userGroups = groupDAO.getGroupsForUser(user);
if (!userGroups.contains(group)) {
  userDAO.addGroupToUser(user, group);
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 14, 2004 1:20 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
In general, I guess, it really depends on whether you are saying you want these DAOs to be swappable "in mass" or that you are looking for co-existence of different DAO impls.

Personally, I've never found the argument of needing the ability to change the DAO access mechanism in a wholesale manner throughtout an entire app to be convincing. Sure it makes sense to try to mitigate as much of the pain of that possible future migration, but to think you're just going to be able to "drop in" the new access mechanism and flip a switch and have it all work is a bit naive to me; it'd be a bit like abstracting web tier logic to be able to seemlessly migrate from Struts to JSF...

Now the need to have some entity's access handles through a JDBC-based DAO impl while the rest of the system deals with Hibernate-based DAOs is another story.

Quote:
This also means that navigating into the object graph will work with Hibernate-based DAOs, because Hibernate will load the associated objects lazily, which will not happen with JDBC-based objects.

Who says that can't happen with objects returned from a JDBC-based DAO? Proxies and interception are beautiful things ;)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 14, 2004 6:56 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
As I understand it depends on your prefered modeling concept. O/R is usefull if you do not like ER for some reason and want to duplicate it in OO model or you model is OO, but you do not like OO database for some reason.
If you prefer to model system as set of abstract services ( DAO is one of them) then OO or ER are not important for your model (it becomes implementation) and you will lose all cool O/R features if you are going to use DAO and DTO.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 14, 2004 8:57 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
hi guys,
baliukas, what do you mean by
Quote:
you will lose all cool O/R features if you are going to use DAO and DTO
?

we are using hibernate and dao (with a factories) without losing any piece of hibernate power.
For me using DAO with having in mind "i'll be able to switch to JDO or JDBC" is funny. In theory it is always possible but for me, the goal, is simply to isolate hibernate code AND JDBC into a specific layer. Don't forget that for massive operation (in app context), most of us, are coding it in JDBC.
It allows me to dispatch advanced hibernate developpers to code this layer. It also allows to find easily queries, complex cascade operations.
For example when we switched from hibernate 1.2 to hibernate 2.1, "fetch" keyword appeared, it was very easy to find which queries to update.

And DTO... this is another story, after reading all your args ( during one year) , i admit DTO seems evil but many in my society aren't ok with that ;(

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


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 14, 2004 9:48 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
Probably I do not understand something, but I see no meaning for DAO + DTO and OO model. If I do not have this model then I have nothing to map ( it will be O/R mapping tool overuse if will try to find something to map and to manage). It means I do not any kind of local cache too.
I can understand it, people love OOP hate procedures, but It is a very big optimization to drop object model, it is too expensive to maintain and to persist it for me.
Hibernate has a "good" way to read unmanaged objects "select new ...", but as I understand there is no way to update/delete/insert. If O/R tool can work with "complex" object graphs then why it can not work with values like String,Integer or struct of values ? I do not need something more, but all "enterprise" frameworks including hibernate makes me to use fat objects for some reason. Everything is object so struct is an object too, is not it ? Probably I am some kind of procedure man and I do not undesrtand OOP, but I can not agree with this populism more.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 14, 2004 10:56 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
i understand what you mean, in fact the problem is that making a domain model "reusable" is expensive, and in fact, few people really think of it. And if a domain model isn't reusable, i agree that it is too expensive lol

Most of hibernate users use it as a persistence framework, not really a way to create a rich domain model which contains business logic (different as an "anemic" model).
The anemic model is a poor object graph very limited, an anti pattern we are used to see thanks to ejb.

but i'm not sure to understand what you say lol. Maybe we are saying the same thing ;)

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


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 14, 2004 3:21 pm 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
Sorry, I am very bad writer and probably this is my problem I can not sell my ideas.
I will try to explain my typical "enterprise" application.
It is frontend with web UI, it talks with backend systems ( money transfers, stock exange orders, funds, ... ) I use JMS if I can or some file exange, binary sockets.

Backends are not prepared for web and any change can break "mission-critical" system, so frontend is very "clever", it uses database too.

My model is very simple and there is no "complex" object graphs, but most of critical code is about data access and I can not dublicate it, it must scale and I can not use http session local caches to "manage" objects.
The same data access command can be executed by JMS handler, HTML form handler or timer. Most of reusable commands are updates, inserts and deletes.

It is very expensive for me to have object model, manage object life cycle and relationships, I have a few GB of memory, but it is very important resource for me.
DAO and DTO are usefull for report style UI , but it is a very good way for transactions, I reuse the same objects too.
I do not need to load objects to update or delete, put loaded objects to local cache (I use global cache fo some data too).
I do not think hibernate or any O/R mapping framework is good for my use cases, Hibernate has single usefull feature for me "select new ..." aka projection, code line count or "easy to use" are not important for me.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 14, 2004 5:02 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
ok ok, all is clear about your use cases.
I can easily understand what you mean now and perfectly agree.
The most important to have in mind is "there are n kinds of applications and n kinds of technologies" there will never be an "ultimate pattern linked to an ultimate tool or technology".
Great to share these ideas with you!

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


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 21, 2004 2:51 am 
Newbie

Joined: Fri Jul 02, 2004 11:18 am
Posts: 5
Hi Steve.

Sorry for the late reply: I was on holiday.

Quote:
Personally, I've never found the argument of needing the ability to change the DAO access mechanism in a wholesale manner throughtout an entire app to be convincing


That's in fact also what I think. And, indeed, as Anthony says:

Quote:
For me using DAO with having in mind "i'll be able to switch to JDO or JDBC" is funny. In theory it is always possible but for me, the goal, is simply to isolate hibernate code AND JDBC into a specific layer. Don't forget that for massive operation (in app context), most of us, are coding it in JDBC.


So I think my choice is the right one: use DAOs to isolate data access logic, queries and so on, and use Hibernate to its full power. Switching to JDO or JDBC will probably never happen, and if it happens, it'll need so much work that changing some methods in the business layer will probably be negligible anyway.

Thanks for all your opinions on this matter.

JB.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 28, 2004 10:19 pm 
Newbie

Joined: Wed Jun 23, 2004 8:51 pm
Posts: 16
Hi All,

I'm currently evaulating whether to use hibernate, so my question may be a bit naive.

I think if you use dao, then you can't have a domain object model represented as java classes (OO design) . i.e. your design is procedural.

And if Hibernate is an OR mapper, then you need an object model (I assume that means an OO one) to map to your relational database.

Therefore you can't have DAO and OR mapper.

So if you're not using Hibernate as an OR mapper, then why use it?

Where's the flaw in this logic?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 29, 2004 5:58 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Quote:
Therefore you can't have DAO and OR mapper.


Why? A DAO and a full ORM tool give you a nice persistence layer API, and fully automated and transparent persistence. Thats great if you are working with an object-oriented domain model.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 29, 2004 5:50 pm 
Newbie

Joined: Wed Jun 23, 2004 8:51 pm
Posts: 16
I'm thinking it would look like

Util class -> DAO -> Hibernate

All your busines logic would need to be in your util class in static methods giving you a procedural design.
Hibernate would not use it's association capabilities becuase this would be implemented in the DAO methods in a procedural way.

I can't think how you could use DAO in an OO domain model?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 29, 2004 5:51 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Ehm, sure you can. Either search the forum for "DAO", there are many threads discussing different patterns, or read Hibernate in Action.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 36 posts ]  Go to page 1, 2, 3  Next

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.