无法从反侧看到拥有方 JPA 休眠



我有一段代码:

@Entity(name = "Person")
@Table(name = "person")
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToMany( mappedBy = "person", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<Post> posts = new ArrayList<Post>();
}

和:

@Entity(name = "Post")
@Table(name = "post")
public class Post extends BaseEntity{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne(
fetch = FetchType.LAZY
)
@JoinColumn(name="person_id")
Person person;
@Column(name = "text", nullable = true)
private String text;
}

问题是,当我连接到Hibernate数据库时,我可以在Post实体中看到Person Id,但在Person实体中看不到Post Id。也没有person_post表。如何查看属于该人员的帖子?我想列出使用thymelaf的人的帖子,但这样的东西不起作用:

<tr th:each="post : ${person.posts}">
<td th:text="${post.text}">nothing in here</td>
</tr>

错误表示thymelaf无法亲自查看帖子。它应该是这样工作的吗?如何列出个人的帖子?提前感谢,Tomasz

您正在定义一个1:N relationshop,这意味着在Post表中有person_id外键,但没有中间表person_Post。然而,person.posts应该会给你想要的结果——你能添加逻辑吗?如何添加person/Post和模板的控制器方法?

顺便提一下,建议用户LAZY抓取:https://vladmihalcea.com/eager-fetching-is-a-code-smell/

最新更新