2015-02-24 193 views
0

我正在Java EE中製作一個小型庫項目。我創作了3個表格,並與作者,流派和書籍一起上課。現在我嘗試使用Hibernate來連接它,但我沒有IDE如何confire註解:請幫我:)Hibernate註釋。如何註釋?

bookTable:

| id_book | author_id |標題| genre_id |描述|照片|

genreTable:

| genre_id |流派|

authorTable:

| author_id |作者|

這是很容易的結構:

bookTable.author_id - authorTable.author_id =多對多

bookTable.genre_id - genreTable.genre_id = OneToOne

下面有我的POJO類:

本書

@Entity 
@Table(name = "books") 
public class Book implements Serializable{ 

    private static final long serialVersionUID = -5057364006691079475L; 

    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    @Column(name = "user_id") 
    private Integer user_id; 
    private Author author; 
    private String description; 
    private BookGenre genre; 
    private String title; 


    public String getDescription() { 
     return description; 
    } 

    public void setDescription(String description) { 
     this.description = description; 
    } 

    public String getTitle() { 
     return title; 
    } 

    public void setTitle(String title) { 
     this.title = title; 
    } 

    public Integer getUser_id() { 
     return user_id; 
    } 

    public void setUser_id(Integer user_id) { 
     this.user_id = user_id; 
    } 

    public Author getAuthor() { 
     return author; 
    } 

    public void setAuthor(Author author) { 
     this.author = author; 
    } 

    public BookGenre getGenre() { 
     return genre; 
    } 

    public void setGenre(BookGenre genre) { 
     this.genre = genre; 
    } 

} 

作者

@Entity 
@Table(name = "author") 
public class Author implements Serializable{ 

    private static final long serialVersionUID = 1L; 

    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    @Column(name = "author_id") 
    private Integer author_id; 

    @Column(name = "author") 
    private String author; 

    public Integer getAuthor_id() { 
     return author_id; 
    } 

    public void setAuthor_id(Integer author_id) { 
     this.author_id = author_id; 
    } 

    public String getAuthor() { 
     return author; 
    } 

    public void setAuthor(String author) { 
     this.author = author; 
    } 
} 

類型

@Entity 
@Table(name = "genre") 
public class BookGenre implements Serializable{ 

    private static final long serialVersionUID = 1L; 

    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    @Column(name = "genre_id") 
    private Integer genreId; 

    @Column(name = "genre") 
    private String genre; 


    public Integer getGenreId() { 
     return genreId; 
    } 
    public void setGenreId(Integer genreId) { 
     this.genreId = genreId; 
    } 
    public String getGenre() { 
     return genre; 
    } 
    public void setGenre(String genre) { 
     this.genre = genre; 
    } 

} 

回答

0

這是實體來說,沒有的POJO,但他們看起來還不錯。對他們來說,你通常不需要照顧。最好的方法是,在將項目與數據庫連接後,自動生成它們。 Hibernate會照顧一切。更有趣的是你的DAO看起來如何,bcz。即與數據庫進行圖層通信。該實體只是Java端數據庫表的表示。 I guess you already connected your project with the database? 請提供您的數據庫訪問對象(DAO)以獲得進一步的幫助。如果你還沒有到目前爲止here you can get help