0

Stripes、Spring、JPA、および Hibernate を使用してプロジェクトを構築しています。多対 1 の子コレクションを持つオブジェクトがあります。ローディングをレイジーに設定しました。

@OneToMany(cascade = CascadeType.MERGE, mappedBy = "paperOffering", fetch = FetchType.LAZY)
private List<PaperOfferingAssessment> paperOfferingAssessments;

データベースからこれらのオブジェクトのコレクションを取得すると、子コレクションに直接アクセスする段階はありませんが、LazyInitializationException が発生します。

遅延読み込みとは、参照されたときにのみ子オブジェクト コレクションをインスタンス化することを意味すると思いましたか? 私は同様の問題を調べてみましたが、このすべてを理解することはできませんので、明確にしていただければ幸いです。

面白いことに、私は常にこの関係を設定していましたが、実際に例外に気付いたのは、データベースを変更して子テーブルと適切な外部キー関係を持つようにしたときだけでした。

編集:スタックトレース:

スタックトレース:

17:39:17] ERROR org.hibernate.LazyInitializationException  - failed to lazily initialize a collection of role: eis.model.PaperOffering.paperOfferingAssessments, no session or session was closed
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: eis.model.PaperOffering.paperOfferingAssessments, no session or session was closed
    at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:380)
    at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:372)
    at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:365)
    at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:108)
    at org.hibernate.collection.PersistentSet.toArray(PersistentSet.java:194)
    at net.sourceforge.stripes.ajax.JavaScriptBuilder.buildCollectionNode(JavaScriptBuilder.java:524)
    at net.sourceforge.stripes.ajax.JavaScriptBuilder.buildNode(JavaScriptBuilder.java:365)
    at net.sourceforge.stripes.ajax.JavaScriptBuilder.buildObjectNode(JavaScriptBuilder.java:420)
    at net.sourceforge.stripes.ajax.JavaScriptBuilder.buildNode(JavaScriptBuilder.java:374)
    at net.sourceforge.stripes.ajax.JavaScriptBuilder.buildArrayNode(JavaScriptBuilder.java:507)
    at net.sourceforge.stripes.ajax.JavaScriptBuilder.buildCollectionNode(JavaScriptBuilder.java:524)
    at net.sourceforge.stripes.ajax.JavaScriptBuilder.buildNode(JavaScriptBuilder.java:365)
    at net.sourceforge.stripes.ajax.JavaScriptBuilder.build(JavaScriptBuilder.java:191)
    at net.sourceforge.stripes.ajax.JavaScriptResolution.execute(JavaScriptResolution.java:75)
    at net.sourceforge.stripes.controller.DispatcherHelper$7.intercept(DispatcherHelper.java:508)
    at net.sourceforge.stripes.controller.ExecutionContext.proceed(ExecutionContext.java:158)
    at net.sourceforge.stripes.controller.HttpCacheInterceptor.intercept(HttpCacheInterceptor.java:99)
    at net.sourceforge.stripes.controller.ExecutionContext.proceed(ExecutionContext.java:155)
    at net.sourceforge.stripes.controller.BeforeAfterMethodInterceptor.intercept(BeforeAfterMethodInterceptor.java:113)
    at net.sourceforge.stripes.controller.ExecutionContext.proceed(ExecutionContext.java:155)
    at net.sourceforge.stripes.controller.ExecutionContext.wrap(ExecutionContext.java:74)
    at net.sourceforge.stripes.controller.DispatcherHelper.executeResolution(DispatcherHelper.java:502)
    at net.sourceforge.stripes.controller.DispatcherServlet.executeResolution(DispatcherServlet.java:286)
    at net.sourceforge.stripes.controller.DispatcherServlet.service(DispatcherServlet.java:170)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
    at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:527)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1216)
    at net.sourceforge.stripes.controller.StripesFilter.doFilter(StripesFilter.java:247)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1187)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:343)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:109)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:97)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
    at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:100)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
    at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:78)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
    at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
    at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:35)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
    at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:188)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
    at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:79)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
    at org.springframework.security.web.session.ConcurrentSessionFilter.doFilter(ConcurrentSessionFilter.java:109)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:149)
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237)
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1187)
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:421)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:119)
    at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:493)
    at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:225)
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:930)
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:358)
    at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:183)
    at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:866)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:117)
    at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:245)
    at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:126)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:113)
    at org.eclipse.jetty.server.Server.handle(Server.java:351)
    at org.eclipse.jetty.server.HttpConnection.handleRequest(HttpConnection.java:594)
    at org.eclipse.jetty.server.HttpConnection$RequestHandler.content(HttpConnection.java:1059)
    at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:764)
    at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:217)
    at org.eclipse.jetty.server.HttpConnection.handle(HttpConnection.java:424)
    at org.eclipse.jetty.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:506)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:436)
    at java.lang.Thread.run(Thread.java:619)
