-->
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: use HibernateUtil.getSessionFactory().getCurrentSession()
PostPosted: Wed Oct 27, 2010 7:05 am 
Newbie

Joined: Tue May 25, 2010 5:51 pm
Posts: 8
hi all, my DAO's are very similar to this
Code:
public abstract class GenericHibernateDAO<T, ID extends Serializable>
        implements GenericDAO<T, ID> {

    private Class<T> persistentClass;
    private Session session;

    public GenericHibernateDAO() {
        this.persistentClass = (Class<T>) ((ParameterizedType) getClass()
                                .getGenericSuperclass()).getActualTypeArguments()[0];
     }

    @SuppressWarnings("unchecked")
    public void setSession(Session s) {
        this.session = s;
    }

    protected Session getSession() {
        if (session == null)
            throw new IllegalStateException("Session has not been set on DAO before usage");
        return session;
    }

    public Class<T> getPersistentClass() {
        return persistentClass;
    }

    @SuppressWarnings("unchecked")
    public T findById(ID id, boolean lock) {
        T entity;
        if (lock)
            entity = (T) getSession().load(getPersistentClass(), id, LockMode.UPGRADE);
        else
            entity = (T) getSession().load(getPersistentClass(), id);

        return entity;
    }

    @SuppressWarnings("unchecked")
    public List<T> findAll() {
        return findByCriteria();
    }

    @SuppressWarnings("unchecked")
    public List<T> findByExample(T exampleInstance, String[] excludeProperty) {
        Criteria crit = getSession().createCriteria(getPersistentClass());
        Example example =  Example.create(exampleInstance);
        for (String exclude : excludeProperty) {
            example.excludeProperty(exclude);
        }
        crit.add(example);
        return crit.list();
    }

    @SuppressWarnings("unchecked")
    public T makePersistent(T entity) {
        getSession().saveOrUpdate(entity);
        return entity;
    }

    public void makeTransient(T entity) {
        getSession().delete(entity);
    }

    public void flush() {
        getSession().flush();
    }

    public void clear() {
        getSession().clear();
    }

    /**
     * Use this inside subclasses as a convenience method.
     */
    @SuppressWarnings("unchecked")
    protected List<T> findByCriteria(Criterion... criterion) {
        Criteria crit = getSession().createCriteria(getPersistentClass());
        for (Criterion c : criterion) {
            crit.add(c);
        }
        return crit.list();
   }

}



but to obtain a session i do this:
Code:
   public Session getSession() {
      return HibernateUtil.getSessionFactory().getCurrentSession();
   }

Is this way correct or could cause some problems?

Anyway, to use my DAO logic im proced like this:

Code:
   private void addNewUser(string username){
      Transaction tx=HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction();
      User user=new User(username);
      //into this method i obtain session with getCurrentSession...so i think session into DAO and session that begin a transaction is the same until a commit, isn't true?
      userHibernateDAO.save(user);
      tx.commit();
   }


So, my question is: can i proced in this way or is a totally wrong implementation?

Thanks all.


Top
 Profile  
 
 Post subject: Re: use HibernateUtil.getSessionFactory().getCurrentSession()
PostPosted: Wed Oct 27, 2010 8:05 am 
Senior
Senior

Joined: Fri Oct 08, 2010 8:44 am
Posts: 130
1. You can proceed like this. It is a bad style and you may have the problems in some cases, but you can change your implementation any time.

2. Writing such generic DAO as you did is a bad style and a waste of code. Just use Hibernate session directly in your DAOs.


Top
 Profile  
 
 Post subject: Re: use HibernateUtil.getSessionFactory().getCurrentSession()
PostPosted: Wed Oct 27, 2010 12:36 pm 
Newbie

Joined: Tue May 25, 2010 5:51 pm
Posts: 8
r wrote:
1. You can proceed like this. It is a bad style and you may have the problems in some cases, but you can change your implementation any time.

2. Writing such generic DAO as you did is a bad style and a waste of code. Just use Hibernate session directly in your DAOs.

Hi, what you mean at the point 2?
Can you show me a real example?

Thank you.


Top
 Profile  
 
 Post subject: Re: use HibernateUtil.getSessionFactory().getCurrentSession()
PostPosted: Thu Oct 28, 2010 10:15 am 
Senior
Senior

Joined: Fri Oct 08, 2010 8:44 am
Posts: 130
Yes.

Instead of using the following:

Code:
makePersistent(entity);


You can just use the following in your custom DAOs:

Code:
getSession().saveOrUpdate(entity);


No wrapper methods that do nothing are needed then.


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.