使用hibernate创建新表时如何保持列顺序?
这是我的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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设您的意思是通过设置为
CREATE
或类似的hbm2ddl.auto
生成数据库表,据 Hibernate 的一位成员称,至少在 2008 年,无法指定列的顺序 团队。。我的建议是通过单独维护的数据库脚本创建数据库(可能与脚本部署/迁移工具结合使用,例如 Flyway 也许还可以使用
hbm2ddl.auto = VALIDATE
来检查结果模式是否与实体匹配)。一旦应用程序投入生产,通过脚本维护数据库就变得更加必要。Assuming you mean the database tables are generated via
hbm2ddl.auto
being set toCREATE
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.