HHH-9110 add test/example

This commit is contained in:
Gavin King 2024-03-10 00:30:48 +01:00
parent 17f1221db2
commit 710d7acb6d
4 changed files with 84 additions and 0 deletions

View File

@ -22,6 +22,7 @@ import java.lang.annotation.Annotation;
* custom mapping annotation}.
*
* @see org.hibernate.annotations.AttributeBinderType
* @see TypeBinder
*
* @author Gavin King
*/

View File

@ -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}.
* <p>
* For example, this annotation disables rowcount checking for insert,
* update, and delete statements for annotated entities:
* <pre>
* &#064;TypeBinderType(binder = NoResultCheck.Binder.class)
* &#064;Target(TYPE) &#064;Retention(RUNTIME)
* public @interface NoResultCheck {
* class Binder implements TypeBinder&lt;NoResultCheck&gt; {
* &#064;Override
* public void bind(NoResultCheck annotation,
* MetadataBuildingContext buildingContext,
* PersistentClass persistentClass) {
* persistentClass.setInsertCheckStyle(NONE);
* persistentClass.setUpdateCheckStyle(NONE);
* persistentClass.setDeleteCheckStyle(NONE);
* }
* &#064;Override
* public void bind(NoResultCheck annotation,
* MetadataBuildingContext buildingContext,
* Component embeddableClass) {
* throw new AnnotationException("'@NoResultCheck' cannot annotate an '@Embeddable' class");
* }
* }
* }
* </pre>
*
* @see org.hibernate.annotations.TypeBinderType
* @see AttributeBinder
*
* @author Gavin King
*/

View File

@ -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<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) {
}
}
}

View File

@ -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;
}
}