Reputation: 5341
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
Upvotes: 12
Views: 31838
Reputation: 257
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;
}
Upvotes: 6
Reputation: 4385
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");
Upvotes: 7
Reputation: 153950
While Hibernate mappings are needed for mapping Domain Models to Relational Data, it's not a good idea to stretch this feature to generating the database schema too.
This might work for a relatively small project, but when you have a medium to large enterprise application, that evolves with every Sprint iteration, you then need a database schema migration tool, like Flyway.
The comment doesn't require a Domain Model mapping, since it's a relational specific description. The Domain Model should make use JavaDocs to document each field meaning (according to a specific domain logic requirement).
Upvotes: 2
Reputation: 5341
I found answer for my own question.
I am not sure is it ok? Will it work for all Databases?
For sure it works for mysql.
Here is the working code:
@Column(columnDefinition=" INT(11) NOT NULL COMMENT '0 for no action, 1 for executed, 2 for validated, 3 for aproved'")
private int status;
Upvotes: 10