2011-05-11 40 views
7

这是我的POJO标注为实体如何使用hibernate创建新表时维护列顺序?

上述实体使用注释创建的,当我看向mysql数据库,这些列未在创建

@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; 
} 

等......顺序,我写下面,而是,第一列是book_id,然后是book_desc,然后是book_athor,然后是book_short_desc,然后是book_name。

我的问题是我如何告诉hibernate创建列的顺序与我在java代码中写的相同?

是否有任何注释?

问候

+0

你的代码不应该依赖于数据库中列的排序。实际上,从技术上讲,不能保证在SELECT *时列将以特定顺序返回。如果您需要特定顺序的列,请在您的SQL查询中指定它。 – artbristol 2017-09-30 10:22:31

回答

2

假设你指的是数据库中的表是通过hbm2ddl.auto生成设定为CREATE或类似,there is no way to specify the order of the columns至少在2008年,根据休眠团队的一个成员。

我的建议是通过单独维护的数据库脚本创建数据库(可能与hbm2ddl.auto = VALIDATE一起检查它是否与实体匹配)。应用程序投入生产后,通过脚本维护数据库变得更加必要。

相关问题