我在尝试通过REST API更新资源时遇到了Spring和Hibernate的错误。
我用最少的额外属性简化了案例。
我正在尝试更新名为Rule
的资源
Rule
有一个ThingGroup
,它代表一组对象。
Rule
还有一组Event
,表示规则的激活范围。
在执行应用程序期间,运行必须检查此事件组上的某些参数以触发警报。
我的问题是,在下面的规则服务中使用update
方法时,它会因此错误而失败。
org.hibernate.PersistentObjectException: detached entity passed to persist: com.smartobjectsecurity.common.domain.rule.Event
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:276)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:221)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:417)
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59)
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.jpa.repository.support.LockModeRepositoryPostProcessor$LockModePopulatingMethodIntercceptor.invoke(LockModeRepositoryPostProcessor.java:105)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
at com.sun.proxy.$Proxy117.findOne(Unknown Source)
at com.smartobjectsecurity.common.service.thing.ThingGroupServiceImpl.find(ThingGroupServiceImpl.java:62)
at com.smartobjectsecurity.common.service.thing.ThingGroupServiceImpl.find(ThingGroupServiceImpl.java:1)
at com.smartobjectsecurity.common.service.GenericServiceImpl.find(GenericServiceImpl.java:1)
at com.smartobjectsecurity.common.service.GenericServiceImpl$$FastClassBySpringCGLIB$$daaa7267.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:717)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653)
at com.smartobjectsecurity.common.service.thing.ThingGroupServiceImpl$$EnhancerBySpringCGLIB$$aa452fd7.find(<generated>)
at com.smartobjectsecurity.common.service.rule.RuleServiceImpl.update(RuleServiceImpl.java:219)
以下是ruleService update
方法
首先,我需要使用reactionGroup服务更新关联的反应组。
@Transactional
public Rule update(final Rule rule, User user) throws UnknownEntityException, MyBadRequestException {
final Long id = rule.getId();
Rule found = null;
try {
found = find(id, user);
found.setEvents(rule.getEvents());
thingGroupService.find(rule.getThingGroup().getId(), user);
found.setThingGroup(rule.getThingGroup());
found = dao.saveAndFlush(found);
} catch (final UnknownEntityException e) {
final UnknownEntityException ex = new UnknownEntityException("UnknownEntity/ResourceException.rule", "update_unknown_rule");
ex.setParameters(new Object[] { id });
throw ex;
}
return found;
}
@Override
@Transactional(rollbackFor = UnknownEntityException.class )
public ThingGroup find(final Long id, User user) throws UnknownEntityException {
logger.debug("-> find, id = " + id);
final ThingGroup found = getDao().findOne(buildSpecificationForIdAndUser(id, user));
if (found == null) {
final UnknownEntityException ex = new UnknownEntityException("UnknownEntity/ResourceException.thingGroup", "unknown_thingGroup");
ex.setParameters(new Object[] { id });
throw ex;
}
logger.debug("<- find : " + found);
return found;
}
此处要求的是buildSpecificationForIdAndUser
和buildSpecificationForUser
方法
它们用于根据用户的权限构建搜索限制。
@Transactional
protected Specification<ENTITY> buildSpecificationForIdAndUser(final ID id,final User user){
return new Specification<ENTITY>() {
@Override
public Predicate toPredicate(Root<ENTITY> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
Expression<Long> entityId = root.get("id");
Predicate userPredicate = buildSpecificationForUser(user).toPredicate(root, query, builder);
return builder.and(
builder.equal(entityId, id),
userPredicate
);
}
};
}
@Override
@Transactional
protected Specification<ThingGroup> buildSpecificationForUser(final User user) {
return new Specification<ThingGroup>() {
@Override
public Predicate toPredicate(Root<ThingGroup> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
Expression<Collection<User>> managersOfThingGroup = root.get("managers");
Expression<Company> thingGroupCompany = root.get("company");
Predicate isManager = builder.isMember(user, managersOfThingGroup);
Predicate isSameCompany = builder.equal(thingGroupCompany,user.getCompany());
return builder.and(isSameCompany,isManager);
}
};
}
当试图运行thingGroupService.find(rule.getThingGroup().getId(), user);
时,Hibernate突然抛出关于事件entoty的异常(传递给持久化的分离实体)。
我真的不知道这里有什么问题
我一直在搜索各种论坛几天而没有找到我的问题的原因。
为什么Event
实体在ThingGroup
资源上运行查找后突然从会话中脱离,该资源无事件发生?
答案 0 :(得分:4)
您不必为已经附加的实体调用saveAndFlush
,因此应将服务方法更改为:
found = find(id, user);
thingGroupService.find(rule.getThingGroup().getId(), user);
found.setThingGroup(rule.getThingGroup());
found.setEvents(rule.getEvents());
found
实体已与当前Session
相关联,因此dirty checking mechanism会检测到所有更改,如果启用了级联,则会传播子entity state transitions。
答案 1 :(得分:3)
我设法解决了这个问题 但是我不明白为什么它现在有效,我还在调查 我只是颠倒了两行代码,Hibernate停止抛出分离的实体异常。
现在我有:
found.setEvents(rule.getEvents());
thingGroupService.find(rule.getThingGroup().getId(), user);
而不是:
thingGroupService.find(rule.getThingGroup().getId(), user);
found.setEvents(rule.getEvents());
也许Hibernate会在某些时候自动刷新,但我不确定为什么它已经解决了这个问题。
答案 2 :(得分:1)
这就是我要求实施buildSpecification
方法的原因。我想检查查询是否已执行,或Session
get
/ load
方法是否按ID检索实体。
基本上,如果刷新模式为AUTO
(默认),Hibernate有时会在查询执行之前刷新会话,以避免查询过时数据。 FlushMode.AUTO的Javadoc:
有时在查询执行之前刷新
Session
确保查询永远不会返回失效状态。这是默认值 冲洗模式。
如果您有从托管实体引用的分离实体,那么您将获得异常。
解决方案是在查询执行之前正确地重新附加实体,或者将受影响的事务的刷新模式更改为COMMIT
:
entityManager.setFlushMode(FlushMode.COMMIT)