开发者

How to maintain the column order when creating a new table using hibernate? [duplicate]

开发者 https://www.devze.com 2023-03-04 22:07 出处:网络
This question already has answers here: Wrong ordering in generated table in jpa (6 answers) Closed 4 years ago.
This question already has answers here: Wrong ordering in generated table in jpa (6 answers) Closed 4 years ago.

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 Str开发者_开发问答ing 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号