8

Spring MVC 3.1 アプリケーション内で Hibernate 4.1.5.Final を使用しようとしています。

(更新: Hibernate 4.1.6.Final および Spring 3.1.2.RELEASE になりましたが、まだうまくいきません!)

エンティティを削除しようとすると、理解できない例外が発生します。

関連する 2 つの主要なクラスを次に示します。

ユーザー :

@Entity
@Table(name="usertable")
public class User extends BaseModel
{ 
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    private long id;

    @OneToOne(optional=true, cascade=CascadeType.ALL, mappedBy="user", orphanRemoval=true, fetch=FetchType.LAZY)
    private FacebookAccount facebookAccount;


    // ...

    public long getId()
    {
        return id;
    }

    public void setId(long id)
    {
        this.id = id;
    }

    public FacebookAccount getFacebookAccount()
    {
        return facebookAccount;
    }

    public void setFacebookAccount(FacebookAccount facebookAccount)
    {
        this.facebookAccount = facebookAccount;
    }
}

Facebookのアカウント :

@Entity
@Table(name="facebookAccount")
public class FacebookAccount extends BaseModel
{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    private Long id;

    @OneToOne
    private User user;

    // ...

}

FacebookAccount を持つ User への参照があるとします。トランザクションで(@Serviceで)私はやろうとしています:

user.setFacebookAccount(null);
userRepository.save(user);  

私も試しました:

user.setFacebookAccount(null);
facebookAccountRepository.delete(facebookAccount); 

両方で、私はこの例外を受け取ります:

org.springframework.orm.hibernate3.HibernateSystemException: could not get a field value by reflection getter of com.XXXXX.model.user.User.id; nested exception is org.hibernate.PropertyAccessException: could not get a field value by reflection getter of com.XXXXX.model.user.User.id
    at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:683) ~[spring-orm-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:104) ~[spring-orm-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:516) ~[spring-orm-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:754) ~[spring-tx-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:723) ~[spring-tx-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:393) ~[spring-tx-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:120) ~[spring-tx-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) ~[spring-aop-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202) ~[spring-aop-3.1.0.RELEASE.jar:3.1.0.RELEASE]

何故ですか?getId()User に存在し、公開されています!

何か案が?

アップデート :

さらにいくつかのテストを行った結果、問題は「orphanRemoval = true」であるという結論になりました。それがなければ、次を使用して FacebookAccount を削除できます。

user.setFacebookAccount(null);
facebookAccountRepository.delete(facebookAccount); 

しかし、これはまさに私が望むものではありません。orphanRemovalが機能することを望みます!

更新 2:

JPA の orphanRemoval と @OneToOne の関係に問題があるようです。私は同じ問題を抱えていると思いますか?

更新 3:

BaseModel クラス:

@MappedSuperclass
public abstract class BaseModel
{
    @Column(name = "dateCreated", nullable = false)
    private Date dateCreated;

    @Column(name = "dateModified", nullable = false)
    private Date dateModified;

    public Date getDateCreated()
    {
        return dateCreated;
    }

    public Date getDateModified()
    {
        return dateModified;
    }

    @PreUpdate
    @PrePersist
    @PreRemove
    protected void prePersist() 
    {
        Date now = DateUtil.now().toDate();
        if(dateCreated == null)
        {
            dateCreated = now;
        }
        dateModified = now;
    }

    @PostUpdate
    @PostPersist
    @PostRemove
    protected void postPersist() 
    {
    }

}

スタックトレース :

