We get a LazyInitializationException from a query outside a transaction in JBoss EAP.

Solution Unverified - Updated -

Issue

We are forced to use begin and commit transactions when we are retrieving an entity object which has another object in it which in turn has another object in it.

This begin and commit was not needed in the community edition of JBOSS. This is needed only in the JBOSS EAP versions.

All the database access code is contained in EJB 3.1 components using BEAN managed transactions. “@TransactionManagement(TransactionManagementType.BEAN)”

Code snippet:

    public List<User> getUsers(List<SearchCriteria> criteria, RequestOptions options)
            throws ServiceException
    {
        List<User> users = null;
        try{
            //begin();
              Query query = em.createQuery("select user from test.entity.User as user");
          List<?> resultset = query.getResultList();
          User user = (User) resultset.get(0);
              user.getRoles().size();
            //commit();
        } catch(Exception ex){
               rollback();
        }
        return users;
    }

If we don't use begin / commit transaction, we will get this "LazyInitializationException" when doing "user.getRoles()":

Caused by: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.test.entity.User.roles, could not initialize proxy - no Session

Environment

  • Red Hat JBoss Enterprise Application Platform (EAP)
    • 6
      • Hibernate 4
    • 5
      • Hibernate 3

Subscriber exclusive content

A Red Hat subscription provides unlimited access to our knowledgebase, tools, and much more.

Current Customers and Partners

Log in for full access

Log In

New to Red Hat?

Learn more about Red Hat subscriptions

Using a Red Hat product through a public cloud?

How to access this content