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

mas*_*y88 6 java mysql sql hibernate jpa

我想知道是否有可能从jpa/hibernate注释创建数据库列描述/注释,如下所示:

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

它将是很棒的功能,但我无法在JPA规范中找到任何相关信息.

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

mas*_*y88 8

我找到了自己问题的答案.

我不确定是否可以?它适用于所有数据库吗?

肯定它适用于mysql.

这是工作代码:

@Column(columnDefinition=" INT(11) NOT NULL COMMENT '0 for no action, 1 for executed, 2 for validated, 3 for aproved'")
private int status;
Run Code Online (Sandbox Code Playgroud)

  • 由于并非所有 RDBMS 都支持针对 COLUMN 的“COMMENT”(Postgresql、HSQLDB、Derby 等),因此这肯定只适用于非常有限的 RDBMS 集。此外,将其放入注释中意味着编译后的代码与允许该语法的 RDBMS 相关联,而如果将其放入 XML 文件中,则可以根据所使用的 RDBMS 交换 orm.xml 文件。 (2认同)

Gui*_*sta 6

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

例如 :

@javax.persistence.Table( name = "Cat" )
@org.hibernate.annotations.Table( comment = "Table for cats" )
public class Cat {
...
Run Code Online (Sandbox Code Playgroud)

关于该专栏的评论:即使在 Hibernate 5.2 / JPA 2.1 中,似乎也没有等价物。

很久以前(2007 年)就这个主题提交了一个问题,但仍未解决:支持 @Table 和 @Column 上的 @Comment 或列属性。自 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 "";
}
Run Code Online (Sandbox Code Playgroud)

例如 PostgreSQL81Dialect 支持它(supportsCommentOn() 返回 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';
Run Code Online (Sandbox Code Playgroud)

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

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

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

-> String comment = (String) columnRs.get("REMARKS");
Run Code Online (Sandbox Code Playgroud)


wpl*_*g11 5

您可以使用@Comment注释。

该注解是在hibernate 5.6.02021-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;

}
Run Code Online (Sandbox Code Playgroud)