2011-05-11 44 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一起檢查它是否與實體匹配)。應用程序投入生產後,通過腳本維護數據庫變得更加必要。

相關問題