0

I'm trying to update the libraries of my project (from Hibernate 3.2.1 GA to Hibernate 4.2.8)

This (complex) application use LAZY loading and get the object later only when we need it. -->it seems to work differently now because I get some org.hibernate.LazyInitializationException: could not initialize proxy - no Session

@Entity
@Table(name = "CLIENTS")
public class Clients  {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "INFOIDT", insertable = true, updatable = false)
private Information info;
//...
}

and when I need to know more about the product before using it :

cli.getInfo();

Note that in my persistence.xml I also have the property hibernate.cache.provider_class set to org.hibernate.cache.EhCacheProvider for a second level cache.

QUESTION : what is the simple way to migrate my existing code with Hibernate4? (the class given for example above is a fake example to illustrate the many cases using the LAZY loading) Thank you.

As requested, see my DAO below :

public class MyAppJpaDAO extends GenericJpaDAO implements IMyAppDAO {
    protected static Log log = LogFactory.getLog(MyAppJpaDAO.class);

    // Entity Manager of the project
    @PersistenceContext(unitName = "MyApp.hibernate")
    private EntityManager em;

    public News readLastNews() {
        StringBuffer sql = new StringBuffer("");
        sql.append(" select object(n) ");
        sql.append(" from News n ");
        sql.append(" Where n.flagLastStatus = 'V' ");
        sql.append(" order by n.pk.date desc ");
        Query aQuery = em.createQuery(sql.toString());

        List<News> res = (List<News>) aQuery.getResultList();
        if (res != null && res.size() != 0) {
            return res.get(0);
        }
        return null;
    }

    //...

    }

/////////////

public class GenericJpaDAO implements IGenericDAO {

    protected static Log log = LogFactory.getLog(GenericJpaDAO.class);

    @PersistenceContext(unitName = "MyApp.hibernate")
    EntityManager em;

    public Object getReference(Class _class, Object _object) {
        return em.getReference(_class, _object);
    }

    public void createObject(Object object) {
        try {
            em.persist(object);
        } catch (LazyInitializationException lie) {
            em.merge(em.merge(object));
        }
    }

    public void deleteObject(Object object) {
        try {
            em.remove(object);
        } catch (Exception e) {
            em.remove(em.merge(object));
        }
    }

    public void updateObject(Object object) {
        em.merge(em.merge(object));
    }

    //...

    }
stof
  • 1
  • 1

3 Answers3

0

If you want to use LazyLoading, you need to have the session opened and connected at the time when you calls .getInfo(). org.hibernate.LazyInitializationException occures if you tries to get an entity but the session is disconnected or closed. I think you have problems with session handling. There is nothing to do with the entities.

bakcsa83
  • 405
  • 1
  • 7
  • 20
  • What do you mean exactly? The code is the same as before and I don't see explicitely any reference to "sessions" in the project. Previously the session sould have stayed open because cli.getInfo() gave me the data. What do I have to change? – stof Feb 06 '14 at 15:19
  • Everything is dealen with an Entitymanager : in my DAO, I only have : @PersistenceContext(unitName = "myApp.hibernate") private EntityManager em; -->I don't open or close the session explicitely. – stof Feb 10 '14 at 12:45
  • could you please post your DAO? – bakcsa83 Feb 10 '14 at 12:50
  • I've just posted my DAO – stof Feb 10 '14 at 14:44
  • I read a lot of things about the ways to deal with spring transactions/sessions but none match with the way my application is written. I really don't know what to do with the new versions of Spring/Hibernate... – stof Feb 13 '14 at 09:20
0

If the SessionFactory is configured in a Spring context file, we can use the OpenSessionInViewFilter to keep the session open.

<filter>
    <filter-name>Hibernate Session In View Filter</filter-name>
       <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>Hibernate Session In View Filter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

Unfortunately, my application is not configured like this...

stof
  • 1
  • 1
0

Interesting...but still not helping http://www.javacodegeeks.com/2012/07/four-solutions-to-lazyinitializationexc_05.html

But I find something :

1)Hibernate 3.2.1 GA and Spring 2.0 I used to put a Person having a LAZY bag in a Group and when I wanted to get some pencil from the bag of any person of the group, I was able to get it.

2)Hibernate 4.2.8 et Spring 3.2.5. If I don't explicitely ask to know the content of the bag just after getting the Person and before putting it into the group, I will have the lazy exception.

If someone could explain me why...

stof
  • 1
  • 1