gpt4 book ai didi

java - hibernate MySQL

转载 作者:行者123 更新时间:2023-11-29 07:51:28 27 4
gpt4 key购买 nike

假设我有这样的 POJO,并且我想将文章插入 MySQL 数据库。

@Entity
@Table(name = "article")
public class Article{
private String author;
private String body;
private int commentCount;
private List<EntryComment> comments;


@Entity
@Table(name = "comments")
public class Comment{
private String author;
private String body;
private int voteCount;

我想将评论放入另一个表中,而不删除“Article.txt”中的字段 comments。是否可以使用 hibernate 注释将注释放在单独的表中?

最佳答案

当然是这样。我建议您对字符串和 int 元素具有 @Column 的所有元素使用注释,但是对于注释,只需查看下面的代码:

文章类别:

@Entity
@Table(name = "article")
public class Article{
private String author;
private String body;
private int commentCount;

@OneToMany(mappedBy = "article")
@LazyCollection(LazyCollectionOption.TRUE)
private List<EntryComment> comments;

评论类:

@Entity
@Table(name = "comments")
public class Comment{

@ManyToOne(fetch = FetchType.LAZY)
private Article article;

private String author;
private String body;
private int voteCount;

关于java - hibernate MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26314769/

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