Last active
August 23, 2016 12:17
-
-
Save pedroxs/9b9009e79f1a01c13365ac431629f33c to your computer and use it in GitHub Desktop.
How to register Hibernate events using JavaConfig so it is possible to use Spring Managed Beans
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component | |
public class CustomEventHandler implements PreInsertEventListener, PreUpdateEventListener, PostInsertEventListener, PostUpdateEventListener { | |
private int someProperty; | |
@Value("${some-property:20}") // Also possible to use @Autowire if needed =) | |
public void setSomeProperty(int someProperty) { | |
this.someProperty = someProperty; | |
} | |
@Override | |
public boolean onPreInsert(PreInsertEvent event) { | |
Object entity = event.getEntity(); | |
if (entity instanceof MyEntity) { // this is necessary because events are triggered to all entities | |
// do what is needed here for entity of type MyEntity.class | |
} | |
return false; | |
} | |
@Override | |
public boolean onPreUpdate(PreUpdateEvent event) { | |
Object entity = event.getEntity(); | |
if (entity instanceof MyEntity) { // this is necessary because events are triggered to all entities | |
// do what is needed here for entity of type MyEntity.class | |
} | |
return false; | |
} | |
@Override | |
public void onPostInsert(PostInsertEvent event) { | |
Object entity = event.getEntity(); | |
if (entity instanceof MyEntity) { // this is necessary because events are triggered to all entities | |
// do what is needed here for entity of type MyEntity.class | |
} | |
} | |
@Override | |
public void onPostUpdate(PostUpdateEvent event) { | |
Object entity = event.getEntity(); | |
if (entity instanceof MyEntity) { // this is necessary because events are triggered to all entities | |
// do what is needed here for entity of type MyEntity.class | |
} | |
} | |
@Override | |
public boolean requiresPostCommitHanding(EntityPersister persister) { | |
return false; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Configuration | |
public class HibernateConfig { | |
/** | |
* This is needed so we have access to Hibernate SessionFactory, not exposed by default. | |
*/ | |
@Bean | |
public HibernateJpaSessionFactoryBean sessionFactory(EntityManagerFactory entityManagerFactory) { | |
HibernateJpaSessionFactoryBean hibernateJpaSessionFactoryBean = new HibernateJpaSessionFactoryBean(); | |
hibernateJpaSessionFactoryBean.setEntityManagerFactory(entityManagerFactory); | |
return hibernateJpaSessionFactoryBean; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component | |
public class RegisterHibernateEvents { | |
/** | |
* Here we get a hold of the SessionFactory and the event we want to register as a managed bean. | |
* Be careful that the events are fired for all entities. | |
*/ | |
@Autowired | |
public void eventRegistry(SessionFactory sessionFactory, CustomEventHandler customEventHandler) { | |
EventListenerRegistry registry = ((SessionFactoryImpl) sessionFactory).getServiceRegistry().getService(EventListenerRegistry.class); | |
registry.appendListeners(EventType.PRE_INSERT, customEventHandler); | |
registry.appendListeners(EventType.PRE_UPDATE, customEventHandler); | |
registry.appendListeners(EventType.POST_INSERT, customEventHandler); | |
registry.appendListeners(EventType.POST_UPDATE, customEventHandler); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment