我尝试在Spring MVC 3.1应用程序中使用Hibernate 4.1.5.Final。
(更新:现在是Hibernate 4.1.6.Final和Spring 3.1.2.RELEASE,但仍然没有运气!)
当我尝试删除一个实体时,我得到一个例外,我不明白。
以下是涉及的两个主要类别:
用户:
@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;
}
}
FacebookAccount:
@Entity
@Table(name="facebookAccount")
public class FacebookAccount extends BaseModel
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToOne
private User user;
// ...
}
假设我有一个拥有FacebookAccount的用户的引用。在一个事务中(在@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() 存在于用户中并且是公开的!
有什么想法吗?
更新:
我做了一些测试,结论是问题是“orphanRemoval = true”。没有它,我可以使用以下方法删除FacebookAccount:
user.setFacebookAccount(null);
facebookAccountRepository.delete(facebookAccount);
但这不是我想要的。我希望 orphanRemoval 能够正常工作!
更新2:
JPA似乎there is a problem与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()
{
}
}
Stacktrace:
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:
A full and minimal application to reproduce the issue.使用我的项目Web结构(控制器/服务/存储库)似乎发生了这个问题,因此应用程序中没有单元测试。相反,你必须运行:
mvn clean jetty:run
然后将浏览器指向:
http://localhost:8080/
触发异常。将被调用的控制器#方法是com.example.web.controllers.TestController #test()。
答案 0 :(得分:2)
答案 1 :(得分:1)
您可以查看问题的实施https://github.com/bondarenko/so-12009955
。它在我的工作中顺利进行。
答案 2 :(得分:1)
如果它可以帮助某人......
我仍有问题,这次与to this有关:当被其他实体替换时,子实体未被删除。仅在设置为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,保存父实例,然后将其关联到新实例。
我希望这种解决方法永远有效!