java JPA @Column 注释以创建注释/描述

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28773022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 14:10:05  来源:igfitidea点击:

JPA @Column annotation to create comment/description

javamysqlsqlhibernatejpa

提问by masterdany88

I was wondering is it possible to create from jpa/hibernate annotation a database column description/comment like this:

我想知道是否可以从 jpa/hibernate 注释创建这样的数据库列描述/评论:

ALTER TABLE tablename CHANGE status status INT(11) NOT NULL COMMENT 'sample description/comment';

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.

这将是很棒的功能,但我在 JPA 规范中找不到任何关于此的信息。

Maybe I should use @Column(columnDefinition="")property, but I dont have any clue. Please help

也许我应该使用@Column(columnDefinition="")财产,但我没有任何线索。请帮忙

采纳答案by masterdany88

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.

肯定它适用于 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;

回答by Guillaume Husta

Comment description already exists for Table annotations. We must use the Hibernate @Table annotation for that, complementary with the JPA @Table annotation.

表注释的注释描述已存在。为此,我们必须使用 Hibernate @Table 注释,与 JPA @Table 注释互补。

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.

关于该专栏的评论:即使在 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 ?

很久以前(2007 年)就这个主题提交了一个问题,但仍未解决:支持 @Table 和 @Column 上的 @Comment 或 column 属性。自 2012 年以来被遗弃?

I also found comment use in org.hibernate.dialect.Dialect:

我还在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).

例如 PostgreSQL81Dialect 支持它(supportsCommentOn() 返回 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 :

它允许使用 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';

It seems to be used there : org.hibernate.tool.schema.internal.StandardTableExporter#getSqlCreateStrings

它似乎在那里使用:org.hibernate.tool.schema.internal.StandardTableExporter#getSqlCreateStrings

The column's comment is extracted from the Hibernate Mapping with org.hibernate.mapping.Column#getComment.

该列的注释是从带有org.hibernate.mapping.Column#getComment的 Hibernate Mapping 中提取的。

Finally, If using Hibernate Toolsand reverse engineering, the column's comment is extracted from JDBC DatabaseMetaData, with org.hibernate.cfg.reveng.BasicColumnProcessor#processBasicColumns.

最后,如果使用Hibernate Tools和逆向工程,则列的注释是从 JDBC DatabaseMetaData 中提取的,使用org.hibernate.cfg.reveng.BasicColumnProcessor#processBasicColumns

-> String comment = (String) columnRs.get("REMARKS");

回答by Vlad Mihalcea

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.

虽然将领域模型映射到关系数据需要 Hibernate 映射,但将此功能扩展到生成数据库模式也不是一个好主意。

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.

这可能适用于一个相对较小的项目,但是当您有一个大中型企业应用程序时,它会随着每次 Sprint 迭代而发展,那么您就需要一个数据库模式迁移工具,比如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).

注释不需要域模型映射,因为它是关系特定的描述。域模型应该使用 JavaDocs 来记录每个字段的含义(根据特定的域逻辑要求)。