From 4324509a59c0f8e4e6e16eee8c154f302826bd88 Mon Sep 17 00:00:00 2001 From: Gavin King Date: Sun, 9 Jan 2022 20:22:37 +0100 Subject: [PATCH] allow @Comment to apply at the class level yes, you can do it with the @Table annotation but this is nicer, no good reason to not allow it --- .../java/org/hibernate/annotations/Comment.java | 9 +++++++-- .../main/java/org/hibernate/annotations/Table.java | 4 +++- .../java/org/hibernate/cfg/AnnotationBinder.java | 11 ++++++----- .../java/org/hibernate/cfg/ColumnsBuilder.java | 14 ++++++++------ .../hibernate/cfg/annotations/EntityBinder.java | 10 +++++++++- .../orm/test/annotations/comment/CommentTest.java | 2 +- 6 files changed, 34 insertions(+), 16 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/annotations/Comment.java b/hibernate-core/src/main/java/org/hibernate/annotations/Comment.java index b1910cb7bb..4a22db3988 100644 --- a/hibernate-core/src/main/java/org/hibernate/annotations/Comment.java +++ b/hibernate-core/src/main/java/org/hibernate/annotations/Comment.java @@ -11,14 +11,19 @@ import java.lang.annotation.Target; import static java.lang.annotation.ElementType.FIELD; import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.ElementType.TYPE; import static java.lang.annotation.RetentionPolicy.RUNTIME; /** - * SQL column comment which can be defined at property level. + * Specifies a comment that will be included in generated DDL. + * * * @author Yanming Zhou */ -@Target({METHOD, FIELD}) +@Target({METHOD, FIELD, TYPE}) @Retention(RUNTIME) public @interface Comment { /** diff --git a/hibernate-core/src/main/java/org/hibernate/annotations/Table.java b/hibernate-core/src/main/java/org/hibernate/annotations/Table.java index 320c96b228..fa17e8128d 100644 --- a/hibernate-core/src/main/java/org/hibernate/annotations/Table.java +++ b/hibernate-core/src/main/java/org/hibernate/annotations/Table.java @@ -33,7 +33,9 @@ public @interface Table { Index[] indexes() default {}; /** - * define a table comment. + * Define a table comment. + * + * @see Comment */ String comment() default ""; diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java b/hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java index 4650ec3572..53ca4be45a 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java @@ -2059,6 +2059,7 @@ public final class AnnotationBinder { PropertyData virtualProperty = isJPA2ForValueMapping ? inferredData : new WrappedInferredData( inferredData, "element" ); + Comment comment = property.getAnnotation(Comment.class); if ( property.isAnnotationPresent( Column.class ) || property.isAnnotationPresent( Formula.class ) ) { @@ -2067,7 +2068,7 @@ public final class AnnotationBinder { elementColumns = AnnotatedColumn.buildColumnFromAnnotation( new Column[] { ann }, formulaAnn, - property.getAnnotation( Comment.class ), + comment, nullability, propertyHolder, virtualProperty, @@ -2080,7 +2081,7 @@ public final class AnnotationBinder { elementColumns = AnnotatedColumn.buildColumnFromAnnotation( anns.columns(), null, - property.getAnnotation( Comment.class ), + comment, nullability, propertyHolder, virtualProperty, @@ -2092,7 +2093,7 @@ public final class AnnotationBinder { elementColumns = AnnotatedColumn.buildColumnFromAnnotation( null, null, - property.getAnnotation( Comment.class ), + comment, nullability, propertyHolder, virtualProperty, @@ -2112,7 +2113,7 @@ public final class AnnotationBinder { AnnotatedColumn[] mapColumns = AnnotatedColumn.buildColumnFromAnnotation( keyColumns, null, - property.getAnnotation( Comment.class ), + comment, Nullability.FORCED_NOT_NULL, propertyHolder, inferredData, @@ -2152,7 +2153,7 @@ public final class AnnotationBinder { AnnotatedJoinColumn[] mapJoinColumns = AnnotatedJoinColumn.buildJoinColumnsWithDefaultColumnSuffix( joinKeyColumns, - property.getAnnotation( Comment.class ), + comment, null, entityBinder.getSecondaryTables(), propertyHolder, diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/ColumnsBuilder.java b/hibernate-core/src/main/java/org/hibernate/cfg/ColumnsBuilder.java index 21eb857b5a..a209d6acaf 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/ColumnsBuilder.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/ColumnsBuilder.java @@ -73,13 +73,14 @@ class ColumnsBuilder { joinColumns = buildExplicitJoinColumns(property, inferredData); + Comment comment = property.getAnnotation(Comment.class); if ( property.isAnnotationPresent( Column.class ) || property.isAnnotationPresent( Formula.class ) ) { Column ann = property.getAnnotation( Column.class ); Formula formulaAnn = property.getAnnotation( Formula.class ); columns = AnnotatedColumn.buildColumnFromAnnotation( new Column[] { ann }, formulaAnn, - property.getAnnotation( Comment.class ), + comment, nullability, propertyHolder, inferredData, @@ -92,7 +93,7 @@ class ColumnsBuilder { columns = AnnotatedColumn.buildColumnFromAnnotation( anns.columns(), null, - property.getAnnotation( Comment.class ), + comment, nullability, propertyHolder, inferredData, @@ -118,7 +119,7 @@ class ColumnsBuilder { ""; joinColumns = AnnotatedJoinColumn.buildJoinColumns( null, - property.getAnnotation( Comment.class ), + comment, mappedBy, entityBinder.getSecondaryTables(), propertyHolder, @@ -135,7 +136,7 @@ class ColumnsBuilder { columns = AnnotatedColumn.buildColumnFromAnnotation( null, null, - property.getAnnotation( Comment.class ), + comment, nullability, propertyHolder, inferredData, @@ -156,10 +157,11 @@ class ColumnsBuilder { AnnotatedJoinColumn[] buildDefaultJoinColumnsForXToOne(XProperty property, PropertyData inferredData) { AnnotatedJoinColumn[] joinColumns; JoinTable joinTableAnn = propertyHolder.getJoinTable( property ); + Comment comment = property.getAnnotation(Comment.class); if ( joinTableAnn != null ) { joinColumns = AnnotatedJoinColumn.buildJoinColumns( joinTableAnn.inverseJoinColumns(), - property.getAnnotation( Comment.class ), + comment, null, entityBinder.getSecondaryTables(), propertyHolder, @@ -180,7 +182,7 @@ class ColumnsBuilder { : null; joinColumns = AnnotatedJoinColumn.buildJoinColumns( null, - property.getAnnotation( Comment.class ), + comment, mappedBy, entityBinder.getSecondaryTables(), propertyHolder, diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/EntityBinder.java b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/EntityBinder.java index ae8e7ec7ae..1210f9ea77 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/EntityBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/EntityBinder.java @@ -31,6 +31,7 @@ import org.hibernate.MappingException; import org.hibernate.annotations.BatchSize; import org.hibernate.annotations.Cache; import org.hibernate.annotations.CacheConcurrencyStrategy; +import org.hibernate.annotations.Comment; import org.hibernate.annotations.DynamicInsert; import org.hibernate.annotations.DynamicUpdate; import org.hibernate.annotations.FetchMode; @@ -817,6 +818,11 @@ public class EntityBinder { table.setRowId( rowId.value() ); } + final Comment comment = annotatedClass.getAnnotation( Comment.class ); + if ( comment != null ) { + table.setComment( comment.value() ); + } + context.getMetadataCollector().addEntityTableXref( persistentClass.getEntityName(), logicalName, @@ -1221,7 +1227,9 @@ public class EntityBinder { "@org.hibernate.annotations.Table references an unknown table: " + appliedTable ); } - if ( !BinderHelper.isEmptyAnnotationValue( table.comment() ) ) hibTable.setComment( table.comment() ); + if ( !BinderHelper.isEmptyAnnotationValue( table.comment() ) ) { + hibTable.setComment( table.comment() ); + } TableBinder.addIndexes( hibTable, table.indexes(), context ); } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/comment/CommentTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/comment/CommentTest.java index 44ba14b77f..67be931923 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/comment/CommentTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/comment/CommentTest.java @@ -56,7 +56,7 @@ public class CommentTest { @Entity(name = "Person") @jakarta.persistence.Table(name = TABLE_NAME) - @org.hibernate.annotations.Table(comment = TABLE_COMMENT, appliesTo = TABLE_NAME) + @Comment(TABLE_COMMENT) public static class TestEntity { @Id