java 如何在 Hibernate 4 实体上设置事件侦听器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10602342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 01:48:06  来源:igfitidea点击:

How do I set up an event listener on a Hibernate 4 entity?

javaspringhibernatelistener

提问by Dave

I'm using Spring 3.1.0.RELEASE with Hibernate 4.0.1.Final. I want to invoke an event listener on my entity bean when it is loaded from the DB, but I can't figure out what event I should be using. I load my entities in my DAO like so

我正在使用 Spring 3.1.0.RELEASE 和 Hibernate 4.0.1.Final。我想在从数据库加载实体 bean 时调用它的事件侦听器,但我不知道应该使用什么事件。我像这样在我的 DAO 中加载我的实体

@Repository("eventFeedsDao")
public class EventFeedsDaoImpl implements EventFeedsDao {

    ...
    @Autowired
    private SessionFactory sessionFactory;

    ...

    @Override
    public EventFeed findById(final Integer id) { 
        EventFeed eventFeed = null;
        final Session session = sessionFactory.getCurrentSession();
        final Criteria crit = session.createCriteria(EventFeed.class).add(Restrictions.eq("id", id));
        final List<EventFeed> results = crit.list();
        if (results != null && results.size() > 0) { 
            eventFeed = results.get(0);
        }   // if
        return eventFeed;       
    }   // findById 

Here is how I'm trying to set up my event wiring ...

这是我尝试设置事件连接的方式...

@Component
public class HibernateEventWiring {

    @Autowired
    private SessionFactory sessionFactory;

    @Autowired
    private EventMavenLoadListener listener;

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

and my listener class is as follows ...

我的听众课程如下......

@Component
public class EventMavenLoadListener implements LoadEventListener {

    ...

    @Override
    public void onLoad(final LoadEvent event, final LoadType loadType) throws HibernateException {
        if(EventFeed.class.getName().equals(event.getEntityClassName())){
            EventFeed entity = (EventFeed) event.getInstanceToLoad();
            entity.setNetUtilsService(netUtilsService);
        }   // if
    }

}

but the "onLoad" event is never called. What am I doing wrong? Should I be using another event?

但“onLoad”事件从未被调用。我究竟做错了什么?我应该使用另一个事件吗?

回答by Dave

For some reason that still eludes me, the EventType.PRE_LOAD event type was the way to go. This worked.

出于某种我仍然无法理解的原因,EventType.PRE_LOAD 事件类型是要走的路。这奏效了。

@Component
public class HibernateEventWiring {

    @Autowired
    private SessionFactory sessionFactory;

    @Autowired
    private EventMavenLoadListener listener;

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

回答by Steve Ebersole

The load event is only triggered when loading an entity individually (session.get, initializing a proxy, fetching eager to-one associations).

load 事件仅在单独加载实体时触发(session.get、初始化代理、获取渴望的关联)。

The pre and post load events however follow JPA defined behavior and occur before and after the entity data is loaded regardless of how that "data load" occurred.

然而,加载前和加载后事件遵循 JPA 定义的行为,并在实体数据加载之前和之后发生,而不管“数据加载”是如何发生的。

Its a case of bad name collision.

它是一个坏名冲突的案例。

The preferred way in Hibernate 4, btw, to specify event listeners is through the use of an org.hibernate.integrator.spi.Integrator. See http://docs.jboss.org/hibernate/orm/4.1/devguide/en-US/html_single/#integrators

顺便说一下,在 Hibernate 4 中指定事件侦听器的首选方法是使用 org.hibernate.integrator.spi.Integrator。见http://docs.jboss.org/hibernate/orm/4.1/devguide/en-US/html_single/#integrators