我正在为现有的hibernate实体添加envers.到目前为止,审计工作一切顺利,但查询是一个不同的问题,因为修订表没有填充现有数据.还有其他人已经解决了这个问题吗?也许您已经找到了一些方法来使用现有表填充修订表?我想我会问,我相信其他人会发现它很有用.
我正在使用spring + hibernate.我所有的HibernateDAO直接使用sessionFactory.
我有应用层 - >服务层 - > DAO层,并且所有集合都是懒惰加载的.
所以,问题是在应用层(包含GUI/swing)的某个时候我使用服务层方法(包含@Transactional注释)加载实体,我想使用这个对象的lazly属性,但是很明显会话已经关闭.
解决这个问题的最佳方法是什么?
编辑
我尝试使用MethodInterceptor,我的想法是为我的所有实体编写一个AroundAdvice并使用注释,例如:
// Custom annotation, say that session is required for this method
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface SessionRequired {
// An AroundAdvice to intercept method calls
public class SessionInterceptor implements MethodInterceptor {
public Object invoke(MethodInvocation mi) throws Throwable {
bool sessionRequired=mi.getMethod().isAnnotationPresent(SessionRequired.class);
// Begin and commit session only if @SessionRequired
if(sessionRequired){
// begin transaction here
}
Object ret=mi.proceed();
if(sessionRequired){
// commit transaction here
}
return ret;
}
}
// An …Run Code Online (Sandbox Code Playgroud)