【问题标题】:Hibernate annotation. How to annotate?休眠注释。如何注释?
【发布时间】:2015-02-24 14:46:30
【问题描述】:

我正在 Java EE 中制作一个小型库项目。我创建了 3 个包含作者、流派和书籍的表格和班级。现在我正在尝试使用休眠连接它,但我不知道如何配置注释......请帮助我:)

bookTable:

|身份证 |作者_id |标题 |流派 ID |描述 |照片 |

类型表:

|流派 ID |流派 |

作者表:

|作者_id |作者|

结构简单:

bookTable.author_id - authorTable.author_id = ManyToMany

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

}

【问题讨论】:

    标签: java hibernate


    【解决方案1】:

    应该是单向关联还是双向关联? 看看这个例子:

    https://howtoprogramwithjava.com/hibernate-manytomany-unidirectional-bidirectional/

    它甚至使用你的实体名称:)

    【讨论】:

      【解决方案2】:

      那是实体,没有 pojo,但到目前为止它们看起来不错。对于他们,你通常不需要照顾。最好的方法是在将项目与数据库连接后自动生成它们。 Hibernate 会照顾好一切。更有趣的是你的 DAO 的外观,bcz。那是与您的数据库的层通信。实体只是Java端数据库表的表示。 I guess you already connected your project with the database? 请提供您的数据库访问对象 (DAO) 以获得进一步帮助。如果你到目前为止还没有here you can get help

      【讨论】:

        猜你喜欢
        • 2012-08-05
        • 1970-01-01
        • 1970-01-01
        • 2017-09-15
        • 2018-01-12
        • 2012-09-11
        • 1970-01-01
        • 1970-01-01
        • 2017-01-24
        相关资源
        最近更新 更多