HHH-18049 - Handle <exclude-default-listeners/> and <exclude-superclass-listeners/>

This commit is contained in:
Steve Ebersole 2024-05-01 10:26:08 -05:00
parent 7b10051a75
commit c7a15a835d
22 changed files with 705 additions and 88 deletions

View File

@ -844,19 +844,12 @@ public class ManagedTypeProcessor {
}
private static void processEntityOrMappedSuperclass(
JaxbEntityOrMappedSuperclass jaxbEntity,
JaxbEntityOrMappedSuperclass jaxbClass,
MutableClassDetails classDetails,
XmlDocumentContext xmlDocumentContext) {
XmlAnnotationHelper.applyIdClass( jaxbEntity.getIdClass(), classDetails, xmlDocumentContext );
XmlAnnotationHelper.applyIdClass( jaxbClass.getIdClass(), classDetails, xmlDocumentContext );
XmlAnnotationHelper.applyLifecycleCallbacks(
jaxbEntity,
JpaEventListenerStyle.CALLBACK,
classDetails,
xmlDocumentContext
);
XmlAnnotationHelper.applyEntityListeners( jaxbEntity.getEntityListenerContainer(), classDetails, xmlDocumentContext );
XmlAnnotationHelper.applyLifecycleCallbacks( jaxbClass, classDetails, xmlDocumentContext );
}
public static void processCompleteEmbeddable(

View File

@ -52,6 +52,7 @@ import org.hibernate.boot.jaxb.mapping.spi.JaxbElementCollectionImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbEntity;
import org.hibernate.boot.jaxb.mapping.spi.JaxbEntityListenerContainerImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbEntityListenerImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbEntityOrMappedSuperclass;
import org.hibernate.boot.jaxb.mapping.spi.JaxbGeneratedValueImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbHbmFilterImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbIdClassImpl;
@ -83,6 +84,7 @@ import org.hibernate.boot.models.xml.internal.db.JoinColumnProcessing;
import org.hibernate.boot.models.xml.internal.db.TableProcessing;
import org.hibernate.boot.models.xml.spi.XmlDocument;
import org.hibernate.boot.models.xml.spi.XmlDocumentContext;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.engine.spi.ExecuteUpdateResultCheckStyle;
import org.hibernate.internal.util.KeyedConsumer;
import org.hibernate.internal.util.StringHelper;
@ -1179,6 +1181,25 @@ public class XmlAnnotationHelper {
idClassAnn.setAttributeValue( "value", idClassImpl );
}
public static void applyLifecycleCallbacks(
JaxbEntityOrMappedSuperclass jaxbClass,
MutableClassDetails classDetails,
XmlDocumentContext xmlDocumentContext) {
final SourceModelBuildingContext modelBuildingContext = xmlDocumentContext.getModelBuildingContext();
if ( jaxbClass.getExcludeDefaultListeners() != null ) {
classDetails.applyAnnotationUsage( JpaAnnotations.EXCLUDE_DEFAULT_LISTENERS, modelBuildingContext );
}
if ( jaxbClass.getExcludeSuperclassListeners() != null ) {
classDetails.applyAnnotationUsage( JpaAnnotations.EXCLUDE_SUPERCLASS_LISTENERS, modelBuildingContext );
}
applyLifecycleCallbacks( jaxbClass, JpaEventListenerStyle.CALLBACK, classDetails, xmlDocumentContext );
applyEntityListeners( jaxbClass.getEntityListenerContainer(), classDetails, xmlDocumentContext );
}
public static void applyEntityListeners(
JaxbEntityListenerContainerImpl entityListenerContainer,
MutableClassDetails classDetails,

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html.
*/
package org.hibernate.orm.test.jpa.compliance.callback.listeneroverrides;
package org.hibernate.orm.test.jpa.callbacks.xml.common;
import java.util.ArrayList;
import java.util.List;

View File

@ -0,0 +1,61 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html.
*/
package org.hibernate.orm.test.jpa.callbacks.xml.complete;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
/**
* @author Steve Ebersole
*/
@Entity
public class LineItem extends LineItemSuper {
@Id
private Integer id;
@ManyToOne
@JoinColumn(name = "order_fk")
private Order order;
@ManyToOne
@JoinColumn(name = "product_fk")
private Product product;
public LineItem() {
}
public LineItem(Integer id, Order order, Product product, int quantity) {
super( quantity );
this.id = id;
this.order = order;
this.product = product;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Order getOrder() {
return order;
}
public void setOrder(Order order) {
this.order = order;
}
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
}

View File

@ -4,16 +4,16 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html.
*/
package org.hibernate.orm.test.jpa.compliance.callback.listeneroverrides;
package org.hibernate.orm.test.jpa.callbacks.xml.complete;
import org.hibernate.orm.test.jpa.callbacks.xml.common.CallbackTarget;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.MappedSuperclass;
/**
* @author Steve Ebersole
*/
@MappedSuperclass
@EntityListeners({ListenerA.class, ListenerB.class})
public class LineItemSuper extends CallbackTarget {
private int quantity;

View File

@ -0,0 +1,45 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html.
*/
package org.hibernate.orm.test.jpa.callbacks.xml.complete;
import org.hibernate.orm.test.jpa.callbacks.xml.common.CallbackTarget;
/**
* @author Steve Ebersole
*/
public class ListenerA {
public static final String NAME = "ListenerA";
protected void prePersist(CallbackTarget target) {
target.prePersistCalled( NAME );
}
protected void postPersist(CallbackTarget target) {
target.postPersistCalled( NAME );
}
protected void preRemove(CallbackTarget target) {
target.preRemoveCalled( NAME );
}
protected void postRemove(CallbackTarget target) {
target.postRemoveCalled( NAME );
}
protected void preUpdate(CallbackTarget target) {
target.preUpdateCalled( NAME );
}
protected void postUpdate(CallbackTarget target) {
target.postUpdateCalled( NAME );
}
protected void postLoad(CallbackTarget target) {
target.postLoadCalled( NAME );
}
}

View File

@ -0,0 +1,44 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html.
*/
package org.hibernate.orm.test.jpa.callbacks.xml.complete;
import org.hibernate.orm.test.jpa.callbacks.xml.common.CallbackTarget;
/**
* @author Steve Ebersole
*/
public class ListenerB {
public static final String NAME = "ListenerB";
protected void prePersist(CallbackTarget target) {
target.prePersistCalled( NAME );
}
protected void postPersist(CallbackTarget target) {
target.postPersistCalled( NAME );
}
protected void preRemove(CallbackTarget target) {
target.preRemoveCalled( NAME );
}
protected void postRemove(CallbackTarget target) {
target.postRemoveCalled( NAME );
}
protected void preUpdate(CallbackTarget target) {
target.preUpdateCalled( NAME );
}
protected void postUpdate(CallbackTarget target) {
target.postUpdateCalled( NAME );
}
protected void postLoad(CallbackTarget target) {
target.postLoadCalled( NAME );
}
}

View File

@ -0,0 +1,45 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html.
*/
package org.hibernate.orm.test.jpa.callbacks.xml.complete;
import org.hibernate.orm.test.jpa.callbacks.xml.common.CallbackTarget;
/**
* @author Steve Ebersole
*/
public class ListenerC {
public static final String NAME = "ListenerC";
protected void prePersist(CallbackTarget target) {
target.prePersistCalled( NAME );
}
protected void postPersist(CallbackTarget target) {
target.postPersistCalled( NAME );
}
protected void preRemove(CallbackTarget target) {
target.preRemoveCalled( NAME );
}
protected void postRemove(CallbackTarget target) {
target.postRemoveCalled( NAME );
}
protected void preUpdate(CallbackTarget target) {
target.preUpdateCalled( NAME );
}
protected void postUpdate(CallbackTarget target) {
target.postUpdateCalled( NAME );
}
protected void postLoad(CallbackTarget target) {
target.postLoadCalled( NAME );
}
}

View File

@ -0,0 +1,161 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html.
*/
package org.hibernate.orm.test.jpa.callbacks.xml.complete;
import java.util.List;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Steve Ebersole
*/
@SuppressWarnings("JUnitMalformedDeclaration")
@DomainModel(
annotatedClasses = { Product.class, Order.class, LineItemSuper.class, LineItem.class},
xmlMappings = "mappings/callbacks/complete.xml"
)
@SessionFactory
public class ListenerCompleteXmlTests {
public static final String[] LISTENER_ABC = { ListenerA.NAME, ListenerB.NAME, ListenerC.NAME };
public static final String[] LISTENER_BC = { ListenerB.NAME, ListenerC.NAME };
@Test
void testSimplePersist(SessionFactoryScope scope) {
final Product product = new Product( 1, "987654321", 123 );
scope.inTransaction( (session) -> {
session.persist( product );
assertThat( product.getPrePersistCallbacks() ).containsExactly( LISTENER_ABC );
} );
assertThat( product.getPostPersistCallbacks() ).containsExactly( LISTENER_ABC );
}
@Test
void testCascadingPersist(SessionFactoryScope scope) {
final Product product = new Product( 1, "987654321", 123 );
final Order order = new Order( 1, 246 );
final LineItem lineItem = new LineItem( 1, order, product, 2 );
order.addLineItem( lineItem );
scope.inTransaction( (session) -> {
session.persist( product );
session.persist( order );
assertThat( product.getPrePersistCallbacks() ).containsExactly( LISTENER_ABC );
assertThat( order.getPrePersistCallbacks() ).containsExactly( LISTENER_ABC );
assertThat( lineItem.getPrePersistCallbacks() ).containsExactly( LISTENER_BC );
} );
assertThat( product.getPostPersistCallbacks() ).containsExactly( LISTENER_ABC );
assertThat( order.getPostPersistCallbacks() ).containsExactly( LISTENER_ABC );
assertThat( lineItem.getPostPersistCallbacks() ).containsExactly( LISTENER_BC );
}
@Test
void testSimpleRemove(SessionFactoryScope scope) {
final Product product = new Product( 1, "987654321", 123 );
scope.inTransaction( (session) -> {
session.persist( product );
} );
scope.inTransaction( (session) -> {
session.remove( product );
assertThat( product.getPreRemoveCallbacks() ).containsExactly( LISTENER_ABC );
} );
assertThat( product.getPostRemoveCallbacks() ).containsExactly( LISTENER_ABC );
}
@Test
void testCascadingRemove(SessionFactoryScope scope) {
final Product product = new Product( 1, "987654321", 123 );
final Order order = new Order( 1, 246 );
final LineItem lineItem = new LineItem( 1, order, product, 2 );
order.addLineItem( lineItem );
scope.inTransaction( (session) -> {
session.persist( product );
session.persist( order );
} );
scope.inTransaction( (session) -> {
session.remove( order );
assertThat( order.getPreRemoveCallbacks() ).containsExactly( LISTENER_ABC );
assertThat( lineItem.getPreRemoveCallbacks() ).containsExactly( LISTENER_BC );
} );
assertThat( order.getPostRemoveCallbacks() ).containsExactly( LISTENER_ABC );
assertThat( lineItem.getPostRemoveCallbacks() ).containsExactly( LISTENER_BC );
}
@Test
void testSimpleUpdate(SessionFactoryScope scope) {
scope.inTransaction( (session) -> {
final Product product = new Product( 1, "987654321", 123 );
session.persist( product );
} );
final Product updated = scope.fromTransaction( (session) -> {
final Product product = session.find( Product.class, 1 );
assertThat( product.getPreUpdateCallbacks() ).isEmpty();
assertThat( product.getPostUpdateCallbacks() ).isEmpty();
product.setCost( 789 );
return product;
} );
assertThat( updated.getPreUpdateCallbacks() ).containsExactly( LISTENER_ABC );
assertThat( updated.getPostUpdateCallbacks() ).containsExactly( LISTENER_ABC );
}
@Test
void testLoading(SessionFactoryScope scope) {
scope.inTransaction( (session) -> {
final Product product = new Product( 1, "987654321", 123 );
final Order order = new Order( 1, 246 );
final LineItem lineItem = new LineItem( 1, order, product, 2 );
order.addLineItem( lineItem );
session.persist( product );
session.persist( order );
} );
scope.inTransaction( (session) -> {
final Product product = session.find( Product.class, 1 );
assertThat( product.getPostLoadCallbacks() ).containsExactly( LISTENER_ABC );
} );
scope.inTransaction( (session) -> {
final List<Product> products = session.createSelectionQuery( "from Product", Product.class ).list();
products.forEach( (product) -> {
assertThat( product.getPostLoadCallbacks() ).containsExactly( LISTENER_ABC );
} );
} );
scope.inTransaction( (session) -> {
final LineItem lineItem = session.find( LineItem.class, 1 );
assertThat( lineItem.getPostLoadCallbacks() ).containsExactly( LISTENER_BC );
assertThat( lineItem.getOrder().getPostLoadCallbacks() ).containsExactly( LISTENER_ABC );
} );
}
@AfterEach
void dropData(SessionFactoryScope scope) {
scope.inTransaction( (session) -> {
session.createMutationQuery( "delete LineItem" ).executeUpdate();
session.createMutationQuery( "delete Order" ).executeUpdate();
session.createMutationQuery( "delete Product" ).executeUpdate();
} );
}
}

View File

@ -0,0 +1,72 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html.
*/
package org.hibernate.orm.test.jpa.callbacks.xml.complete;
import java.util.ArrayList;
import java.util.Collection;
import org.hibernate.orm.test.jpa.callbacks.xml.common.CallbackTarget;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
/**
* @author Steve Ebersole
*/
@Entity
@Table(name = "orders")
public class Order extends CallbackTarget {
@Id
private Integer id;
@Column(name = "total_price")
private double totalPrice;
@OneToMany(mappedBy = "order", cascade = CascadeType.ALL)
private Collection<LineItem> lineItems = new ArrayList<>();
public Order() {
}
public Order(Integer id, double totalPrice) {
this.id = id;
this.totalPrice = totalPrice;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public double getTotalPrice() {
return totalPrice;
}
public void setTotalPrice(double totalPrice) {
this.totalPrice = totalPrice;
}
public Collection<LineItem> getLineItems() {
return lineItems;
}
public void setLineItems(Collection<LineItem> lineItems) {
this.lineItems = lineItems;
}
public void addLineItem(LineItem lineItem) {
if ( lineItems == null ) {
lineItems = new ArrayList<>();
}
lineItems.add( lineItem );
}
}

View File

@ -4,17 +4,17 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html.
*/
package org.hibernate.orm.test.jpa.compliance.callback.listeneroverrides;
package org.hibernate.orm.test.jpa.callbacks.xml.complete;
import org.hibernate.orm.test.jpa.callbacks.xml.common.CallbackTarget;
import jakarta.persistence.Entity;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.Id;
/**
* @author Steve Ebersole
*/
@Entity
@EntityListeners({ListenerC.class, ListenerB.class})
public class Product extends CallbackTarget {
@Id
private Integer id;

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html.
*/
package org.hibernate.orm.test.jpa.compliance.callback.listeneroverrides;
package org.hibernate.orm.test.jpa.callbacks.xml.replace;
import jakarta.persistence.Entity;
import jakarta.persistence.EntityListeners;

View File

@ -0,0 +1,36 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html.
*/
package org.hibernate.orm.test.jpa.callbacks.xml.replace;
import org.hibernate.orm.test.jpa.callbacks.xml.common.CallbackTarget;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.MappedSuperclass;
/**
* @author Steve Ebersole
*/
@MappedSuperclass
@EntityListeners({ ListenerA.class, ListenerB.class})
public class LineItemSuper extends CallbackTarget {
private int quantity;
public LineItemSuper() {
}
public LineItemSuper(int quantity) {
this.quantity = quantity;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
}

View File

@ -4,7 +4,9 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html.
*/
package org.hibernate.orm.test.jpa.compliance.callback.listeneroverrides;
package org.hibernate.orm.test.jpa.callbacks.xml.replace;
import org.hibernate.orm.test.jpa.callbacks.xml.common.CallbackTarget;
import jakarta.persistence.PostLoad;
import jakarta.persistence.PostPersist;

View File

@ -4,7 +4,9 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html.
*/
package org.hibernate.orm.test.jpa.compliance.callback.listeneroverrides;
package org.hibernate.orm.test.jpa.callbacks.xml.replace;
import org.hibernate.orm.test.jpa.callbacks.xml.common.CallbackTarget;
import jakarta.persistence.PostLoad;
import jakarta.persistence.PostPersist;

View File

@ -4,7 +4,9 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html.
*/
package org.hibernate.orm.test.jpa.compliance.callback.listeneroverrides;
package org.hibernate.orm.test.jpa.callbacks.xml.replace;
import org.hibernate.orm.test.jpa.callbacks.xml.common.CallbackTarget;
import jakarta.persistence.PostLoad;
import jakarta.persistence.PostPersist;

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html.
*/
package org.hibernate.orm.test.jpa.compliance.callback.listeneroverrides;
package org.hibernate.orm.test.jpa.callbacks.xml.replace;
import java.util.List;
@ -17,15 +17,17 @@ import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests overriding entity-listeners via XML
*
* @author Steve Ebersole
*/
@SuppressWarnings("JUnitMalformedDeclaration")
@DomainModel(
annotatedClasses = {Product.class,Order.class, LineItemSuper.class, LineItem.class},
xmlMappings = "mappings/callbacks/listener_overrides.xml"
xmlMappings = "mappings/callbacks/replace.xml"
)
@SessionFactory
public class OverridesTests {
public class ListenerReplacementTests {
public static final String[] LISTENER_ABC = {"ListenerA", "ListenerB", "ListenerC"};
public static final String[] LISTENER_BC = {"ListenerB", "ListenerC"};

View File

@ -4,11 +4,13 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html.
*/
package org.hibernate.orm.test.jpa.compliance.callback.listeneroverrides;
package org.hibernate.orm.test.jpa.callbacks.xml.replace;
import java.util.ArrayList;
import java.util.Collection;
import org.hibernate.orm.test.jpa.callbacks.xml.common.CallbackTarget;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;

View File

@ -0,0 +1,58 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html.
*/
package org.hibernate.orm.test.jpa.callbacks.xml.replace;
import org.hibernate.orm.test.jpa.callbacks.xml.common.CallbackTarget;
import jakarta.persistence.Entity;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.Id;
/**
* @author Steve Ebersole
*/
@Entity
@EntityListeners({ ListenerC.class, ListenerB.class})
public class Product extends CallbackTarget {
@Id
private Integer id;
private String partNumber;
private double cost;
public Product() {
}
public Product(Integer id, String partNumber, double cost) {
this.id = id;
this.partNumber = partNumber;
this.cost = cost;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getPartNumber() {
return partNumber;
}
public void setPartNumber(String partNumber) {
this.partNumber = partNumber;
}
public double getCost() {
return cost;
}
public void setCost(double cost) {
this.cost = cost;
}
}

View File

@ -1,57 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html.
*/
package org.hibernate.orm.test.jpa.compliance.callback.listeneroverrides;
import jakarta.persistence.PostLoad;
import jakarta.persistence.PostPersist;
import jakarta.persistence.PostRemove;
import jakarta.persistence.PostUpdate;
import jakarta.persistence.PrePersist;
import jakarta.persistence.PreRemove;
import jakarta.persistence.PreUpdate;
/**
* @author Steve Ebersole
*/
public abstract class ListenerBase {
protected abstract String getListenerName();
@PrePersist
protected void prePersist(CallbackTarget target) {
target.prePersistCalled( getListenerName() );
}
@PostPersist
protected void postPersist(CallbackTarget target) {
target.postPersistCalled( getListenerName() );
}
@PreRemove
protected void preRemove(CallbackTarget target) {
target.preRemoveCalled( getListenerName() );
}
@PostRemove
protected void postRemove(CallbackTarget target) {
target.postRemoveCalled( getListenerName() );
}
@PreUpdate
protected void preUpdate(CallbackTarget target) {
target.preUpdateCalled( getListenerName() );
}
@PostUpdate
protected void postUpdate(CallbackTarget target) {
target.postUpdateCalled( getListenerName() );
}
@PostLoad
protected void postLoad(CallbackTarget target) {
target.postLoadCalled( getListenerName() );
}
}

View File

@ -0,0 +1,128 @@
<!--
~ Hibernate, Relational Persistence for Idiomatic Java
~
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
~ See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html.
-->
<entity-mappings xmlns="https://jakarta.ee/xml/ns/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/persistence/orm https://jakarta.ee/xml/ns/persistence/orm/orm_3_2.xsd"
version="3.2">
<persistence-unit-metadata>
<persistence-unit-defaults>
<entity-listeners>
<entity-listener class="org.hibernate.orm.test.jpa.callbacks.xml.complete.ListenerA">
<pre-persist method-name="prePersist"/>
<post-persist method-name="postPersist"/>
<pre-remove method-name="preRemove"/>
<post-remove method-name="postRemove"/>
<pre-update method-name="preUpdate"/>
<post-update method-name="postUpdate"/>
<post-load method-name="postLoad"/>
</entity-listener>
</entity-listeners>
</persistence-unit-defaults>
</persistence-unit-metadata>
<package>org.hibernate.orm.test.jpa.callbacks.xml.complete</package>
<mapped-superclass class="LineItemSuper">
<entity-listeners>
<entity-listener class="ListenerA">
<pre-persist method-name="prePersist"/>
<post-persist method-name="postPersist"/>
<pre-remove method-name="preRemove"/>
<post-remove method-name="postRemove"/>
<pre-update method-name="preUpdate"/>
<post-update method-name="postUpdate"/>
<post-load method-name="postLoad"/>
</entity-listener>
<entity-listener class="ListenerB">
<pre-persist method-name="prePersist"/>
<post-persist method-name="postPersist"/>
<pre-remove method-name="preRemove"/>
<post-remove method-name="postRemove"/>
<pre-update method-name="preUpdate"/>
<post-update method-name="postUpdate"/>
<post-load method-name="postLoad"/>
</entity-listener>
</entity-listeners>
</mapped-superclass>
<entity class="LineItem">
<exclude-default-listeners/>
<exclude-superclass-listeners/>
<entity-listeners>
<entity-listener class="ListenerB">
<pre-persist method-name="prePersist"/>
<post-persist method-name="postPersist"/>
<pre-remove method-name="preRemove"/>
<post-remove method-name="postRemove"/>
<pre-update method-name="preUpdate"/>
<post-update method-name="postUpdate"/>
<post-load method-name="postLoad"/>
</entity-listener>
<entity-listener class="ListenerC">
<pre-persist method-name="prePersist"/>
<post-persist method-name="postPersist"/>
<pre-remove method-name="preRemove"/>
<post-remove method-name="postRemove"/>
<pre-update method-name="preUpdate"/>
<post-update method-name="postUpdate"/>
<post-load method-name="postLoad"/>
</entity-listener>
</entity-listeners>
</entity>
<entity class="Order">
<table name="ORDER_TABLE"/>
<entity-listeners>
<entity-listener class="ListenerB">
<pre-persist method-name="prePersist"/>
<post-persist method-name="postPersist"/>
<pre-remove method-name="preRemove"/>
<post-remove method-name="postRemove"/>
<pre-update method-name="preUpdate"/>
<post-update method-name="postUpdate"/>
<post-load method-name="postLoad"/>
</entity-listener>
<entity-listener class="ListenerC">
<pre-persist method-name="prePersist"/>
<post-persist method-name="postPersist"/>
<pre-remove method-name="preRemove"/>
<post-remove method-name="postRemove"/>
<pre-update method-name="preUpdate"/>
<post-update method-name="postUpdate"/>
<post-load method-name="postLoad"/>
</entity-listener>
</entity-listeners>
</entity>
<entity class="Product">
<table name="PRODUCT_TABLE"/>
<entity-listeners>
<entity-listener class="ListenerB">
<pre-persist method-name="prePersist"/>
<post-persist method-name="postPersist"/>
<pre-remove method-name="preRemove"/>
<post-remove method-name="postRemove"/>
<pre-update method-name="preUpdate"/>
<post-update method-name="postUpdate"/>
<post-load method-name="postLoad"/>
</entity-listener>
<entity-listener class="ListenerC">
<pre-persist method-name="prePersist"/>
<post-persist method-name="postPersist"/>
<pre-remove method-name="preRemove"/>
<post-remove method-name="postRemove"/>
<pre-update method-name="preUpdate"/>
<post-update method-name="postUpdate"/>
<post-load method-name="postLoad"/>
</entity-listener>
</entity-listeners>
</entity>
</entity-mappings>

View File

@ -13,18 +13,18 @@
<persistence-unit-metadata>
<persistence-unit-defaults>
<entity-listeners>
<entity-listener class="org.hibernate.orm.test.jpa.compliance.callback.listeneroverrides.ListenerA">
<entity-listener class="org.hibernate.orm.test.jpa.callbacks.xml.replace.ListenerA">
</entity-listener>
</entity-listeners>
</persistence-unit-defaults>
</persistence-unit-metadata>
<package>org.hibernate.orm.test.jpa.compliance.callback.listeneroverrides</package>
<package>org.hibernate.orm.test.jpa.callbacks.xml.replace</package>
<entity class="Product">
<entity-listeners>
<entity-listener class="ListenerB"/>
<entity-listener class="org.hibernate.orm.test.jpa.compliance.callback.listeneroverrides.ListenerC"/>
<entity-listener class="ListenerC"/>
</entity-listeners>
</entity>