如何使用hibernate创建新表时维护列顺序? [英] How to maintain the column order when creating a new table using hibernate?
问题描述
这是我的pojo注释为实体
@Entity
@Table(name =book,catalog =book_db)
public class Book {
private Integer bookId;
私人字符串bookName;
private String bookShortDesc;
private String bookDesc;
私人字符串bookAuthor;
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name =book_id,unique = true,nullable = false)
public Integer getBookId (){
return this.bookId;
$ b $ @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 then book_name。
我的问题是如何告诉hibernate创建与我在java代码中写入的顺序相同的列?
是否有任何注释?
问候
hbm2ddl.auto
设置为 CREATE
或类似的,没有办法指定列的顺序 至少在2008年,根据Hibernate团队的一名成员。我的建议是通过sep创建数据库有效维护的数据库脚本(可能与脚本部署/迁移工具相结合,例如 Flyway ,也可能与 hbm2ddl.auto = VALIDATE
来检查生成的模式与实体匹配)。一旦应用程序投入生产,通过脚本维护数据库变得更加必要。
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
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.
这篇关于如何使用hibernate创建新表时维护列顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!