同一实体上的 JPA 父子关系(视图)

发布于 2025-01-10 07:59:12 字数 783 浏览 0 评论 0原文

我有一个观点,它有亲子关系。所以我的实体是这样的:

@Entity
@Table(name = "assessment_v", schema = "hlt_hrsc")
public class HrscLabellingAssessmentVEntity {

@Id
private String id;

@Column(name = "child_id")
private String childId;

@ManyToOne(cascade = {CascadeType.ALL})
@JoinColumn(name = "id")
private HrscLabellingAssessmentVEntity parent;

@OneToMany(mappedBy = "parent")
private Set<HrscLabellingAssessmentVEntity> child = new HashSet<>();
}

当我启动我的应用程序时,它会抛出一个错误: 嵌套异常是org.hibernate.MappingException:无法确定类型:java.util.Set

请找到父子视图的下图,例如:

id          child_id
120.35871   120.35872
120.35872   null

这里120.35872是父记录,120.35871是儿童记录。 ParentId 维护在 child_id 列中。我的要求是,当我尝试获取父记录时,它也应该有子记录。

我做错了什么?

I have a view which has a parent-child relationship. So I have my entity this way:

@Entity
@Table(name = "assessment_v", schema = "hlt_hrsc")
public class HrscLabellingAssessmentVEntity {

@Id
private String id;

@Column(name = "child_id")
private String childId;

@ManyToOne(cascade = {CascadeType.ALL})
@JoinColumn(name = "id")
private HrscLabellingAssessmentVEntity parent;

@OneToMany(mappedBy = "parent")
private Set<HrscLabellingAssessmentVEntity> child = new HashSet<>();
}

when I start my application it throws an error :
nested exception is org.hibernate.MappingException: Could not determine type for: java.util.Set

Please find the below image of the view for parent child eg:

id          child_id
120.35871   120.35872
120.35872   null

Here 120.35872 is the parent record and 120.35871 is the child record. The parentId is maintained on the child_id column. My requirement is when I try to fetch the parent it should have the child records also.

what am I doing wrong??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

上课铃就是安魂曲 2025-01-17 07:59:12

请检查这个例子

    @Entity
    @Table(name = "assessment_v", schema = "hlt_hrsc")
    public class HrscLabellingAssessmentVEntity {
    
    @Id
    private String id;
    
    @Column(name = "child_id")
    private String childId;
     
    @JsonBackReference
    @JoinColumn(name = "parent_id", referencedColumnName = "id")
    @ManyToOne(fetch = FetchType.LAZY)
    @Fetch(FetchMode.JOIN)
    private HrscLabellingAssessmentVEntity parentId;

     @JsonManagedReference
     @OneToMany(mappedBy = "parentId", cascade = CascadeType.ALL)
     private Set<HrscLabellingAssessmentVEntity> child = new HashSet<>();
    }

Please check this example

    @Entity
    @Table(name = "assessment_v", schema = "hlt_hrsc")
    public class HrscLabellingAssessmentVEntity {
    
    @Id
    private String id;
    
    @Column(name = "child_id")
    private String childId;
     
    @JsonBackReference
    @JoinColumn(name = "parent_id", referencedColumnName = "id")
    @ManyToOne(fetch = FetchType.LAZY)
    @Fetch(FetchMode.JOIN)
    private HrscLabellingAssessmentVEntity parentId;

     @JsonManagedReference
     @OneToMany(mappedBy = "parentId", cascade = CascadeType.ALL)
     private Set<HrscLabellingAssessmentVEntity> child = new HashSet<>();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文