この例外の原因は、どうにかわかりません。
Request processing failed; nested exception is javax.persistence.PersistenceException: org.hibernate.PropertyAccessException: Exception occurred inside setter of my.Class
根本的な原因:
javax.persistence.PersistenceException: org.hibernate.PropertyAccessException: Exception occurred inside setter of my.Class
コード:
@ManyToMany(fetch = FetchType.EAGER)
public void setTags(Set<Tags> tags) {
this.tags.clear();
for (Tag tag : tags) {
addTag(tag);
}
}
public boolean addTag(final Tag tag) {
if (tags.contains(tag)) {
return false;
}
return tags.add(tag);
}
コンストラクターでタグを初期化します。
tags = new HashSet<Tag>();
編集
Setterメソッドでのログインの例外:
ax.persistence.PersistenceException: org.hibernate.PropertyAccessException: Exception occurred inside setter of com.mycompany.domain.Book.Tags
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.Java:1387) ~[hibernate-entitymanager-4.2.0.Final.jar:4.2.0.Final]
at org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.Java:838) ~[hibernate-entitymanager-4.2.0.Final.jar:4.2.0.Final]
at org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.Java:781) ~[hibernate-entitymanager-4.2.0.Final.jar:4.2.0.Final]
at Sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:201304051638]
at Sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.Java:57) ~[na:201304051638]
at Sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.Java:43) ~[na:201304051638]
at Java.lang.reflect.Method.invoke(Method.Java:601) ~[na:1.7.0]
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.Java:240) ~[spring-orm-3.1.4.RELEASE.jar:3.1.4.RELEASE]
at $Proxy215.find(Unknown Source) ~[na:na]
at com.mycompany.persistence.BookJpaRepository.get(BookJpaRepository.Java:22) ~[classes/:na]
at com.mycompany.service.BookService.getBook(BookService.Java:44) ~[classes/:na]
at com.mycompany.service.BookService$$FastClassByCGLIB$$d6b91ae6.invoke(<generated>) ~[BookService$$FastClassByCGLIB$$d6b91ae6.class:na]
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.Java:204) ~[spring-core-3.2.1.RELEASE.jar:3.2.1.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.Java:698) ~[spring-aop-3.2.1.RELEASE.jar:3.2.1.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.Java:150) ~[spring-aop-3.2.1.RELEASE.jar:3.2.1.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.Java:110) ~[spring-tx-3.2.1.RELEASE.jar:3.2.1.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.Java:172) ~[spring-aop-3.2.1.RELEASE.jar:3.2.1.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.Java:631) ~[spring-aop-3.2.1.RELEASE.jar:3.2.1.RELEASE]
at com.mycompany.service.BookService$$EnhancerByCGLIB$$e6a4e1a3.getBook(<generated>) ~[BookService$$EnhancerByCGLIB$$e6a4e1a3.class:na]
at com.mycompany.rest.controller.BookController.getFeedbackForBook(BookController.Java:106) ~[classes/:na]
at Sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:201304051638]
at Sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.Java:57) ~[na:201304051638]
at Sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.Java:43) ~[na:201304051638]
at Java.lang.reflect.Method.invoke(Method.Java:601) ~[na:1.7.0]
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.Java:219) ~[spring-web-3.2.1.RELEASE.jar:3.2.1.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.Java:132) ~[spring-web-3.2.1.RELEASE.jar:3.2.1.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.Java:104) ~[spring-webmvc-3.2.1.RELEASE.jar:3.2.1.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.Java:745) ~[spring-webmvc-3.2.1.RELEASE.jar:3.2.1.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.Java:686) ~[spring-webmvc-3.2.1.RELEASE.jar:3.2.1.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.Java:80) ~[spring-webmvc-3.2.1.RELEASE.jar:3.2.1.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.Java:925) ~[spring-webmvc-3.2.1.RELEASE.jar:3.2.1.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.Java:856) ~[spring-webmvc-3.2.1.RELEASE.jar:3.2.1.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.Java:920) [spring-webmvc-3.2.1.RELEASE.jar:3.2.1.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.Java:816) [spring-webmvc-3.2.1.RELEASE.jar:3.2.1.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.Java:668) [javax.servlet-api.jar:3.0.1]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.Java:801) [spring-webmvc-3.2.1.RELEASE.jar:3.2.1.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.Java:770) [javax.servlet-api.jar:3.0.1]
at org.Apache.catalina.core.StandardWrapper.service(StandardWrapper.Java:1550) [web-core.jar:3.1.2.1-SNAPSHOT]
at org.Apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.Java:281) [web-core.jar:3.1.2.1-SNAPSHOT]
at org.Apache.catalina.core.StandardContextValve.__invoke(StandardContextValve.Java:175) [web-core.jar:3.1.2.1-SNAPSHOT]
at org.Apache.catalina.core.StandardContextValve.invoke(StandardContextValve.Java) [web-core.jar:3.1.2.1-SNAPSHOT]
at org.Apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.Java:655) [web-core.jar:3.1.2.1-SNAPSHOT]
at org.Apache.catalina.core.StandardPipeline.invoke(StandardPipeline.Java:595) [web-core.jar:3.1.2.1-SNAPSHOT]
at org.Apache.catalina.core.StandardHostValve.__invoke(StandardHostValve.Java:161) [web-core.jar:3.1.2.1-SNAPSHOT]
at org.Apache.catalina.core.StandardHostValve.invoke(StandardHostValve.Java) [web-core.jar:3.1.2.1-SNAPSHOT]
at org.Apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.Java:331) [web-core.jar:3.1.2.1-SNAPSHOT]
at org.Apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.Java:231) [web-core.jar:3.1.2.1-SNAPSHOT]
at com.Sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.Java:317) [kernel.jar:3.1.2.1-SNAPSHOT]
at com.Sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.Java:195) [kernel.jar:3.1.2.1-SNAPSHOT]
at com.Sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.Java:860) [grizzly-http.jar:1.9.50]
at com.Sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.Java:757) [grizzly-http.jar:1.9.50]
at com.Sun.grizzly.http.ProcessorTask.process(ProcessorTask.Java:1056) [grizzly-http.jar:1.9.50]
at com.Sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.Java:229) [grizzly-http.jar:1.9.50]
at com.Sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.Java:137) [grizzly-framework.jar:1.9.50]
at com.Sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.Java:104) [grizzly-framework.jar:1.9.50]
at com.Sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.Java:90) [grizzly-framework.jar:1.9.50]
at com.Sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.Java:79) [grizzly-http.jar:1.9.50]
at com.Sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.Java:54) [grizzly-framework.jar:1.9.50]
at com.Sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.Java:59) [grizzly-framework.jar:1.9.50]
at com.Sun.grizzly.ContextTask.run(ContextTask.Java:71) [grizzly-framework.jar:1.9.50]
at com.Sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.Java:532) [grizzly-utils.jar:1.9.50]
at com.Sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.Java:513) [grizzly-utils.jar:1.9.50]
at Java.lang.Thread.run(Thread.Java:722) [na:1.7.0]
Caused by: org.hibernate.PropertyAccessException: Exception occurred inside setter of com.mycompany.domain.Book.Tags
at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.Java:88) ~[hibernate-core-4.2.0.Final.jar:4.2.0.Final]
at org.hibernate.Tuple.entity.AbstractEntityTuplizer.setPropertyValues(AbstractEntityTuplizer.Java:710) ~[hibernate-core-4.2.0.Final.jar:4.2.0.Final]
at org.hibernate.Tuple.entity.PojoEntityTuplizer.setPropertyValues(PojoEntityTuplizer.Java:371) ~[hibernate-core-4.2.0.Final.jar:4.2.0.Final]
at org.hibernate.persister.entity.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.Java:4499) ~[hibernate-core-4.2.0.Final.jar:4.2.0.Final]
at org.hibernate.engine.internal.TwoPhaseLoad.doInitializeEntity(TwoPhaseLoad.Java:185) ~[hibernate-core-4.2.0.Final.jar:4.2.0.Final]
at org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.Java:137) ~[hibernate-core-4.2.0.Final.jar:4.2.0.Final]
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.Java:1103) ~[hibernate-core-4.2.0.Final.jar:4.2.0.Final]
at org.hibernate.loader.Loader.processResultSet(Loader.Java:960) ~[hibernate-core-4.2.0.Final.jar:4.2.0.Final]
at org.hibernate.loader.Loader.doQuery(Loader.Java:910) ~[hibernate-core-4.2.0.Final.jar:4.2.0.Final]
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.Java:341) ~[hibernate-core-4.2.0.Final.jar:4.2.0.Final]
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.Java:311) ~[hibernate-core-4.2.0.Final.jar:4.2.0.Final]
at org.hibernate.loader.Loader.loadEntity(Loader.Java:2111) ~[hibernate-core-4.2.0.Final.jar:4.2.0.Final]
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.Java:82) ~[hibernate-core-4.2.0.Final.jar:4.2.0.Final]
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.Java:72) ~[hibernate-core-4.2.0.Final.jar:4.2.0.Final]
at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.Java:3917) ~[hibernate-core-4.2.0.Final.jar:4.2.0.Final]
at org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.Java:460) ~[hibernate-core-4.2.0.Final.jar:4.2.0.Final]
at org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.Java:429) ~[hibernate-core-4.2.0.Final.jar:4.2.0.Final]
at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.Java:206) ~[hibernate-core-4.2.0.Final.jar:4.2.0.Final]
at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.Java:262) ~[hibernate-core-4.2.0.Final.jar:4.2.0.Final]
at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.Java:150) ~[hibernate-core-4.2.0.Final.jar:4.2.0.Final]
at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.Java:1091) ~[hibernate-core-4.2.0.Final.jar:4.2.0.Final]
at org.hibernate.internal.SessionImpl.access$2000(SessionImpl.Java:174) ~[hibernate-core-4.2.0.Final.jar:4.2.0.Final]
at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.load(SessionImpl.Java:2473) ~[hibernate-core-4.2.0.Final.jar:4.2.0.Final]
at org.hibernate.internal.SessionImpl.get(SessionImpl.Java:987) ~[hibernate-core-4.2.0.Final.jar:4.2.0.Final]
at org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.Java:807) ~[hibernate-entitymanager-4.2.0.Final.jar:4.2.0.Final]
... 61 common frames omitted
Caused by: Java.lang.reflect.InvocationTargetException: null
at Sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:201304051638]
at Sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.Java:57) ~[na:201304051638]
at Sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.Java:43) ~[na:201304051638]
at Java.lang.reflect.Method.invoke(Method.Java:601) ~[na:1.7.0]
at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.Java:65) ~[hibernate-core-4.2.0.Final.jar:4.2.0.Final]
... 85 common frames omitted
Caused by: Java.lang.NullPointerException: null
at org.hibernate.engine.internal.StatefulPersistenceContext.getLoadedCollectionOwnerOrNull(StatefulPersistenceContext.Java:859) ~[hibernate-core-4.2.0.Final.jar:4.2.0.Final]
at org.hibernate.event.spi.AbstractCollectionEvent.getLoadedOwnerOrNull(AbstractCollectionEvent.Java:75) ~[hibernate-core-4.2.0.Final.jar:4.2.0.Final]
at org.hibernate.event.spi.InitializeCollectionEvent.<init>(InitializeCollectionEvent.Java:36) ~[hibernate-core-4.2.0.Final.jar:4.2.0.Final]
at org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.Java:1846) ~[hibernate-core-4.2.0.Final.jar:4.2.0.Final]
at org.hibernate.collection.internal.AbstractPersistentCollection$4.doWork(AbstractPersistentCollection.Java:549) ~[hibernate-core-4.2.0.Final.jar:4.2.0.Final]
at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.Java:234) ~[hibernate-core-4.2.0.Final.jar:4.2.0.Final]
at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.Java:545) ~[hibernate-core-4.2.0.Final.jar:4.2.0.Final]
at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.Java:124) ~[hibernate-core-4.2.0.Final.jar:4.2.0.Final]
at org.hibernate.collection.internal.PersistentSet.iterator(PersistentSet.Java:180) ~[hibernate-core-4.2.0.Final.jar:4.2.0.Final]
at com.mycompany.domain.Book.setTags(Book.Java:185) ~[classes/:na]
... 90 common frames omitted
私の推測では、Hibernateはレイジーロードを実装する独自のSet(PersistentSet)の実装でセッターを呼び出し、セッターが呼び出されたときにまだ初期化されていません。このセットでメソッドを呼び出すので、既にロード段階にある間にセット自体がロードされ、Hibernateが不整合な状態になります。
そのため、プロパティアクセスよりもフィールドアクセスの方を使用します(つまり、すべてのマッピングアノテーションをゲッターではなくフィールドに配置します)。メソッドが「通常の」コードによって呼び出されたときに、渡されたセットのコピーを作成する必要がありますが、Hibernate自体がセッターを呼び出すときにコピーを作成したくありません。遅延読み込みが完全に中断されます。
javadoc から:
リフレクションによって、またはCGLIBを介して永続クラスのインスタンスのプロパティにアクセスするときに問題が発生しました。考えられる根本的な原因はいくつかあります。
- failure of a security check - an exception occurring inside the getter or setter method - a nullable database column was mapped to a primitive-type property - the Hibernate type was not castable to the property type (or vice-versa)
this.tags
(フィールド)またはtags
(パラメータ)はnull
ですが、それを理解するために、スタックトレース全体を出力できます。tisは根本的な原因を示します。
メソッドの引数としてnull
を渡しているため、for-each構文が例外をスローします。ガード条件を使用すると、すべてがうまくいきます:
@ManyToMany(fetch = FetchType.EAGER)
public void setTags(Set<Tags> tags) {
this.tags.clear();
if (tags != null) {
for (Tag tag : tags) {
addTag(tag);
}
}
}
でも私は〜がいい:
@ManyToMany(fetch = FetchType.EAGER)
public void setTags(Set<Tags> tags) {
this.tags = tags;
}
これを理解する最も簡単な方法は、setTagsの内部をtry catchでラップし、選択したロギングシステムを使用して例外をログに記録することです。
私の最初の推測は、タグのインスタンス変数がnullであることです。これが当てはまらない場合は非常に驚きますが、統合テストでデバッガを使用して確認するのは簡単です。
2つ目の推測は、setTags()に渡された引数が正しく初期化されていないか、nullであるか、forループが繰り返しを試行したときに何らかの例外をスローしていることです。