Spring Data Hibernate Event

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

Лицензия

Лицензия

Категории

Категории

Данные Hibernate ORM
Группа

Группа

io.github.teastman
Идентификатор

Идентификатор

spring-data-hibernate-event
Последняя версия

Последняя версия

1.0.1
Дата

Дата

Тип

Тип

jar
Описание

Описание

Spring Data Hibernate Event
Library for spring data that binds annotated methods to listen to hibernate events.
Ссылка на сайт

Ссылка на сайт

https://github.com/teastman/spring-data-hibernate-event
Система контроля версий

Система контроля версий

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

Скачать spring-data-hibernate-event

Как подключить последнюю версию

<!-- 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"]

Зависимости

compile (2)

Идентификатор библиотеки Тип Версия
org.springframework.boot : spring-boot-autoconfigure jar 2.0.6.RELEASE
org.hibernate : hibernate-core jar 5.3.7.Final

test (4)

Идентификатор библиотеки Тип Версия
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

Модули Проекта

Данный проект не имеет модулей.

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

Версии библиотеки

Версия
1.0.1
1.0.0