-->
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.  [ 8 posts ] 
Author Message
 Post subject: Class design, why not have save/update as base class member?
PostPosted: Fri Aug 26, 2005 7:36 pm 
Newbie

Joined: Fri Aug 26, 2005 7:24 pm
Posts: 4
From the simplicity of my question I have no doubt that this question has been answered before, but I have not been able to find the answer searching these forums, and a reason of "why not" is not given in the documentation.

If you have a POJO "Cat", why is it that we have to have a CatDAO class to manage persistence? I know we dont "have to", but is it not possible to create a base class that has "save", "delete", "getById" and some kind of "getList(filter)" functionality? It seems so redundant to have to definte these for each object type I create.

I have not seen this anywhere and I wonder why. Im guessing its one of these two reasons.

There is no clean way of doing this. I notice you have to pass in the class name to session.save, but im sure there are work arounds?

For some reason this is a bad design decision when using hibernate.

Thank you in advance for the help, and believe me when I say I searched through the documentation, the forums and caveatemptor sample project.


Top
 Profile  
 
 Post subject: Forgot to add...
PostPosted: Fri Aug 26, 2005 7:45 pm 
Newbie

Joined: Fri Aug 26, 2005 7:24 pm
Posts: 4
I do know that people might want to keep a POJO a pojo, but to me it seems that I might be willing to have this trade-off although I understand coupling is a no-no.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 26, 2005 9:21 pm 
Senior
Senior

Joined: Wed Jul 13, 2005 4:31 pm
Posts: 142
Location: Seattle, WA
I often use the same pojo used in the data access layer in the web layer...where it doesn't have access to a session. In the web layer save, update, and gets won't work. Defining save, update, and gets in the pojos makes the web layer code confusing.

Besides in most cases, we don't expose individual pojo operations to the web layer. We use functions that do several things. Save order would save each of the items in the order and fire off the fulfillment messages in the same transaction.

In a way, we use hibernate session as our DAO. So, dao per pojo is not neccesary.


Top
 Profile  
 
 Post subject: Re: Class design, why not have save/update as base class mem
PostPosted: Sat Aug 27, 2005 2:32 am 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
rain wrote:
For some reason this is a bad design decision when using hibernate.


Transparent persistence is what seperates hibernate (and a few tiny competitors) from EJBs (which are closer to what you are describing). With a POJO, your business code is more portable, more testable, and still useful outside of whatever framework/container that actually implements the persistence.


Top
 Profile  
 
 Post subject: Thanks!
PostPosted: Wed Aug 31, 2005 12:23 pm 
Newbie

Joined: Fri Aug 26, 2005 7:24 pm
Posts: 4
I see... basically the whole point(or a part of it) is the abstraction.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 31, 2005 12:30 pm 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
See Gavin's interview on serverside.com for more on that.


Top
 Profile  
 
 Post subject: will do...
PostPosted: Wed Aug 31, 2005 12:58 pm 
Newbie

Joined: Fri Aug 26, 2005 7:24 pm
Posts: 4
I'll take a look at that video as soon as I get home(no speakers on work comp), should be interesting.

Quick question for you, I have a db that has about 30 tables, is it normal to define 30 different DA classes, or don't wrap that logic at all?

e.g there seems to be 3 ways I can do this:

1. write all the seperate data access classes so I can do stuff like.

Cat cat = new Cat();
CatDA.save(cat);

pros: clean code
cons: define a class for each business object, ouch!

2. write the code there

Cat cat = new Cat();

//open hibernate session etc.

session.saveOrUpdate(cat);

pros: no special data access class
cons: ugly?! no code re-use

3. Some other mechanism. It seems I would want to define a single class that could handle any object type, but not sure how this would like. This might not cover special conditions like when transactions need to be handled, but I would like to use it 90% of the time.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 31, 2005 5:14 pm 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
I believe #2 is the more popular pattern these days.


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