From a1a4446f096a010236ef98e915e956f5c83f7efc Mon Sep 17 00:00:00 2001 From: Yanming Zhou Date: Tue, 13 Aug 2024 09:26:20 +0800 Subject: [PATCH] HHH-18414 Add test for issue --- .../orm/test/mapping/attributebinder/Foo.java | 27 +++++++++++++ .../mapping/attributebinder/FooBinder.java | 38 +++++++++++++++++++ .../test/mapping/attributebinder/System.java | 1 + 3 files changed, 66 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/mapping/attributebinder/Foo.java create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/mapping/attributebinder/FooBinder.java diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/attributebinder/Foo.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/attributebinder/Foo.java new file mode 100644 index 0000000000..81c08b14d5 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/attributebinder/Foo.java @@ -0,0 +1,27 @@ +/* + * 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.mapping.attributebinder; + +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import org.hibernate.annotations.AttributeBinderType; + +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +/** + * Dummy annotation to verify binders are called only once. + * + * @author Yanming Zhou + */ +@Target({METHOD,FIELD}) +@Retention(RUNTIME) +@AttributeBinderType( binder = FooBinder.class ) +public @interface Foo { +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/attributebinder/FooBinder.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/attributebinder/FooBinder.java new file mode 100644 index 0000000000..394a7b7b7e --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/attributebinder/FooBinder.java @@ -0,0 +1,38 @@ +/* + * 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.mapping.attributebinder; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +import org.hibernate.binder.AttributeBinder; +import org.hibernate.boot.spi.MetadataBuildingContext; +import org.hibernate.mapping.PersistentClass; +import org.hibernate.mapping.Property; + +/** + * The binder to verify binders are called only once. + * + * @author Yanming Zhou + */ +public class FooBinder implements AttributeBinder { + + private static final Map map = new ConcurrentHashMap<>(); + + @Override + public void bind( + Foo annotation, + MetadataBuildingContext buildingContext, + PersistentClass persistentClass, + Property property) { + String key = persistentClass.getClassName() + "." + property.getName(); + Foo existing = map.putIfAbsent( key, annotation ); + if ( existing == annotation ) { + throw new IllegalStateException( "AttributeBinder is called twice" ); + } + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/attributebinder/System.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/attributebinder/System.java index 60dd253fcd..a7b7f63bcf 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/attributebinder/System.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/attributebinder/System.java @@ -19,6 +19,7 @@ public class System { @Basic public String name; @YesNo + @Foo public boolean active; private System() {