2012-02-16 9 views
5

私はすでにJBoss webappを動作させていますが、遅延初期化の問題があります。 したがって、私は春に調査し、OpenEntityManagerInViewFilterを使用するようにアドバイスされました。OpenEntityManagerInViewFilterによる遅延初期化?

まだ私はエラーが表示されますが、あなたが私を助けてくれることを願っていますか? Spring OEMフィルタを利用するために、私のアプリで何か変更する必要がありますか?

@Entity 
class Customer; 

@Stateless 
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) 
class DaoService { 
    @PersistenceContext 
    EntityManager em; 
} 

@Named 
@RequestScoped 
class CustomerFacade; 

+ JSFもの:

私のセットアップは、このようなものです。

[javax.enterprise.resource.webcontainer.jsf.context] (http--127.0.0.1-8080-1) org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: domain.Customer.customerList, no session or session was closed 

私はこのようにそれを設定します。 web.xmlの

<filter> 
    <filter-name> 
     OpenEntityManagerInViewFilter 
    </filter-name> 
    <filter-class> 
     org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter 
    </filter-class> 
    <init-param> 
     <param-name>singleSession</param-name> 
     <param-value>true</param-value> 
    </init-param> 
    <init-param> 
     <param-name>flushMode</param-name> 
     <param-value>AUTO</param-value> 
    </init-param> 
</filter> 
<!-- Include this if you are using Hibernate --> 
<filter-mapping> 
    <filter-name>OpenEntityManagerInViewFilter</filter-name> 
    <url-pattern>/*</url-pattern> 
</filter-mapping> 

<!-- Spring config --> 
<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 
<listener> 
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> 
</listener> 

applicationContext.xmlを:

<bean id="entityManagerFactory" 
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
    <property name="jpaVendorAdapter"> 
     <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" /> 
    </property> 
    <property name="jpaProperties"> 
     <props> 
      <prop key="hibernate.transaction.manager_lookup_class"> 
       org.hibernate.transaction.JBossTransactionManagerLookup 
      </prop> 
     </props> 
    </property> 
</bean> 

の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"> 
     <!-- If you are running in a production environment, add a managed 
     data source, the example data source is just for proofs of concept! --> 
     <jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source> 
     <properties> 
     <!-- Properties for Hibernate --> 
     <property name="hibernate.hbm2ddl.auto" value="create-drop" /> 
     <property name="hibernate.show_sql" value="false" /> 
     </properties> 
    </persistence-unit> 
</persistence> 

ザッツ基本的にすべてのセットアップ。怠惰なローディングの問題とは別にすべてが機能しています。

+1

フィルタリングパイプライン(アプリケーションのメインコントローラサーブレット/フィルタの前)にOEMIVフィルタが最初に配置されていることを確認してください。 –

+0

これは私が投稿した完全なweb.xmlです。以前はフィルタがありません。 – membersound

+0

JSFを使用しています。 javax.faces.webapp.FacesServletサーブレットを宣言していませんか? –

答えて

2

は、あなたが(前回のリクエストでのセッションでものを連載など)古いエンティティにアクセスしないことを確認してくださいEntityManagerの初期化パラメータで示すことが必要です。

4

<filter> 
    <filter-name> 
     OpenEntityManagerInViewFilter 
    </filter-name> 
    <filter-class> 
     org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter 
    </filter-class> 
    <init-param> 
     <param-name>entityManagerFactoryBeanName</param-name> 
     <param-value>entityManagerFactory</param-value> 
    </init-param> 
    <init-param> 
     <param-name>flushMode</param-name> 
     <param-value>AUTO</param-value> 
    </init-param> 
</filter> 
+0

hm?それはまさに私が上に持っているものです... – membersound

+0

フィルタマッピングはどこですか? URLパターン?少なくともメンバーはそれを正しく行っています –

関連する問題