gpt4 book ai didi

java.lang.IllegalStateException : No value for key [org. hibernate.impl.SessionFactoryImpl] 与 AOP

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:04:49 27 4
gpt4 key购买 nike

我每次使用 Hibernate 更新实体时都会出错。这是我的故事:

我有一个带有注解@Transactional 的服务,并且正在调用 DAO.update 方法。

@Service
@Transactional
public class FooServiceImpl implements FooService {
@Inject
private FooDAO fooDAO;

@Override
public Foo update(Foo foo) {
return fooDAO.update(foo);
}

@Override
public Foo find(int fooId) {
return fooDAO.findById(fooId);
}
}

我有一个 AOP,它捕获 DAO.update 以进行一些操作。

@Aspect
@Order(2)
@Component
public class FooTackerAspect {
@Inject
private FooService fooService;

@Inject
private SessionFactoryManager sessionFactoryManager;

@Pointcut("execution(* com.foo.bar.FooService.update(..))")
public void methodToTrack() {}

@Around("methodToTrack")
public Object track(final ProceedingJoinPoint joinPoint) throws Throwable {
int fooId = fetchIdFromJoinPoint(joinPoint);
Foo foo = fooService.find(fooId);

// Do some operations

// I close the session in order to avoid the exception
// org.hibernate.NonUniqueObjectException: a different object with
// the same identifier value was already associated with the session
sessionFactoryManager.getSessionFactory(DataSource.FOO)
.getCurrentSession().close();

Object retVal = joinPoint.proceed();
return retVal;
}
}

但是,在更新实体 (fooDAO.update(foo)) 之后,出现以下错误:

2056953 [http-bio-8080-exec-4] ERROR o.s.t.s.TransactionSynchronizationUtils - TransactionSynchronization.beforeCompletion threw exception 
java.lang.IllegalStateException: No value for key [org.hibernate.impl.SessionFactoryImpl@762d6271] bound to thread [http-bio-8080-exec-4]
at org.springframework.transaction.support.TransactionSynchronizationManager.unbindResource(TransactionSynchronizationManager.java:209) ~[spring-tx-3.2.4.RELEASE.jar:3.2.4.RELEASE]
at org.springframework.orm.hibernate3.SpringSessionSynchronization.beforeCompletion(SpringSessionSynchronization.java:193) ~[spring-orm-3.2.4.RELEASE.jar:3.2.4.RELEASE]
at org.springframework.transaction.support.TransactionSynchronizationUtils.triggerBeforeCompletion(TransactionSynchronizationUtils.java:106) ~[spring-tx-3.2.4.RELEASE.jar:3.2.4.RELEASE]
at org.springframework.transaction.support.AbstractPlatformTransactionManager.triggerBeforeCompletion(AbstractPlatformTransactionManager.java:938) [spring-tx-3.2.4.RELEASE.jar:3.2.4.RELEASE]
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:739) [spring-tx-3.2.4.RELEASE.jar:3.2.4.RELEASE]
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:724) [spring-tx-3.2.4.RELEASE.jar:3.2.4.RELEASE]
at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:475) [spring-tx-3.2.4.RELEASE.jar:3.2.4.RELEASE]
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:270) [spring-tx-3.2.4.RELEASE.jar:3.2.4.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94) [spring-tx-3.2.4.RELEASE.jar:3.2.4.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) [spring-aop-3.2.4.RELEASE.jar:3.2.4.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204) [spring-aop-3.2.4.RELEASE.jar:3.2.4.RELEASE]
at com.sun.proxy.$Proxy112.update(Unknown Source) [$Proxy112.class:na]

这个错误没有阻塞(更新仍然通过)。但我想纠正这个错误。你知道我该怎么做吗?

我认为错误来自于我关闭了 session 。但我必须关闭它才能更新实体。

最佳答案

正如@kriegaex 所说,问题出在我使用同一个 session 执行查找更新 操作。

根据这些信息,我使用以下解决方法(有效):我使用两个单独的 session ,一个用于查找,另一个用于更新

@Repository
public class FooDAOImpl implements FooDAO {
private FooHibernateDaoSupport hibernateDaoSupport;

public FooDAOImpl() {
// Init DaoSupport
}

@Override
public Foo findById(final Serializable fooId) {
StatelessSession session = hibernateDaoSupport.getSessionFactory().openStatelessSession();
Foo foo = (Foo) session.get(Foo.class, fooId);
session.close;
return foo;
}

@Override
public Foo update(Foo foo) {
getHibernateTemplate().update(entity);
return entity;
}
}

使用此解决方案,不再需要在我的建议代码中关闭 session 。

注意:我正在打开一个无状态 session ,因为在我的项目中 FooService.update 也调用了 FooService.find 来设置一些属性,所以我不想在我的建议代码中进行修改。

关于java.lang.IllegalStateException : No value for key [org. hibernate.impl.SessionFactoryImpl] 与 AOP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23605124/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com