I was wondering is it possible to create from jpa/hibernate annotation a database column description/comment like this:
ALTER TABLE tablename CHANGE status status INT(11) NOT NULL COMMENT 'sample description/comment';
It will be great functionality, but I cant find anything about this in JPA specification.
Maybe I should use @Column(columnDefinition="")
property, but I dont have any clue.
Please help
You can use @Comment
annotation.
This annotation is introduced in hibernate 5.6.0
version(released at 2021-09-21), and Spring Boot 2.6
(released at 2021-11-17) is using Hibernate 5.6
https://hibernate.atlassian.net/browse/HHH-4369\ https://github.com/hibernate/hibernate-orm/pull/3611
@Entity(name = "Person")
@javax.persistence.Table(name = TABLE_NAME)
@org.hibernate.annotations.Table(comment = TABLE_COMMENT, appliesTo = TABLE_NAME)
public static class TestEntity {
@Id
@GeneratedValue
@Comment("I am id")
private Long id;
@Comment("I am name")
@javax.persistence.Column(length = 50)
private String name;
@ManyToOne
@JoinColumn(name = "other")
@Comment("I am other")
private TestEntity other;
}
Comment description already exists for Table annotations. We must use the Hibernate @Table annotation for that, complementary with the JPA @Table annotation.
For example :
@javax.persistence.Table( name = "Cat" )
@org.hibernate.annotations.Table( comment = "Table for cats" )
public class Cat {
...
Concerning the column's comment : It seems there is no equivalent, even in Hibernate 5.2 / JPA 2.1.
There has been an issue submitted a long time ago (2007) on this subject, but still unresolved : Support @Comment or column attribute on @Table and @Column. Abandoned since 2012 ?
I also found comment use in org.hibernate.dialect.Dialect :
/**
* Does this dialect/database support commenting on tables, columns, etc?
*
* @return {@code true} if commenting is supported
*/
public boolean supportsCommentOn() {
return false;
}
/**
* Get the comment into a form supported for table definition.
*
* @param comment The comment to apply
*
* @return The comment fragment
*/
public String getTableComment(String comment) {
return "";
}
/**
* Get the comment into a form supported for column definition.
*
* @param comment The comment to apply
*
* @return The comment fragment
*/
public String getColumnComment(String comment) {
return "";
}
For example PostgreSQL81Dialect supports it (supportsCommentOn() returns true).
It enables the use of the SQL command "COMMENT ON ...",
like in PostgreSQL (https://www.postgresql.org/docs/current/static/sql-comment.html).
For example :
COMMENT ON TABLE my_schema.my_table IS 'Employee Information';
COMMENT ON COLUMN my_table.my_column IS 'Employee ID number';
It seems to be used there : org.hibernate.tool.schema.internal.StandardTableExporter#getSqlCreateStrings
The column's comment is extracted from the Hibernate Mapping with org.hibernate.mapping.Column#getComment.
Finally, If using Hibernate Tools and reverse engineering, the column's comment is extracted from JDBC DatabaseMetaData, with org.hibernate.cfg.reveng.BasicColumnProcessor#processBasicColumns.
-> String comment = (String) columnRs.get("REMARKS");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With