Hibernate/JPA - Entity listener not being called properly

后端 未结 3 577
悲哀的现实
悲哀的现实 2021-01-02 19:52

I\'m trying to leverage EntityListener objects and callback methods within my Seam/Hibernate/JPA application. I\'m using a Seam 2.2-managed persistence context on JBoss 5.1

3条回答
  •  生来不讨喜
    2021-01-02 20:40

    Sorry if i give a wrong answer... i don't know Seam.

    But in your subject you say "Hibernate/JPA" which is unclear. Do you persist entities using a Session from a SessionFactory, or an EntityManager from an EntityManagerFactory?

    There is a big difference because if you use Seam with a SessionFactory, the big difference is that by default the JPA listeners (which fire your annotated callbacks) are not registred by default, while they are with an EntityManagerFactory. So it may be possible that you are using a SessionFactory, and that someone else on your project, who configured that session factory, only registered a subset of all the JPA callback listeners.

    See: http://docs.jboss.org/hibernate/entitymanager/3.5/reference/en/html/configuration.html#d0e865


    Edit: Ok sorry, you use an EntityManager...

    but perhaps it would be a good idea to try to get that SessionFactory behind the EntityManagerFactory, and see which event listeners are registred. Are you alone on that application? If someone tried to register a custom/legacy eventlistener or something, he may have overriden a JPA eventlistener.

    This can be achieved by something like that:

    EntityManager em = ...
    Session session = (Session)em.getDelegage()
    SessionFactoryImpl sessionFactoryImpl = (SessionFactoryImpl)session.getSessionFactory();
    EventListeners el = sessionFactoryImpl.getEventListeners()
    

    And then you can look what's inside, for exemple, according to your problem, you can compare:

    el.getPreLoadEventListeners()
    el.getPreDeleteEventListeners()
    

    And remember the "default behaviour" is: http://docs.jboss.org/hibernate/stable/entitymanager/reference/en/html_single/#d0e865

    It seems that it is possible to easily override JPA default listeners, see that: http://docs.jboss.org/hibernate/entitymanager/3.6/reference/en/html/listeners.html

    It would be nice if you show us your persistence.xml

提交回复
热议问题