-->
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.  [ 1 post ] 
Author Message
 Post subject: Hibernate DAO using java 5.0 generics
PostPosted: Thu Aug 04, 2005 9:06 pm 
Beginner
Beginner

Joined: Thu Aug 04, 2005 8:41 pm
Posts: 47
I'm using the following DAO implementation for hibernate, which
I'd like to share on this forum to get other people opinions.

It is using java 5.0 generics and is based on the code found at
http://www.ericburke.com/blog/2004/11/h ... ava-5.html

with the difference that id class is also parameterized.

Basically when creating your own DAO you usually only need to write
finder methods

Here is an example of using this generic DAO class

public class CategoryDAO extends GenericDAO<Category, Long> {
public CategoryDAO() {
super(Category.class, Long.class);
}

public List<Category> getRootCategories() {
return get(Expression.isNull("parentCategory"));
}
}

Please, let know what do you this of this approach.

Thanks a lot


import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;

public abstract class GenericDAO<T, K extends Serializable> {

private Class persistentClass;
private Class idClass;

private static Log log = LogFactory.getLog(GenericDAO.class);

protected Session currentSession() {
return HibernateSessionUtil.currentSession();
}

/**
* Constructor
*
* @param persistentClass
* @param idClass
*/
public GenericDAO(Class persistentClass, Class idClass) {
this.persistentClass = persistentClass;
this.idClass = idClass;
currentSession().beginTransaction();
}

/**
* @return Returns the idClass.
*/
public Class getIdClass() {
return idClass;
}

/**
* @return Returns the persistentClass.
*/
public Class getPersistentClass() {
return persistentClass;
}

/**
* Persist entity in database
*
* @param entity
*/
public void persist(T entity) {
Session session = null;

try {
session = currentSession();
session.persist(entity);
} catch (HibernateException ex) {
log.error(ex.getMessages());
throw new DaoException(ex);
}
}

/**
* Save entity in database
*
* @param entity
*/
public void save(T entity) {
Session session = null;

try {
session = currentSession();
session.save(entity);
} catch (HibernateException ex) {
log.error(ex.getMessages());
throw new DaoException(ex);
}
}

/**
* Save or update entity in database
*
* @param entity
*/
public void saveOrUpdate(T entity) {
Session session = null;

try {
session = currentSession();
session.saveOrUpdate(entity);
} catch (HibernateException ex) {
log.error(ex.getMessages());
throw new DaoException(ex);
}
}

/**
* Delete entity in database
*
* @param entity
*/
public void delete(T entity) {
Session session = null;

try {
session = currentSession();
session.delete(entity);
} catch (HibernateException ex) {
log.error(ex.getMessages());
throw new DaoException(ex);
}
}

/**
* Delete all entities
*/
public void deleteAll() {
Session session = null;

try {
session = currentSession();
session.delete("from " + getPersistentClass().getName() + " x");

} catch (HibernateException ex) {
log.error(ex.getMessages());
throw new DaoException(ex);
}
}

/**
* Find entity by ID
*
* @param id
* @param lock
* @return entity
*/
@SuppressWarnings("unchecked")
public T getById(K id, boolean lock) {
T entity = null;

try {
if (lock) {
entity = (T) currentSession().get(getPersistentClass(), id, LockMode.UPGRADE);
}
else {
entity = (T) currentSession().get(getPersistentClass(), id);
}
} catch (HibernateException ex) {
log.error(ex.getMessages());
throw new DaoException(ex);
}

return entity;
}

/**
* Get all
*
* @return list of entities
*/
@SuppressWarnings("unchecked")
public List<T> getAll() {
List<T> list = null;
Session session = null;
Query query = null;

try {
session = currentSession();
query = session.createQuery("from " + getPersistentClass().getName() + " x");
list = query.list();
} catch (HibernateException ex) {
throw new DaoException(ex);
}

return list;
}

/**
* This is the cool finder method
*
* @param criterion
* @return list of entities
*/
@SuppressWarnings("unchecked")
public List<T> get(Criterion... criterion) {
List<T> list = null;

Criteria criteria = currentSession().createCriteria(getPersistentClass());

try {
for (Criterion c : criterion) {
criteria.add(c);
}

list = new ArrayList<T>(criteria.list());
} catch (HibernateException ex) {
throw new DaoException(ex);
}

return list;
}
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.