We are converting our application from JBoss AS 5.2 to JBoss AS 6.3. The createNamedQuery method that is called by default in 6.3 is causing our code to throw a Hibernate exception, due to the detachQueryNonTxInvocation call within createNamedQuery method.
After calling em.createNamedQuery("someStringValue"), we call (back in our EJB) Hibernate.initialize(someEntity.getSomeCollection), which now results in the Hibernate Exception "collection is not associated with any session". The transaction atribute on that method is TransactionAttributeType.SUPPORTS
We use the Hibernate.initialize pattern in many places in our code base.
We can avoid the "collection is not associated with any session" exception in two ways: 1. Instead of passing in the default EntityManager to the Entity, pass in em.getEntityManagerFactory().createEntityManager() or 2. After getting the result of the query, call HibernateThreadLocal.getSession().refresh(result) before calling Hibernate.initialize(someEntity.getSomeCollection).
Are either of the two "workarounds" above good practice, and why the change in JBoss 6.x to detach entities loaded by a query in a non-jta invocation?
|