1

I have an EJB application on JBoss AS 7.1.1, that use Hibernate 4.3 for database connection via jta-data-source. My persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
             xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
                                 http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="primary" transaction-type="JTA">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <jta-data-source>java:/postds</jta-data-source>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.ProgressDialect" />
            <property name="hibernate.connection.charSet" value="UTF-8" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
            <property name="hibernate.show_sql" value="false" />
        </properties>
    </persistence-unit>
</persistence>

I use DAO pattern like this:

@Stateless  
public abstract class GenericHibernateDAO<T, ID extends Serializable>  
        implements GenericDAO<T, ID> {  

    private Class<T> persistentClass;  

    @PersistenceContext  
    private EntityManager em;  

    protected Session getSession() {
        return em.unwrap(Session.class);
    }

    public T save(T entity) {
        getSession().saveOrUpdate(entity);
        return entity;
    }

...  

In EJB backend all Lazy-initialized fields work fine. Now I need a small web admin panel for my backend. I am familiar with Spring MVC and decided use it. In Spring MVC is not a problem use EJB beans. But Lazy-initialized fields don't work in Spring controllers and I get:

org.hibernate.LazyInitializationException: could not initialize proxy - no Session

I tried to use OpenSessionInViewFilter in web.xml:

    <filter>
        <filter-name>openSessionFilter</filter-name>
        <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>openSessionFilter</filter-name>
        <url-pattern>/admin/*</url-pattern>
    </filter-mapping>

OpenSessionInViewFilter needs sessionFactory bean. I added it's definition into applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
                           http://www.springframework.org/schema/jee
                           http://www.springframework.org/schema/jee/spring-jee-3.2.xsd">

    <jee:jndi-lookup id="dataSource" jndi-name="java:/postds"/>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
    </bean>

</beans>

But it did not solve my problem... Have you any advice? Thanks a lot!

Idel Pivnitskiy
  • 1,057
  • 7
  • 9

1 Answers1

0

If you use EJB container manages transactions and sessions for you. After transaction commit (by default all EJB methods are transactional) container destroy the session. If you use lazy loading then you get the org.hibernate.LazyInitializationException.

Try use fetch join lazy loaded fields via HQL or Criteria API.

Please see this answer for more information.

Community
  • 1
  • 1
A. A. Vybegallo
  • 246
  • 1
  • 6