Eventlisteners Using Hibernate 4.0 with Spring 3.1.0.Release

eventlisteners using hibernate 4.0 with spring 3.1.0.release?

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);
}
}

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

Hibernate 4.1 with Spring 3.1.0 unable to register event listeners in web application

For now, I've used the other idea mentioned in the thread which is a code based approach:

@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);
}

A config based approach, however will be more helpful.

How do I set event listeners for my Spring 3 / Hibernate 4 LocalSessionFactoryBean?

Hibernate4 changed the way EventListeners are registered and doesn't provide a setter for EventListeners map anymore. There's different solutions around for dealing with this, have a look at this thread:

eventlisteners using hibernate 4.0 with spring 3.1.0.release?

Hibernate - since JPA Listener activation I get IllegalArgumentException: Removing a detached instance

I spent some hours but I got it not working with the JPA Listener "org.hibernate.jpa.event.spi.JpaIntegrator". After studying the following links

  • what-is-the-proper-way-to-re-attach-detached-objects-in-hibernate
  • what-are-the-differences-between-the-different-saving-methods-in-hibernate
  • transitive objectstate

I came to the conclusion that this is mostlikely a bug of Hibernate, because "saveOrUpdate()" should exaclty handle such situations which it also does if I not use the "JpaIntegrator" stuff. I tried also with Hibernate 4.3.11, same issue.

A solution for me was rather using the special JPA Listener activation the Hibernate Event Listener

e.g.

@Component
@Log4j2
public class HibernateEventListener implements PostCommitDeleteEventListener {

@Autowired
@Qualifier("hibernateOneERPSessionFactory")
private SessionFactory sessionFactory;

@Autowired
private ThreadPoolTaskExecutor taskExecutor;

@Autowired
private TranslationDAO translationDAO;

@PostConstruct
public void registerListeners() {

taskExecutor.setWaitForTasksToCompleteOnShutdown(true);
taskExecutor.setAwaitTerminationSeconds(Integer.MAX_VALUE);

final EventListenerRegistry registry = ((SessionFactoryImpl) sessionFactory)
.getServiceRegistry().getService(EventListenerRegistry.class);
registry.appendListeners(EventType.POST_COMMIT_DELETE, this);
}

@Override
public void onPostDelete(PostDeleteEvent event) {
Object obj = event.getEntity();
if(obj instanceof Translateable) {
Translateable translateable = (Translateable)obj;
if (translateable.hasTranslation()) {
Runnable task = new Runnable() {
public void run() {
translationDAO.deleteUnusedTranslations();
}
};
taskExecutor.initialize();
taskExecutor.execute(task);
taskExecutor.shutdown();
}
}
}
}

Custom Listener Definition in Hibernate

Hibernate 4.x do not have setListener method, this method is available in Hibernate 3.x version only.

In Hibernate 4.x you can use interceptors, go through this link for details:

Chapter 14. Interceptors and events
Hibernate interceptor example

This SO post has some details on how to use Listeners in Hibernate 4.x, you can have a look at this:

eventlisteners using hibernate 4.0 with spring 3.1.0.release?



Related Topics



Leave a reply



Submit