gpt4 book ai didi

java - hibernate 注释。如何注释?

转载 作者:行者123 更新时间:2023-12-01 11:52:26 25 4
gpt4 key购买 nike

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

图书表:

| id_book |作者 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;
}

}

最佳答案

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

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

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

关于java - hibernate 注释。如何注释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28698789/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com