Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 270
Sony Avatar asked May 11 '11 13:05

Sony


1 Answers

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.

like image 169
Steve Chambers Avatar answered Sep 30 '22 02:09

Steve Chambers