0

OpenSessionInViewFilterを使用しています。これは私のweb.xmlの最初のフィルターです

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

これが私のapplicationContext.xmlの一部です

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

<bean id="userBc" class="com.tutorial.bc.auth.UserBcImpl">
    <property name="userDao">
      <ref local="userDao"/>
    </property>
  </bean>
...
<tx:annotation-driven transaction-manager="txManager"/>

hbmファイルでの私のマッピング-

<set name="userCoachingRoles" table="user_coaching_role" lazy="true" cascade="all-delete-orphan">
      <key column="user_id"/>
      <many-to-many column="coaching_id" class="com.tutorial.entity.coaching.Coaching" lazy="proxy"/>
</set>

ユーザーを取得するuserBcのメソッドには注釈が付けられ@Transactional、エンティティUserにはメソッドがありgetUserCoachingRolesます。このメソッドにヒットする例外が発生します-

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

を使用してもセッションがない理由がわかりませんOpenSessionInViewFilter。これについて助けが必要です。

4

1 に答える 1

1

getUserCoachingRolesひょっとして、別のスレッドで呼び出していますか? その場合、これは予期される動作です。

UserCoachingRoles積極的にロードされないため、Spring はそのスレッド内のデータベース接続にアクセスできず、ロードできません。必要がある:

  • スレッドで囲んでいるセッション ラッパーを作成します。
  • オブジェクトを渡す代わりにの ID をUserCoachingRolesスレッドに渡し、このスレッド内にプロパティをロードします
于 2012-11-09T21:28:07.507 に答える