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
This commit is contained in:
Gavin King 2022-01-09 20:22:37 +01:00
parent 9eac2b193b
commit 4324509a59
6 changed files with 34 additions and 16 deletions

View File

@ -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.
* <ul>
* <li>When a field or property is annotated, the comment applies to the mapped column.
* <li>When an entity class is annotated, the comment applies to the primary table.
* </ul>
*
* @author Yanming Zhou
*/
@Target({METHOD, FIELD})
@Target({METHOD, FIELD, TYPE})
@Retention(RUNTIME)
public @interface Comment {
/**

View File

@ -33,7 +33,9 @@ public @interface Table {
Index[] indexes() default {};
/**
* define a table comment.
* Define a table comment.
*
* @see Comment
*/
String comment() default "";

View File

@ -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,

View File

@ -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,

View File

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

View File

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