事件监听程序在Spring 3.1.0.release中使用Hibernate 4.0? [英] eventlisteners using hibernate 4.0 with spring 3.1.0.release?

查看:114
本文介绍了事件监听程序在Spring 3.1.0.release中使用Hibernate 4.0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这些jar都是新发布的,并且具有针对Java EE应用程序的最新解决方案.但是我在hibernate.cfg.xml中指定休眠侦听器时遇到问题.

These jars are both new released and have the latest solutions for Java EE applications. But I have a problem on specifiying hibernate listeners in hibernate.cfg.xml.

在春季3.1.0之前,LocalSessionFactroyBean拥有一个保留事件侦听器的属性.但是对于3.1.0.release,没有事件监听器映射.现在,我无法在saveorupdate,postload等上跟踪模态对象,因为它们不是由Spring配置的.您有解决此问题的想法吗?

Before spring 3.1.0, LocalSessionFactroyBean was holding an attribute that keeps eventlisteners. But with 3.1.0.release there is no eventlisteners map. Now I fail keeping the track of modal objects on saveorupdate, postload etc. because they are not configured by Spring. Do you have an idea to solve this issue?

推荐答案

我遇到了同样令人沮丧的问题. Hibernate 4似乎从根本上改变了您注册事件的方式,而Spring组尚未赶上.这是我的基于注释的解决方案,使用init方法注册侦听器:

I had the same frustrating problem. Hibernate 4 appears to have fundamentally changed the way you register for events and the Spring group has not yet caught up. Here's my annotation-based solution using an init method to register a listener:

@Component
public class HibernateEventWiring {

    @Autowired
    private SessionFactory sessionFactory;

    @Autowired
    private SomeHibernateListener listener;

    @PostConstruct
    public void registerListeners() {
        EventListenerRegistry registry = ((SessionFactoryImpl) sessionFactory).getServiceRegistry().getService(
        EventListenerRegistry.class);
        registry.getEventListenerGroup(EventType.POST_COMMIT_INSERT).appendListener(listener);
        registry.getEventListenerGroup(EventType.POST_COMMIT_UPDATE).appendListener(listener);
    }
}

拦截器是另一种不错的方法,但是错误地放弃了对拦截器的支持: https://jira.springsource.org/browse/SPR-8940

An interceptor would be another fine approach, but support for interceptors was mistakenly dropped: https://jira.springsource.org/browse/SPR-8940

这篇关于事件监听程序在Spring 3.1.0.release中使用Hibernate 4.0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