'JPA Parent Child relationship on same entity(view)
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??
Solution 1:[1]
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<>();
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Zaur Farrukhzada |
