I'm trying to implement some event listeners in order to log the history of some entities in my java EE application. I followed all steps suggested by https://vladmihalcea.com/hibernate-event-listeners/ but couldn't manage it to work.
Update listener:
public class HistoryUpdateEventListener implements PostUpdateEventListener {
private static final long serialVersionUID = 1L;
private static final Logger LOGGER = LoggerFactory.getLogger( HistoryUpdateEventListener.class );
public static final HistoryUpdateEventListener INSTANCE = new HistoryUpdateEventListener();
@Override
public void onPostUpdate( PostUpdateEvent event ) {
// post update logic
}
@Override
public boolean requiresPostCommitHanding(EntityPersister persister) {
return false;
}
}
Listener integrator:
public class HistoryEventListenerIntegrator implements Integrator {
@Override
public void integrate( Metadata metadata, SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry ) {
final EventListenerRegistry eventListenerRegistry = serviceRegistry.getService(EventListenerRegistry.class);
eventListenerRegistry.appendListeners(EventType.POST_UPDATE, HistoryUpdateEventListener.INSTANCE);
}
@Override
public void disintegrate( SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry ) {
}
}
Persistence xml
<properties>
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="hibernate.integrator_provider" value="com.app.HistoryEventListenerIntegrator" />
<property name="hibernate.hbm2dll.extra_physical_table_types" value="MATERIALIZED VIEW"/>
<property name="javax.persistence.schema-generation.database.action" value="update" />
</properties>
Model class:
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@Entity
@Table( name = "PESSOA" )
public class Pessoa {
@Id
@GeneratedValue( strategy = GenerationType.AUTO )
@Column( name = "ID_PESSOA" )
private Long id;
@Column( name = "NOME" )
private String nome;
@Column( name = "NOME_SOCIAL" )
private String nomeSocial;
@Column( name = "CPF" )
private String cpf;
}
I added several breakpoints and tested my application but the method onPostUpdate is never being called. My question is, am I missing any configuration?