From c7a15a835d24e921d190999c11937bb3479a6687 Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Wed, 1 May 2024 10:26:08 -0500 Subject: [PATCH] HHH-18049 - Handle and --- .../xml/internal/ManagedTypeProcessor.java | 13 +- .../xml/internal/XmlAnnotationHelper.java | 21 +++ .../xml/common}/CallbackTarget.java | 2 +- .../jpa/callbacks/xml/complete/LineItem.java | 61 +++++++ .../xml/complete}/LineItemSuper.java | 6 +- .../jpa/callbacks/xml/complete/ListenerA.java | 45 +++++ .../jpa/callbacks/xml/complete/ListenerB.java | 44 +++++ .../jpa/callbacks/xml/complete/ListenerC.java | 45 +++++ .../complete/ListenerCompleteXmlTests.java | 161 ++++++++++++++++++ .../jpa/callbacks/xml/complete/Order.java | 72 ++++++++ .../xml/complete}/Product.java | 6 +- .../xml/replace}/LineItem.java | 4 +- .../callbacks/xml/replace/LineItemSuper.java | 36 ++++ .../xml/replace}/ListenerA.java | 4 +- .../xml/replace}/ListenerB.java | 4 +- .../xml/replace}/ListenerC.java | 4 +- .../replace/ListenerReplacementTests.java} | 8 +- .../xml/replace}/Order.java | 6 +- .../jpa/callbacks/xml/replace/Product.java | 58 +++++++ .../listeneroverrides/ListenerBase.java | 57 ------- .../resources/mappings/callbacks/complete.xml | 128 ++++++++++++++ .../{listener_overrides.xml => replace.xml} | 8 +- 22 files changed, 705 insertions(+), 88 deletions(-) rename hibernate-core/src/test/java/org/hibernate/orm/test/jpa/{compliance/callback/listeneroverrides => callbacks/xml/common}/CallbackTarget.java (96%) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/jpa/callbacks/xml/complete/LineItem.java rename hibernate-core/src/test/java/org/hibernate/orm/test/jpa/{compliance/callback/listeneroverrides => callbacks/xml/complete}/LineItemSuper.java (78%) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/jpa/callbacks/xml/complete/ListenerA.java create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/jpa/callbacks/xml/complete/ListenerB.java create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/jpa/callbacks/xml/complete/ListenerC.java create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/jpa/callbacks/xml/complete/ListenerCompleteXmlTests.java create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/jpa/callbacks/xml/complete/Order.java rename hibernate-core/src/test/java/org/hibernate/orm/test/jpa/{compliance/callback/listeneroverrides => callbacks/xml/complete}/Product.java (85%) rename hibernate-core/src/test/java/org/hibernate/orm/test/jpa/{compliance/callback/listeneroverrides => callbacks/xml/replace}/LineItem.java (91%) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/jpa/callbacks/xml/replace/LineItemSuper.java rename hibernate-core/src/test/java/org/hibernate/orm/test/jpa/{compliance/callback/listeneroverrides => callbacks/xml/replace}/ListenerA.java (91%) rename hibernate-core/src/test/java/org/hibernate/orm/test/jpa/{compliance/callback/listeneroverrides => callbacks/xml/replace}/ListenerB.java (91%) rename hibernate-core/src/test/java/org/hibernate/orm/test/jpa/{compliance/callback/listeneroverrides => callbacks/xml/replace}/ListenerC.java (91%) rename hibernate-core/src/test/java/org/hibernate/orm/test/jpa/{compliance/callback/listeneroverrides/OverridesTests.java => callbacks/xml/replace/ListenerReplacementTests.java} (96%) rename hibernate-core/src/test/java/org/hibernate/orm/test/jpa/{compliance/callback/listeneroverrides => callbacks/xml/replace}/Order.java (89%) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/jpa/callbacks/xml/replace/Product.java delete mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/jpa/compliance/callback/listeneroverrides/ListenerBase.java create mode 100644 hibernate-core/src/test/resources/mappings/callbacks/complete.xml rename hibernate-core/src/test/resources/mappings/callbacks/{listener_overrides.xml => replace.xml} (83%) diff --git a/hibernate-core/src/main/java/org/hibernate/boot/models/xml/internal/ManagedTypeProcessor.java b/hibernate-core/src/main/java/org/hibernate/boot/models/xml/internal/ManagedTypeProcessor.java index 7796b9a8b2..b4f4580c95 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/models/xml/internal/ManagedTypeProcessor.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/models/xml/internal/ManagedTypeProcessor.java @@ -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( diff --git a/hibernate-core/src/main/java/org/hibernate/boot/models/xml/internal/XmlAnnotationHelper.java b/hibernate-core/src/main/java/org/hibernate/boot/models/xml/internal/XmlAnnotationHelper.java index 1501f1814a..7ac4ed7327 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/models/xml/internal/XmlAnnotationHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/models/xml/internal/XmlAnnotationHelper.java @@ -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, diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/compliance/callback/listeneroverrides/CallbackTarget.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/callbacks/xml/common/CallbackTarget.java similarity index 96% rename from hibernate-core/src/test/java/org/hibernate/orm/test/jpa/compliance/callback/listeneroverrides/CallbackTarget.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/jpa/callbacks/xml/common/CallbackTarget.java index ebdfe67d7e..a3ee409846 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/compliance/callback/listeneroverrides/CallbackTarget.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/callbacks/xml/common/CallbackTarget.java @@ -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; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/callbacks/xml/complete/LineItem.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/callbacks/xml/complete/LineItem.java new file mode 100644 index 0000000000..a41be81bde --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/callbacks/xml/complete/LineItem.java @@ -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; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/compliance/callback/listeneroverrides/LineItemSuper.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/callbacks/xml/complete/LineItemSuper.java similarity index 78% rename from hibernate-core/src/test/java/org/hibernate/orm/test/jpa/compliance/callback/listeneroverrides/LineItemSuper.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/jpa/callbacks/xml/complete/LineItemSuper.java index ced2e55685..cdff4dadcd 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/compliance/callback/listeneroverrides/LineItemSuper.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/callbacks/xml/complete/LineItemSuper.java @@ -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; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/callbacks/xml/complete/ListenerA.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/callbacks/xml/complete/ListenerA.java new file mode 100644 index 0000000000..87e249a4bf --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/callbacks/xml/complete/ListenerA.java @@ -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 ); + } + +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/callbacks/xml/complete/ListenerB.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/callbacks/xml/complete/ListenerB.java new file mode 100644 index 0000000000..ba3e93f052 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/callbacks/xml/complete/ListenerB.java @@ -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 ); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/callbacks/xml/complete/ListenerC.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/callbacks/xml/complete/ListenerC.java new file mode 100644 index 0000000000..ff7270dd26 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/callbacks/xml/complete/ListenerC.java @@ -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 ); + } + +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/callbacks/xml/complete/ListenerCompleteXmlTests.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/callbacks/xml/complete/ListenerCompleteXmlTests.java new file mode 100644 index 0000000000..f1c816302d --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/callbacks/xml/complete/ListenerCompleteXmlTests.java @@ -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 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(); + } ); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/callbacks/xml/complete/Order.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/callbacks/xml/complete/Order.java new file mode 100644 index 0000000000..04e4cabc96 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/callbacks/xml/complete/Order.java @@ -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 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 getLineItems() { + return lineItems; + } + + public void setLineItems(Collection lineItems) { + this.lineItems = lineItems; + } + + public void addLineItem(LineItem lineItem) { + if ( lineItems == null ) { + lineItems = new ArrayList<>(); + } + lineItems.add( lineItem ); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/compliance/callback/listeneroverrides/Product.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/callbacks/xml/complete/Product.java similarity index 85% rename from hibernate-core/src/test/java/org/hibernate/orm/test/jpa/compliance/callback/listeneroverrides/Product.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/jpa/callbacks/xml/complete/Product.java index 2e825a34d4..06e16ea445 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/compliance/callback/listeneroverrides/Product.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/callbacks/xml/complete/Product.java @@ -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; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/compliance/callback/listeneroverrides/LineItem.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/callbacks/xml/replace/LineItem.java similarity index 91% rename from hibernate-core/src/test/java/org/hibernate/orm/test/jpa/compliance/callback/listeneroverrides/LineItem.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/jpa/callbacks/xml/replace/LineItem.java index 49ecb85146..60c984489b 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/compliance/callback/listeneroverrides/LineItem.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/callbacks/xml/replace/LineItem.java @@ -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; @@ -18,7 +18,7 @@ import jakarta.persistence.ManyToOne; * @author Steve Ebersole */ @Entity -@EntityListeners({ListenerC.class, ListenerB.class}) +@EntityListeners({ ListenerC.class, ListenerB.class}) @ExcludeDefaultListeners() @ExcludeSuperclassListeners public class LineItem extends LineItemSuper { diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/callbacks/xml/replace/LineItemSuper.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/callbacks/xml/replace/LineItemSuper.java new file mode 100644 index 0000000000..4cc301aad9 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/callbacks/xml/replace/LineItemSuper.java @@ -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; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/compliance/callback/listeneroverrides/ListenerA.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/callbacks/xml/replace/ListenerA.java similarity index 91% rename from hibernate-core/src/test/java/org/hibernate/orm/test/jpa/compliance/callback/listeneroverrides/ListenerA.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/jpa/callbacks/xml/replace/ListenerA.java index 5f97219959..52fbb41256 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/compliance/callback/listeneroverrides/ListenerA.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/callbacks/xml/replace/ListenerA.java @@ -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; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/compliance/callback/listeneroverrides/ListenerB.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/callbacks/xml/replace/ListenerB.java similarity index 91% rename from hibernate-core/src/test/java/org/hibernate/orm/test/jpa/compliance/callback/listeneroverrides/ListenerB.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/jpa/callbacks/xml/replace/ListenerB.java index d4715f3688..4869078a22 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/compliance/callback/listeneroverrides/ListenerB.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/callbacks/xml/replace/ListenerB.java @@ -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; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/compliance/callback/listeneroverrides/ListenerC.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/callbacks/xml/replace/ListenerC.java similarity index 91% rename from hibernate-core/src/test/java/org/hibernate/orm/test/jpa/compliance/callback/listeneroverrides/ListenerC.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/jpa/callbacks/xml/replace/ListenerC.java index ebd85c4d26..58184c998b 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/compliance/callback/listeneroverrides/ListenerC.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/callbacks/xml/replace/ListenerC.java @@ -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; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/compliance/callback/listeneroverrides/OverridesTests.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/callbacks/xml/replace/ListenerReplacementTests.java similarity index 96% rename from hibernate-core/src/test/java/org/hibernate/orm/test/jpa/compliance/callback/listeneroverrides/OverridesTests.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/jpa/callbacks/xml/replace/ListenerReplacementTests.java index bd05b35a02..2c3a9c3d98 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/compliance/callback/listeneroverrides/OverridesTests.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/callbacks/xml/replace/ListenerReplacementTests.java @@ -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"}; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/compliance/callback/listeneroverrides/Order.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/callbacks/xml/replace/Order.java similarity index 89% rename from hibernate-core/src/test/java/org/hibernate/orm/test/jpa/compliance/callback/listeneroverrides/Order.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/jpa/callbacks/xml/replace/Order.java index 19e5c78c18..e79df49c06 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/compliance/callback/listeneroverrides/Order.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/callbacks/xml/replace/Order.java @@ -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; @@ -22,7 +24,7 @@ import jakarta.persistence.Table; */ @Entity @Table(name = "orders") -@EntityListeners({ListenerC.class, ListenerB.class}) +@EntityListeners({ ListenerC.class, ListenerB.class}) public class Order extends CallbackTarget { @Id private Integer id; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/callbacks/xml/replace/Product.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/callbacks/xml/replace/Product.java new file mode 100644 index 0000000000..402f930acc --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/callbacks/xml/replace/Product.java @@ -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; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/compliance/callback/listeneroverrides/ListenerBase.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/compliance/callback/listeneroverrides/ListenerBase.java deleted file mode 100644 index a20a12f567..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/compliance/callback/listeneroverrides/ListenerBase.java +++ /dev/null @@ -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() ); - } -} diff --git a/hibernate-core/src/test/resources/mappings/callbacks/complete.xml b/hibernate-core/src/test/resources/mappings/callbacks/complete.xml new file mode 100644 index 0000000000..4713368c10 --- /dev/null +++ b/hibernate-core/src/test/resources/mappings/callbacks/complete.xml @@ -0,0 +1,128 @@ + + + + + + + + + + + + + + + + + + + + org.hibernate.orm.test.jpa.callbacks.xml.complete + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + diff --git a/hibernate-core/src/test/resources/mappings/callbacks/listener_overrides.xml b/hibernate-core/src/test/resources/mappings/callbacks/replace.xml similarity index 83% rename from hibernate-core/src/test/resources/mappings/callbacks/listener_overrides.xml rename to hibernate-core/src/test/resources/mappings/callbacks/replace.xml index 586455b913..1c4f17cd5d 100644 --- a/hibernate-core/src/test/resources/mappings/callbacks/listener_overrides.xml +++ b/hibernate-core/src/test/resources/mappings/callbacks/replace.xml @@ -13,18 +13,18 @@ - + - org.hibernate.orm.test.jpa.compliance.callback.listeneroverrides + org.hibernate.orm.test.jpa.callbacks.xml.replace - + @@ -42,4 +42,4 @@ - \ No newline at end of file +