[17:39:17] WARN  net.sourceforge.stripes.ajax.JavaScriptBuilder  - Could not translate property [paperOfferingAssessments] of type [java.util.Set] due to an exception.
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: eis.model.PaperOffering.paperOfferingAssessments, no session or session was closed
    at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:380)
    at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:372)
    at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:365)
    at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:108)
    at org.hibernate.collection.PersistentSet.toArray(PersistentSet.java:194)
    at net.sourceforge.stripes.ajax.JavaScriptBuilder.buildCollectionNode(JavaScriptBuilder.java:524)
    at net.sourceforge.stripes.ajax.JavaScriptBuilder.buildNode(JavaScriptBuilder.java:365)
    at net.sourceforge.stripes.ajax.JavaScriptBuilder.buildObjectNode(JavaScriptBuilder.java:420)
    at net.sourceforge.stripes.ajax.JavaScriptBuilder.buildNode(JavaScriptBuilder.java:374)
    at net.sourceforge.stripes.ajax.JavaScriptBuilder.buildArrayNode(JavaScriptBuilder.java:507)
    at net.sourceforge.stripes.ajax.JavaScriptBuilder.buildCollectionNode(JavaScriptBuilder.java:524)
    at net.sourceforge.stripes.ajax.JavaScriptBuilder.buildNode(JavaScriptBuilder.java:365)
    at net.sourceforge.stripes.ajax.JavaScriptBuilder.build(JavaScriptBuilder.java:191)
    at net.sourceforge.stripes.ajax.JavaScriptResolution.execute(JavaScriptResolution.java:75)
    at net.sourceforge.stripes.controller.DispatcherHelper$7.intercept(DispatcherHelper.java:508)
    at net.sourceforge.stripes.controller.ExecutionContext.proceed(ExecutionContext.java:158)
    at net.sourceforge.stripes.controller.HttpCacheInterceptor.intercept(HttpCacheInterceptor.java:99)
    at net.sourceforge.stripes.controller.ExecutionContext.proceed(ExecutionContext.java:155)
    at net.sourceforge.stripes.controller.BeforeAfterMethodInterceptor.intercept(BeforeAfterMethodInterceptor.java:113)
    at net.sourceforge.stripes.controller.ExecutionContext.proceed(ExecutionContext.java:155)
    at net.sourceforge.stripes.controller.ExecutionContext.wrap(ExecutionContext.java:74)
    at net.sourceforge.stripes.controller.DispatcherHelper.executeResolution(DispatcherHelper.java:502)
    at net.sourceforge.stripes.controller.DispatcherServlet.executeResolution(DispatcherServlet.java:286)
    at net.sourceforge.stripes.controller.DispatcherServlet.service(DispatcherServlet.java:170)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
    at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:527)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1216)
    at net.sourceforge.stripes.controller.StripesFilter.doFilter(StripesFilter.java:247)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1187)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:343)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:109)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:97)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
    at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:100)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
    at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:78)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
    at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
    at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:35)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
    at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:188)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
    at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:79)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
    at org.springframework.security.web.session.ConcurrentSessionFilter.doFilter(ConcurrentSessionFilter.java:109)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:149)
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237)
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1187)
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:421)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:119)
    at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:493)
    at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:225)
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:930)
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:358)
    at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:183)
    at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:866)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:117)
    at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:245)
    at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:126)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:113)
    at org.eclipse.jetty.server.Server.handle(Server.java:351)
    at org.eclipse.jetty.server.HttpConnection.handleRequest(HttpConnection.java:594)
    at org.eclipse.jetty.server.HttpConnection$RequestHandler.content(HttpConnection.java:1059)
    at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:764)
    at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:217)
    at org.eclipse.jetty.server.HttpConnection.handle(HttpConnection.java:424)
    at org.eclipse.jetty.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:506)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:436)
    at java.lang.Thread.run(Thread.java:619)
4

2 に答える 2

1

レイジーとは、誰かが呼び出したときにコレクションアイテムが読み込まれること、get(index)または完全に初期化されたコレクションで動作する必要がある他のメソッドを意味します。size()コレクションを初期化しません。

エラーとは、コレクションから要素を取得しようとするコードが、Hibernateセッションの外部で機能していることを意味します。セッションは少し前に閉じられたので、怠惰な切り離されたコレクションを取得します。

コレクションで動作するコードがトランザクション内で実行されていることを確認してください。たぶん、yoiuはメソッドに@Transactionalアノテーションを付けるか(宣言型の説明を使用している場合)、コードをその中に配置する必要があります

TransactionManager tm = //get TransactionManager
try {
    tm.begin();
    //your code
    tm.commit();
} catch (Exception e) {
    tm.rollback();
}
于 2010-08-31T06:17:30.887 に答える
0

toString、hashCode、または equals メソッドでコレクションにアクセスしていませんか? 後の 2 つは、休止状態で結果をキャッシュに追加するために使用できます。

于 2010-08-06T06:29:41.913 に答える