diff --git a/hibernate-core/src/main/java/org/hibernate/binder/AttributeBinder.java b/hibernate-core/src/main/java/org/hibernate/binder/AttributeBinder.java index 4eb497455c..fc4786513b 100644 --- a/hibernate-core/src/main/java/org/hibernate/binder/AttributeBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/binder/AttributeBinder.java @@ -22,6 +22,7 @@ import java.lang.annotation.Annotation; * custom mapping annotation}. * * @see org.hibernate.annotations.AttributeBinderType + * @see TypeBinder * * @author Gavin King */ diff --git a/hibernate-core/src/main/java/org/hibernate/binder/TypeBinder.java b/hibernate-core/src/main/java/org/hibernate/binder/TypeBinder.java index d3d3b33fed..f4c0499b61 100644 --- a/hibernate-core/src/main/java/org/hibernate/binder/TypeBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/binder/TypeBinder.java @@ -20,8 +20,34 @@ import java.lang.annotation.Annotation; * like {@link PersistentClass} and {@link Component} to implement the * semantics of some {@linkplain org.hibernate.annotations.TypeBinderType * custom mapping annotation}. + *

+ * For example, this annotation disables rowcount checking for insert, + * update, and delete statements for annotated entities: + *

+ * @TypeBinderType(binder = NoResultCheck.Binder.class)
+ * @Target(TYPE) @Retention(RUNTIME)
+ * public @interface NoResultCheck {
+ *     class Binder implements TypeBinder<NoResultCheck> {
+ *         @Override
+ *         public void bind(NoResultCheck annotation,
+ *                 MetadataBuildingContext buildingContext,
+ *                 PersistentClass persistentClass) {
+ *             persistentClass.setInsertCheckStyle(NONE);
+ *             persistentClass.setUpdateCheckStyle(NONE);
+ *             persistentClass.setDeleteCheckStyle(NONE);
+ *         }
+ *         @Override
+ *         public void bind(NoResultCheck annotation,
+ *                 MetadataBuildingContext buildingContext,
+ *                 Component embeddableClass) {
+ *             throw new AnnotationException("'@NoResultCheck' cannot annotate an '@Embeddable' class");
+ *         }
+ *     }
+ * }
+ * 
* * @see org.hibernate.annotations.TypeBinderType + * @see AttributeBinder * * @author Gavin King */ diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/attributebinder/typebinder/NoResultCheck.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/attributebinder/typebinder/NoResultCheck.java new file mode 100644 index 0000000000..1f9f35a0e0 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/attributebinder/typebinder/NoResultCheck.java @@ -0,0 +1,33 @@ +package org.hibernate.orm.test.mapping.attributebinder.typebinder; + +import org.hibernate.annotations.TypeBinderType; +import org.hibernate.binder.TypeBinder; +import org.hibernate.boot.spi.MetadataBuildingContext; +import org.hibernate.mapping.Component; +import org.hibernate.mapping.PersistentClass; + +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import static java.lang.annotation.ElementType.TYPE; +import static java.lang.annotation.RetentionPolicy.RUNTIME; +import static org.hibernate.engine.spi.ExecuteUpdateResultCheckStyle.NONE; + +@TypeBinderType(binder = NoResultCheck.Binder.class) +@Retention(RUNTIME) +@Target(TYPE) +public @interface NoResultCheck { + class Binder implements TypeBinder { + @Override + public void bind(NoResultCheck annotation, MetadataBuildingContext buildingContext, PersistentClass persistentClass) { + persistentClass.setInsertCheckStyle(NONE); + persistentClass.setUpdateCheckStyle(NONE); + persistentClass.setDeleteCheckStyle(NONE); + } + + @Override + public void bind(NoResultCheck annotation, MetadataBuildingContext buildingContext, Component embeddableClass) { + + } + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/attributebinder/typebinder/ResultCheckBinderTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/attributebinder/typebinder/ResultCheckBinderTest.java new file mode 100644 index 0000000000..d3fdd3bfde --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/attributebinder/typebinder/ResultCheckBinderTest.java @@ -0,0 +1,24 @@ +package org.hibernate.orm.test.mapping.attributebinder.typebinder; + +import jakarta.persistence.Id; +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.Test; + +@SessionFactory +@DomainModel(annotatedClasses = ResultCheckBinderTest.Entity.class) +public class ResultCheckBinderTest { + @Test void test(SessionFactoryScope scope) { + Entity entity = new Entity(); + scope.inStatelessTransaction(s -> s.insert(entity) ); + scope.inStatelessTransaction(s -> s.delete(entity) ); + scope.inStatelessTransaction( s -> s.update(entity) ); + scope.inStatelessTransaction(s -> s.delete(entity) ); + } + @NoResultCheck + @jakarta.persistence.Entity + static class Entity { + @Id long id; + } +}