From df5980226ca4dc654479ff03ff6dfa0a798fc520 Mon Sep 17 00:00:00 2001 From: Gavin Date: Sun, 1 Jan 2023 18:21:07 +0100 Subject: [PATCH] HHH-15959 add TypeBinders + fix multiple AttributeBinders on a single field --- .../annotations/AttributeBinderType.java | 2 + .../hibernate/annotations/TypeBinderType.java | 38 ++++++++++ .../java/org/hibernate/binder/TypeBinder.java | 48 ++++++++++++ .../internal/AttributeAccessorBinder.java | 1 - .../binder/internal/package-info.java | 3 +- .../org/hibernate/binder/package-info.java | 26 +++++-- .../boot/model/internal/EmbeddableBinder.java | 73 ++++++++++++++----- .../boot/model/internal/EntityBinder.java | 17 +++++ .../boot/model/internal/HCANNHelper.java | 17 +++-- .../boot/model/internal/PropertyBinder.java | 29 +++++--- .../java/org/hibernate/mapping/Component.java | 6 +- .../java/org/hibernate/mapping/Table.java | 2 +- hibernate-core/src/main/javadoc/overview.html | 5 +- release/src/release/javadoc/overview.html | 5 +- 14 files changed, 219 insertions(+), 53 deletions(-) create mode 100644 hibernate-core/src/main/java/org/hibernate/annotations/TypeBinderType.java create mode 100644 hibernate-core/src/main/java/org/hibernate/binder/TypeBinder.java diff --git a/hibernate-core/src/main/java/org/hibernate/annotations/AttributeBinderType.java b/hibernate-core/src/main/java/org/hibernate/annotations/AttributeBinderType.java index 8be772e139..7b5794fe40 100644 --- a/hibernate-core/src/main/java/org/hibernate/annotations/AttributeBinderType.java +++ b/hibernate-core/src/main/java/org/hibernate/annotations/AttributeBinderType.java @@ -24,6 +24,8 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME; * will be called when the annotation is discovered by Hibernate. * * @author Gavin King + * + * @see TypeBinderType */ @Target(ANNOTATION_TYPE) @Retention(RUNTIME) diff --git a/hibernate-core/src/main/java/org/hibernate/annotations/TypeBinderType.java b/hibernate-core/src/main/java/org/hibernate/annotations/TypeBinderType.java new file mode 100644 index 0000000000..807f3454c5 --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/annotations/TypeBinderType.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 . + */ +package org.hibernate.annotations; + +import org.hibernate.Incubating; +import org.hibernate.binder.TypeBinder; + +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import static java.lang.annotation.ElementType.ANNOTATION_TYPE; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +/** + * Associates a user-defined annotation with a {@link TypeBinder}, + * allowing the annotation to drive some custom model binding. + *

+ * The user-defined annotation may be used to annotate entity and + * embeddable classes. The {@code TypeBinder} will be called when + * the annotation is discovered by Hibernate. + * + * @author Gavin King + * + * @see AttributeBinderType + */ +@Target(ANNOTATION_TYPE) +@Retention(RUNTIME) +@Incubating +public @interface TypeBinderType { + /** + * @return a type which implements {@link TypeBinder} + */ + Class> binder(); +} diff --git a/hibernate-core/src/main/java/org/hibernate/binder/TypeBinder.java b/hibernate-core/src/main/java/org/hibernate/binder/TypeBinder.java new file mode 100644 index 0000000000..fc0188a3da --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/binder/TypeBinder.java @@ -0,0 +1,48 @@ +/* + * 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 . + */ +package org.hibernate.binder; + +import org.hibernate.Incubating; +import org.hibernate.boot.spi.MetadataBuildingContext; +import org.hibernate.mapping.Component; +import org.hibernate.mapping.PersistentClass; + +import java.lang.annotation.Annotation; + +/** + * Allows a user-written annotation to drive some customized model binding. + *

+ * An implementation of this interface interacts directly with model objects + * like {@link PersistentClass} and {@link Component} to implement the + * semantics of some {@link org.hibernate.annotations.TypeBinderType + * custom mapping annotation}. + * + * @see org.hibernate.annotations.TypeBinderType + * + * @author Gavin King + */ +@Incubating +public interface TypeBinder { + /** + * Perform some custom configuration of the model relating to the given annotated + * {@link PersistentClass entity class}. + * + * @param annotation an annotation of the entity class that is declared as an + * {@link org.hibernate.annotations.TypeBinderType} + * @param persistentClass the entity class + */ + void bind(A annotation, MetadataBuildingContext buildingContext, PersistentClass persistentClass); + /** + * Perform some custom configuration of the model relating to the given annotated + * {@link Component embeddable class}. + * + * @param annotation an annotation of the embeddable class that is declared as an + * {@link org.hibernate.annotations.TypeBinderType} + * @param embeddableClass the embeddable class + */ + void bind(A annotation, MetadataBuildingContext buildingContext, Component embeddableClass); +} diff --git a/hibernate-core/src/main/java/org/hibernate/binder/internal/AttributeAccessorBinder.java b/hibernate-core/src/main/java/org/hibernate/binder/internal/AttributeAccessorBinder.java index ce445e0875..fd50355086 100644 --- a/hibernate-core/src/main/java/org/hibernate/binder/internal/AttributeAccessorBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/binder/internal/AttributeAccessorBinder.java @@ -37,6 +37,5 @@ public class AttributeAccessorBinder implements AttributeBinder - * The meta-annotation - * {@link org.hibernate.annotations.AttributeBinderType} - * associates an {@link org.hibernate.binder.AttributeBinder} - * with a user-written annotation. + * This package defines an easy way to extend Hibernate with user-defined + * annotations that define customized O/R mappings of annotated entities + * and annotated entity attributes. + *

diff --git a/release/src/release/javadoc/overview.html b/release/src/release/javadoc/overview.html index e244feb252..d69e3dcca4 100644 --- a/release/src/release/javadoc/overview.html +++ b/release/src/release/javadoc/overview.html @@ -133,7 +133,10 @@

  • {@link org.hibernate.context.spi} defines support for context-bound "current" sessions - and contextual multi-tenancy, + and contextual multi-tenancy, and +
  • +
  • + {@link org.hibernate.binder} allows for user-defined mapping annotations.