Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JPA加入childEntity.childForeignEntity_Jpa_Spring Data Jpa_Persistence - Fatal编程技术网

JPA加入childEntity.childForeignEntity

JPA加入childEntity.childForeignEntity,jpa,spring-data-jpa,persistence,Jpa,Spring Data Jpa,Persistence,我不知道用一个词怎么称呼它,但让我详细解释一下 假设我的数据库中有以下表/架构: 以及相应的以下类别: 1.邮政 @Entity @Table(name = "posts") public class Post { @Id private Long id; @Column(name = "text") private String text; @OneToMany(fetch = FetchType.LAZY,

我不知道用一个词怎么称呼它,但让我详细解释一下

假设我的数据库中有以下表/架构:

以及相应的以下类别:

1.邮政

@Entity
@Table(name = "posts")
public class Post {
    @Id
    private Long id;

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

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "post")
    private Set<PostComment> postComments = new HashSet<>();
}
3.用户

@Entity
@Table(name = "users")
public class User {
    @Id
    private Long id;

    @Column(name = "some_attributes")
    private String someAttributes;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "user")
    private Set<PostComment> postComments = new HashSet<>();
}
@实体
@表(name=“users”)
公共类用户{
@身份证
私人长id;
@列(name=“某些属性”)
私有字符串属性;
@OneToMany(fetch=FetchType.LAZY,mappedBy=“user”)
private Set postComments=new HashSet();
}
如何通过PostComment与用户加入帖子,以便在我的帖子实体中让所有用户发表评论:

@Entity
@Table(name = "posts")
public class Post {
    ....
    
    //@ join with post_comments.user_id
    private Set<User> users = new HashSet<>();

    ....
}
@实体
@表(name=“posts”)
公营职位{
....
//@加入post_comments.user_id
private Set users=new HashSet();
....
}

好吧,只需获取
PostComment.user
其中
PostComment.post
等于您的帖子

@Query("select pc.user from PostComment pc where pc.post = :post")
List<User> getUsersWithComments(@Param("post") Post post);
我不知道这是怎么回事:

@Column(name = "post_id")
private Long postId;

@Column(name = "user_id")
private Long userId;
还是这个

@JoinColumn(name="user_id")
@JoinColumn(name="post_id")
你不应该这样做:

 = new HashSet<>();

对我来说,在实体字段上有@Query是至关重要的,这可能吗?我从未尝试过。
@JoinColumn(name="user_id")
@JoinColumn(name="post_id")
 = new HashSet<>();
fetch = FetchType.LAZY,