JPA @Column 注释创建 comment/description
JPA @Column annotation to create comment/description
我想知道是否可以从 jpa/hibernate 注释创建数据库列 description/comment,如下所示:
ALTER TABLE tablename CHANGE status status INT(11) NOT NULL COMMENT 'sample description/comment';
这将是一个很棒的功能,但我在 JPA 规范中找不到任何相关信息。
也许我应该使用 @Column(columnDefinition="")
属性,但我没有任何线索。
请帮忙
我找到了我自己问题的答案。
我不确定可以吗?它适用于所有数据库吗?
当然 它适用于 mysql。
这是工作代码:
@Column(columnDefinition=" INT(11) NOT NULL COMMENT '0 for no action, 1 for executed, 2 for validated, 3 for aproved'")
private int status;
虽然将域模型映射到关系数据需要 Hibernate 映射,但将此功能也扩展到生成数据库模式并不是一个好主意。
这可能适用于相对较小的项目,但是当您有一个大中型企业应用程序时,它会随着每次 Sprint 迭代而发展,然后您需要一个数据库架构迁移工具,例如 Flyway。
评论不需要域模型映射,因为它是关系特定的描述。领域模型应该使用 JavaDocs 来记录每个字段的含义(根据特定的领域逻辑要求)。
Table 个注释 的注释说明已存在。为此,我们必须使用 Hibernate @Table 注释,与 JPA @Table 注释互补。
例如:
@javax.persistence.Table( name = "Cat" )
@org.hibernate.annotations.Table( comment = "Table for cats" )
public class Cat {
...
关于专栏的评论:
似乎没有等效项,即使在 Hibernate 5.2 / JPA 2.1 中也是如此。
很久以前(2007 年)就此主题提交了一个问题,但仍未解决:Support @Comment or column attribute on @Table and @Column。自 2012 年以来就被遗弃了?
我还发现 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 "";
}
例如 PostgreSQL81Dialect 支持它(supportsCommentOn() returns true)。
它允许使用 SQL 命令 "COMMENT ON ...",
就像在 PostgreSQL (https://www.postgresql.org/docs/current/static/sql-comment.html).
例如:
COMMENT ON TABLE my_schema.my_table IS 'Employee Information';
COMMENT ON COLUMN my_table.my_column IS 'Employee ID number';
那里好像用到了:org.hibernate.tool.schema.internal.StandardTableExporter#getSqlCreateStrings
该列的评论是从 Hibernate 映射中提取的org.hibernate.mapping.Column#getComment。
最后,如果使用 Hibernate Tools 和逆向工程,列的注释是从 JDBC DatabaseMetaData 中提取的,使用 org.hibernate.cfg.reveng.BasicColumnProcessor# processBasicColumns.
-> String comment = (String) columnRs.get("REMARKS");
您可以使用 @Comment
注释。
此注解在hibernate 5.6.0
版本(2021-09-21发布)中引入,Spring Boot 2.6
(2021-11-17发布)使用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;
}
我想知道是否可以从 jpa/hibernate 注释创建数据库列 description/comment,如下所示:
ALTER TABLE tablename CHANGE status status INT(11) NOT NULL COMMENT 'sample description/comment';
这将是一个很棒的功能,但我在 JPA 规范中找不到任何相关信息。
也许我应该使用 @Column(columnDefinition="")
属性,但我没有任何线索。
请帮忙
我找到了我自己问题的答案。
我不确定可以吗?它适用于所有数据库吗?
当然 它适用于 mysql。
这是工作代码:
@Column(columnDefinition=" INT(11) NOT NULL COMMENT '0 for no action, 1 for executed, 2 for validated, 3 for aproved'")
private int status;
虽然将域模型映射到关系数据需要 Hibernate 映射,但将此功能也扩展到生成数据库模式并不是一个好主意。
这可能适用于相对较小的项目,但是当您有一个大中型企业应用程序时,它会随着每次 Sprint 迭代而发展,然后您需要一个数据库架构迁移工具,例如 Flyway。
评论不需要域模型映射,因为它是关系特定的描述。领域模型应该使用 JavaDocs 来记录每个字段的含义(根据特定的领域逻辑要求)。
Table 个注释 的注释说明已存在。为此,我们必须使用 Hibernate @Table 注释,与 JPA @Table 注释互补。
例如:
@javax.persistence.Table( name = "Cat" )
@org.hibernate.annotations.Table( comment = "Table for cats" )
public class Cat {
...
关于专栏的评论: 似乎没有等效项,即使在 Hibernate 5.2 / JPA 2.1 中也是如此。
很久以前(2007 年)就此主题提交了一个问题,但仍未解决:Support @Comment or column attribute on @Table and @Column。自 2012 年以来就被遗弃了?
我还发现 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 "";
}
例如 PostgreSQL81Dialect 支持它(supportsCommentOn() returns true)。
它允许使用 SQL 命令 "COMMENT ON ...",
就像在 PostgreSQL (https://www.postgresql.org/docs/current/static/sql-comment.html).
例如:
COMMENT ON TABLE my_schema.my_table IS 'Employee Information';
COMMENT ON COLUMN my_table.my_column IS 'Employee ID number';
那里好像用到了:org.hibernate.tool.schema.internal.StandardTableExporter#getSqlCreateStrings
该列的评论是从 Hibernate 映射中提取的org.hibernate.mapping.Column#getComment。
最后,如果使用 Hibernate Tools 和逆向工程,列的注释是从 JDBC DatabaseMetaData 中提取的,使用 org.hibernate.cfg.reveng.BasicColumnProcessor# processBasicColumns.
-> String comment = (String) columnRs.get("REMARKS");
您可以使用 @Comment
注释。
此注解在hibernate 5.6.0
版本(2021-09-21发布)中引入,Spring Boot 2.6
(2021-11-17发布)使用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;
}