JPA @Column annotation to create comment/description
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;
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");
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).