使用hibernate创建新表时如何保持列顺序?

发布于 2024-11-07 01:18:25 字数 910 浏览 1 评论 0原文

这是我的pojo,注释为实体

@Entity
@Table(name = "book", catalog = "book_db")
public class Book {
    private Integer bookId;
    private String bookName;
    private String bookShortDesc;
    private String bookDesc;
    private String bookAuthor;
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "book_id", unique = true, nullable = false)
public Integer getBookId() {
    return this.bookId;
}

@Column(name = "book_name", nullable = false, length = 256)
public String getBookName() {
    return this.bookName;
}
@Column(name = "book_short_desc", nullable = false, length = 1024)
public String getBookShortDesc() {
    return this.bookShortDesc;
}

等......

上面的实体是使用注释创建的,当我查看mysql数据库时,列不是按顺序创建的,我在下面写了,相反,第一列是book_id,然后是 book_desc,然后是 book_athor,然后是 book_short_desc,然后是 book_name。

我的问题是我如何告诉 hibernate 按照我在 java 代码中编写的顺序创建列?

有这方面的注释吗??

问候

This is my pojo annotated as entity

@Entity
@Table(name = "book", catalog = "book_db")
public class Book {
    private Integer bookId;
    private String bookName;
    private String bookShortDesc;
    private String bookDesc;
    private String bookAuthor;
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "book_id", unique = true, nullable = false)
public Integer getBookId() {
    return this.bookId;
}

@Column(name = "book_name", nullable = false, length = 256)
public String getBookName() {
    return this.bookName;
}
@Column(name = "book_short_desc", nullable = false, length = 1024)
public String getBookShortDesc() {
    return this.bookShortDesc;
}

etc......

the above entity is created using annotation, when i look to the mysql database,the columns are not created in the order, i have written below, instead , first columns is the book_id, then book_desc, then book_athor, then book_short_desc then book_name.

my question is how can i tell hibernate to create the columns the same order as i have written in the java code ??

is there any annotation for that ??

regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吃不饱 2024-11-14 01:18:25

假设您的意思是通过设置为 CREATE 或类似的 hbm2ddl.auto 生成数据库表,据 Hibernate 的一位成员称,至少在 2008 年,无法指定列的顺序 团队。

我的建议是通过单独维护的数据库脚本创建数据库(可能与脚本部署/迁移工具结合使用,例如 Flyway 也许还可以使用 hbm2ddl.auto = VALIDATE 来检查结果模式是否与实体匹配)。一旦应用程序投入生产,通过脚本维护数据库就变得更加必要。

Assuming you mean the database tables are generated via hbm2ddl.auto being set to CREATE or similar, there is no way to specify the order of the columns at least in 2008, according to one member of the Hibernate team.

My advice would be to create the database via separately maintained database scripts (possibly in conjunction with a script deployment/migration tool such as Flyway and perhaps also with hbm2ddl.auto = VALIDATE to check the resulting schema matches the entities). Maintaining the database via scripts becomes much more necessary once the application goes into production.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文