У меня есть приложение EJB на JBoss AS 7.1.1, которое использует Hibernate 4.3 для подключения к базе данных через jta-data-source. Мой persistence.xml:LazyInitializationException в интерфейсе SpringMVC с бэкэндом EJB
<?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>
Я использую DAO шаблон как 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;
}
...
В EJB бэкэндом все Ленивый-инициализируются поля работают отлично. Теперь мне нужна небольшая веб-панель администратора для моего бэкэнда. Я знаком с Spring MVC и решил использовать его. Весной MVC не является проблемой использовать EJB beans. Но Ленивый-инициализируются поля не работают в контроллерах Spring и я получаю:
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
Я пытался использовать OpenSessionInViewFilter в 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 нуждается SessionFactory боб. Я добавил его определение в 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>
Но это не решило мою проблему ... У вас есть какие-либо советы? Спасибо большое!