same erasure error!

54 views
Skip to first unread message

rsedw...@gmail.com

unread,
May 21, 2013, 9:45:07 AM5/21/13
to java-gen...@googlegroups.com
Hi,

I am trying to use the genericDAO package. Getting started I followed the steps on wiki, however I'm getting the same erasure error for GenericDAO and GenericDAOImpl when I create an DAO implementation of my entity.

public class BusinessEntityDAOImpl extends GenericDAOImpl<BusinessEntity, Long> implements BusinessEntityDAO   // getting an error here

my BusinessEntityDAO is

public interface BusinessEntityDAO extends GenericDAO<BusinessEntity, Long>{

}


--
Thanks

rsedw...@gmail.com

unread,
May 22, 2013, 8:26:00 AM5/22/13
to java-gen...@googlegroups.com, rsedw...@gmail.com
So, I got the above error fixed. However, I've stumbled on something very important. HibernateBaseDao uses getCurrentSession() to get a session from the SessionFactory, however it gives me an error whenever I try to execute any query.

      org.hibernate.HibernateException: createCriteria is not valid without active transaction

However, when I use openSession() in place of getCurrentSession() it works like a charm. I'm not using spring as a dependency in pom.xml. I've been reading on the openSession() and getCurrentSession(), however still cannot understand why this is happening?

David Wolverton

unread,
May 22, 2013, 10:02:00 PM5/22/13
to java-gen...@googlegroups.com
Yeah. With GenericDAO you need to explicitly open a transaction before and close it after calling DAO methods. Generally transactions are bound to sessions. The DAO uses the current active session. (You can make as many DAO calls as you want within a transaction/session.) One standard practice is to have a service layer in your application that controls the transactions and calls the DAO methods.

GenericDAO makes the assumption that you will be handling transactions externally to the DAO. But there are a few customization points if this is not the behavior you desire. (1) You can override getSession() (2) you can override all the methods and add transaction handling code before and after calling through to GenericDAOImpl (3) you can use some kind of Aspect Oriented Programming library to automatically open and close transactions around all your DAO methods.

Now let me go back and give one very basic example of how you might handle transactions externally.

     Session session = null;
    Transaction tx = null;
 
    try{
    session = sessionFactory.openSession(); // This sessionFactory is the same you set on hamburgerDao.setSessionFactory()
    tx = session.beginTransaction();
    tx.setTimeout(5);
 
    Hamburger hamburger = hamburgerDao.search(123);
     hamburger.weight = .25;
 
    tx.commit();
 
 
    }catch(RuntimeException e){
    try{
    tx.rollback();
    }catch(RuntimeException rbe){
    log.error("Couldn’t roll back transaction", rbe);
    }
    throw e;
    }finally{
    if(session!=null){
    session.close();
    }
    }

Hope this helps.

- David W.


--
You received this message because you are subscribed to the Google Groups "java-generic-dao" group.
To unsubscribe from this group and stop receiving emails from it, send an email to java-generic-d...@googlegroups.com.
To post to this group, send email to java-gen...@googlegroups.com.
Visit this group at http://groups.google.com/group/java-generic-dao?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages