Spring Data Hibernate Event

Library for spring data that binds annotated methods to listen to hibernate events.

License

License

Categories

Categories

Data Hibernate ORM
GroupId

GroupId

io.github.teastman
ArtifactId

ArtifactId

spring-data-hibernate-event
Last Version

Last Version

1.0.1
Release Date

Release Date

Type

Type

jar
Description

Description

Spring Data Hibernate Event
Library for spring data that binds annotated methods to listen to hibernate events.
Project URL

Project URL

https://github.com/teastman/spring-data-hibernate-event
Source Code Management

Source Code Management

https://github.com/teastman/spring-data-hibernate-event

Download spring-data-hibernate-event

How to add to project

<!-- https://jarcasting.com/artifacts/io.github.teastman/spring-data-hibernate-event/ -->
<dependency>
    <groupId>io.github.teastman</groupId>
    <artifactId>spring-data-hibernate-event</artifactId>
    <version>1.0.1</version>
</dependency>
// https://jarcasting.com/artifacts/io.github.teastman/spring-data-hibernate-event/
implementation 'io.github.teastman:spring-data-hibernate-event:1.0.1'
// https://jarcasting.com/artifacts/io.github.teastman/spring-data-hibernate-event/
implementation ("io.github.teastman:spring-data-hibernate-event:1.0.1")
'io.github.teastman:spring-data-hibernate-event:jar:1.0.1'
<dependency org="io.github.teastman" name="spring-data-hibernate-event" rev="1.0.1">
  <artifact name="spring-data-hibernate-event" type="jar" />
</dependency>
@Grapes(
@Grab(group='io.github.teastman', module='spring-data-hibernate-event', version='1.0.1')
)
libraryDependencies += "io.github.teastman" % "spring-data-hibernate-event" % "1.0.1"
[io.github.teastman/spring-data-hibernate-event "1.0.1"]

Dependencies

compile (2)

Group / Artifact Type Version
org.springframework.boot : spring-boot-autoconfigure jar 2.0.6.RELEASE
org.hibernate : hibernate-core jar 5.3.7.Final

test (4)

Group / Artifact Type Version
org.springframework.boot : spring-boot-starter-test jar 2.0.6.RELEASE
org.springframework.boot : spring-boot-starter-data-jpa jar 2.0.6.RELEASE
com.h2database : h2 jar 1.4.197
javax.xml.bind : jaxb-api jar 2.3.0

Project Modules

There are no modules declared in this project.

Spring Data Hibernate Event

The primary goal of the Spring Data Hibernate Event project is to make it easier to listen for Hibernate spi events that affect given entities via the use of an annotation.

Dependency

Download the jar through Maven:

<dependency>
    <groupId>io.github.teastman</groupId>
    <artifactId>spring-data-hibernate-event</artifactId>
    <version>1.0.1</version>
</dependency>

Configuration

Spring auto configurations are used to listen for Hibernate events on the EntityManagerFactory. So if you are using spring auto configurations you can skip the next step.

If you are NOT using auto configurations, you can manually add the required configuration by adding an @Import to another configuration.

@Configuration
@Import(HibernateEventAutoConfiguration.class)
class AppConfig {
...

Or you can override the default configuration behaviour by creating your own AnnotatedHibernateEventListenerInvoker bean .

@Configuration
class AppConfig {
 
    @Bean
    public AnnotatedHibernateEventListenerInvoker annotatedHibernateEventHandlerInvoker() {
        AnnotatedHibernateEventListenerInvoker invoker = new AnnotatedHibernateEventListenerInvoker();
        SessionFactoryImplementor sessionFactory = entityManagerFactory.unwrap(SessionFactoryImplementor.class);
        EventListenerRegistry registry = sessionFactory.getServiceRegistry().getService(EventListenerRegistry.class);
        registry.prependListeners(EventType.PRE_UPDATE, invoker);
        registry.prependListeners(EventType.PRE_DELETE, invoker);
        registry.prependListeners(EventType.PRE_INSERT, invoker);
        ...
        return invoker;
    }
}

Utilization

You can now use the @HibernateEventListener annotation to bind methods to listen for Hibernate events.

@Service
public class MyListenerService {

    @HibernateEventListener
    public void handlePreInsert(MyEntity entity, PreInsertEvent event){
        // This will be called any time an entity of type MyEntity is first saved.
    }
}

To check if a specific field changed in an entity you can use the static utility function getPropertyIndex.

@HibernateEventListener
public void onUpdate(MyEntity entity, PreUpdateEvent event) {
    int index = getPropertyIndex(event, "name");
    if (event.getOldState()[index] != event.getState()[index]) {
        // The name changed.
    }
}

Event Types

The valid event types are:

  • SaveOrUpdateEvent
  • PreInsertEvent
  • PreUpdateEvent
  • PreDeleteEvent
  • PreCollectionRecreateEvent
  • PreCollectionUpdateEvent
  • PreCollectionRemoveEvent
  • PostInsertEvent
  • PostUpdateEvent
  • PostDeleteEvent
  • PostCollectionRecreateEvent
  • PostCollectionUpdateEvent
  • PostCollectionRemoveEvent

Versions

Version
1.0.1
1.0.0