org.springframework.orm.hibernate3.HibernateSystemException: could not get a field value by reflection getter of com.XXXXX.model.user.User.id; nested exception is org.hibernate.PropertyAccessException: could not get a field value by reflection getter of com.XXXXX.model.user.User.id
    at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:683) ~[spring-orm-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:104) ~[spring-orm-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:516) ~[spring-orm-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:754) ~[spring-tx-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:723) ~[spring-tx-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:393) ~[spring-tx-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:120) ~[spring-tx-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) ~[spring-aop-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202) ~[spring-aop-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at com.XXXXX.web.controllers.myprofile.abc.SomeController.facebookAccountConfirmationFormPosted(SomeController.java:272) ~[SomeController.class:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.6.0_22]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) ~[na:1.6.0_22]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) ~[na:1.6.0_22]
    at java.lang.reflect.Method.invoke(Method.java:597) ~[na:1.6.0_22]
    at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:212) ~[spring-web-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126) ~[spring-web-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96) ~[spring-webmvc-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617) ~[spring-webmvc-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578) ~[spring-webmvc-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80) ~[spring-webmvc-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:900) [spring-webmvc-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:827) [spring-webmvc-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882) [spring-webmvc-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789) [spring-webmvc-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727) [javax.servlet-2.5.0.v201103041518.jar:na]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:820) [javax.servlet-2.5.0.v201103041518.jar:na]
    at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:565) [jetty-servlet-7.6.2.v20120308.jar:7.6.2.v20120308]
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1360) [jetty-servlet-7.6.2.v20120308.jar:7.6.2.v20120308]
    at org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:147) [spring-orm-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76) [spring-web-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1331) [jetty-servlet-7.6.2.v20120308.jar:7.6.2.v20120308]
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88) [spring-web-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76) [spring-web-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1331) [jetty-servlet-7.6.2.v20120308.jar:7.6.2.v20120308]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:311) [spring-security-web-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:116) [spring-security-web-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83) [spring-security-web-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323) [spring-security-web-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113) [spring-security-web-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323) [spring-security-web-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:101) [spring-security-web-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323) [spring-security-web-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113) [spring-security-web-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323) [spring-security-web-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter.doFilter(RememberMeAuthenticationFilter.java:146) [spring-security-web-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323) [spring-security-web-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54) [spring-security-web-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323) [spring-security-web-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45) [spring-security-web-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323) [spring-security-web-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:182) [spring-security-web-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323) [spring-security-web-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105) [spring-security-web-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323) [spring-security-web-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87) [spring-security-web-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323) [spring-security-web-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at com.XXXXX.util.security.beanutils.AuthenticationManager.doFilter(AuthenticationManager.java:127) [AuthenticationManager.class:na]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323) [spring-security-web-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:173) ~[spring-security-web-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346) ~[spring-web-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259) ~[spring-web-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1331) [jetty-servlet-7.6.2.v20120308.jar:7.6.2.v20120308]
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77) ~[spring-web-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76) [spring-web-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1331) [jetty-servlet-7.6.2.v20120308.jar:7.6.2.v20120308]
    at com.XXXXX.util.web.filters.SaveGeneratedDynamicResourcesContentFilter.doFilter(SaveGeneratedDynamicResourcesContentFilter.java:60) ~[SaveGeneratedDynamicResourcesContentFilter.class:na]
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1331) [jetty-servlet-7.6.2.v20120308.jar:7.6.2.v20120308]
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:477) ~[na:na]
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:119) ~[na:na]
    at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:524) ~[na:na]
    at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:227) ~[na:na]
    at org.eclipse.jetty.server.handler.ContextHandler.__doHandle(ContextHandler.java:1031) ~[na:na]
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java) ~[na:na]
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:406) ~[na:na]
    at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:186) ~[na:na]
    at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:965) ~[na:na]
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:117) ~[na:na]
    at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:250) ~[na:na]
    at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:149) ~[na:na]
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:111) ~[na:na]
    at org.eclipse.jetty.server.Server.handle(Server.java:349) ~[na:na]
    at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:452) ~[na:na]
    at org.eclipse.jetty.server.AbstractHttpConnection.content(AbstractHttpConnection.java:894) ~[na:na]
    at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.content(AbstractHttpConnection.java:948) ~[na:na]
    at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:857) ~[na:na]
    at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:235) ~[na:na]
    at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:77) ~[na:na]
    at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:609) ~[na:na]
    at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:45) ~[na:na]
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:599) ~[na:na]
    at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:534) ~[na:na]
    at java.lang.Thread.run(Thread.java:662) ~[na:1.6.0_22]
Caused by: org.hibernate.PropertyAccessException: could not get a field value by reflection getter of com.XXXXX.model.user.User.id
    at org.hibernate.property.DirectPropertyAccessor$DirectGetter.get(DirectPropertyAccessor.java:62) ~[hibernate-core-4.1.6.Final.jar:4.1.6.Final]
    at org.hibernate.tuple.entity.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:341) ~[hibernate-core-4.1.6.Final.jar:4.1.6.Final]
    at org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:4425) ~[hibernate-core-4.1.6.Final.jar:4.1.6.Final]
    at org.hibernate.persister.entity.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:4147) ~[hibernate-core-4.1.6.Final.jar:4.1.6.Final]
    at org.hibernate.engine.internal.ForeignKeys.isTransient(ForeignKeys.java:209) ~[hibernate-core-4.1.6.Final.jar:4.1.6.Final]
    at org.hibernate.event.internal.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:102) ~[hibernate-core-4.1.6.Final.jar:4.1.6.Final]
    at org.hibernate.internal.SessionImpl.fireDelete(SessionImpl.java:954) ~[hibernate-core-4.1.6.Final.jar:4.1.6.Final]
    at org.hibernate.internal.SessionImpl.delete(SessionImpl.java:939) ~[hibernate-core-4.1.6.Final.jar:4.1.6.Final]
    at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:262) ~[hibernate-core-4.1.6.Final.jar:4.1.6.Final]
    at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:165) ~[hibernate-core-4.1.6.Final.jar:4.1.6.Final]
    at org.hibernate.event.internal.AbstractFlushingEventListener.cascadeOnFlush(AbstractFlushingEventListener.java:160) ~[hibernate-core-4.1.6.Final.jar:4.1.6.Final]
    at org.hibernate.event.internal.AbstractFlushingEventListener.prepareEntityFlushes(AbstractFlushingEventListener.java:151) ~[hibernate-core-4.1.6.Final.jar:4.1.6.Final]
    at org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:88) ~[hibernate-core-4.1.6.Final.jar:4.1.6.Final]
    at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51) ~[hibernate-core-4.1.6.Final.jar:4.1.6.Final]
    at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1214) ~[hibernate-core-4.1.6.Final.jar:4.1.6.Final]
    at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:403) ~[hibernate-core-4.1.6.Final.jar:4.1.6.Final]
    at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101) ~[hibernate-core-4.1.6.Final.jar:4.1.6.Final]
    at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:175) ~[hibernate-core-4.1.6.Final.jar:4.1.6.Final]
    at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:75) ~[hibernate-entitymanager-4.1.6.Final.jar:4.1.6.Final]
    at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:512) ~[spring-orm-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    ... 89 common frames omitted
Caused by: java.lang.IllegalArgumentException: Can not set long field com.XXXXX.model.user.User.id to com.XXXXX.model.social.FacebookAccount
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:146) ~[na:1.6.0_22]
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:150) ~[na:1.6.0_22]
    at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:37) ~[na:1.6.0_22]
    at sun.reflect.UnsafeLongFieldAccessorImpl.getLong(UnsafeLongFieldAccessorImpl.java:42) ~[na:1.6.0_22]
    at sun.reflect.UnsafeLongFieldAccessorImpl.get(UnsafeLongFieldAccessorImpl.java:18) ~[na:1.6.0_22]
    at java.lang.reflect.Field.get(Field.java:358) ~[na:1.6.0_22]
    at org.hibernate.property.DirectPropertyAccessor$DirectGetter.get(DirectPropertyAccessor.java:59) ~[hibernate-core-4.1.6.Final.jar:4.1.6.Final]
    ... 108 common frames omitted

更新 4:

問題を再現するための完全かつ最小限のアプリケーション。この問題は、プロジェクトの Web 構造 (コントローラー/サービス/リポジトリ) を使用して発生しているように見えるため、アプリケーションに単体テストはありません。代わりに、次を実行する必要があります。

mvn clean jetty:run

次に、ブラウザを次のように指定します。

http://localhost:8080/

例外をトリガーします。呼び出される controller#method は com.example.web.controllers.TestController#test() です。

4

3 に答える 3

1

それが誰かを助けることができるなら...

私はまだ問題を抱えていましたが、今回はこれに関連しています:子エンティティは、別のエンティティに置き換えられても削除されません。NULLに設定されている場合にのみ削除されます。

今のところ使用する回避策は、「親」で同じエンティティを強制的に再利用することです。

例えば ​​:

public class User extends BaseModel
{ 
    @OneToOne(cascade=CascadeType.ALL, orphanRemoval=true, fetch=FetchType.LAZY, optional=true)
    private FacebookAccount facebookAccount;

    public void setFacebookAccount(FacebookAccount facebookAccount)
    {
        if(facebookAccount != null && this.facebookAccount != null && (facebookAccount.getId() == 0 || this.facebookAccount.getId() != facebookAccount.getId()))
        {
            throw new OneToOneFixMustReUseExistingEntityException();
        }

        this.facebookAccount = facebookAccount;
    }

}

...ここで、OneToOneFixMustReUserExistingEntityExceptionはカスタム例外です。

本当に新しいインスタンスを関連付ける必要がある場合は現在のインスタンスをNULLに設定し、親を保存してから、それを新しいインスタンスに関連付けます。

この回避策が常に機能することを願っています!

于 2012-08-26T18:25:58.770 に答える
1

問題のこの実装https://github.com/bondarenko/so-12009955を確認できます。私の側ではスムーズに動作しています。

于 2012-08-19T11:50:15.953 に答える