-->
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.  [ 4 posts ] 
Author Message
 Post subject: Base class for hibernate DAO class
PostPosted: Fri Oct 21, 2005 10:56 pm 
Newbie

Joined: Fri Oct 21, 2005 10:38 pm
Posts: 2
Hi All,

In one my projects we had lot of DAO classes. It was tedious to code hibernate codes all the time into every one of these classes. So I ended
up writing a generic base class. This class had methods like :

-- saveObject(Object obj) throws SomeException

-- Object getObject(Class jClass, Serializable primKey) throws SomeException

-- Object getObjectByHql(String hqlStr) throws SomeException

-- List getObjectListByHql(String hqlStr, Map valMap) throws SomeException

-- Object getObjectByHql(String hqlStr, Map valMap) throws SomeException
, etc.

While saveObject and getObject methods are trivial, the getObjectListByHql and getObjectByHql methods are not. And these
saved a tons of lines of code. If you have hundred DAO classes, it will
easily save you thousand lines of code. The DAO sub-classes will call
these methods in stead of writing codes like :

Query query = session.createQuery(hqlStr);
query.setString(key, value);
query.setInteger(key, value);

etc.

The getObjectListByHql methods can be further abstracted by defining
methods like :

-- getObjectListByHql (String hqlStr, String key, Object value)

-- getObjectListByHql (String hqlStr, String key1, Object value1, String key2, Object value2)

etc.

Has anyone done this? Was there any issue with that?

Thanks,

Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:[3.0.5]

[b]Mapping documents:



Full stack trace of any exception that occurs:

Name and version of the database you are using:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:

_________________
--Gautam
http://www.stock-article.com


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 21, 2005 11:20 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
http://www.hibernate.org/328.html


Top
 Profile  
 
 Post subject: Generic DAO
PostPosted: Sun Oct 23, 2005 1:53 am 
Newbie

Joined: Fri Oct 21, 2005 10:38 pm
Posts: 2
This link was useful but it only work for findById, findByExample etc.
It will not help with the getByHQL methods which we had lot of.
I am not doing anything fancy here, but it worked for arbitrary hqls. The code
looked much simpler and leaner.

_________________
--Gautam
http://www.stock-article.com


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 23, 2005 5:47 pm 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
you may want to consider holding off on HQL unless it is a really complex query. querying by Class is good because changes in the domain model are caught during IDE refactors/and mistakes are caught at compile time.

in other words, when User is changed to Client, you are less likely to catch that w/ "from User" .


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