hibernate grandfather , parent ,child mapping problem
I have a problem where I have this three grandfather parent and child and I am trying to get from parent all his children he gives me an half of is children odd as it seemes I just cant understand why can someone please help me ?
this is the case:
parentDao.getChildrenSet();//problem... return half every time
parentDao.getChildrenWithSqlQuery();//works o.k but it's not the w开发者_运维百科ay to work
I think my problem is in the mapping therefore guidance would b appreciated thanks.
Notice : every entity got her get/set empty c'tor hash and equals (without the mapping entitys inside hash and equals)
EDIT:i am not using embedded id , my boss desiccation there are pro's and con's and this is what he has thought ...
grandfather:
@Entity
@Table(name = "grandfather")
@SequenceGenerator(name = "grandfather_SEQ", sequenceName = "grandfather_SEQ", allocationSize = 1)
public class ComponentTreeProfile extends TrackableEntity {
// id
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "grandfather_SEQ")
@Column(name = "grandfather_ID")
private Integer grandfatherId;
@OneToMany(mappedBy = "grandfather")
private Set<Parent> parent;
//some info equals, hashcode empty c'tor ...}
parent
@Entity
@Table(name = "parent")
public class Parent extends TrackableEntity implements
Serializable {
@Id
@Column(name = "grandfather_ID", nullable = false, insertable = true, updatable = false)
private Integer grandfatherId;
@Id
//seq is taking cared
@Column(name = "parent_ID", nullable = false, insertable = true, updatable = false)
private Integer profileModelId;
@ManyToOne
@JoinColumn(name = "grandfather_ID", nullable = false, insertable = false, updatable = false)
private Grandfather grandfather;
@OneToMany(mappedBy = "parent", fetch = FetchType.EAGER)
private List<Child> child = new LinkedList<Child>();}
child
@Entity
@Table(name = "Child") public class Child extends TrackableEntity implements Serializable {
private static final long serialVersionUID = -2451336147838275971L;
// id's fields
@Id
@Column(name = "grandfather_ID", nullable = false, insertable = true, updatable = false)
private Integer profileId;
@Id
@Column(name = "Parent_ID", nullable = false, insertable = true, updatable = false)
private Integer ParentId;
@Id
//seq is being handled
@Column(name = "child_ID", nullable = false)
private Integer ChildTypeId;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumns({
@JoinColumn(name = "grandFather_ID", insertable = false, updatable = false),
@JoinColumn(name = "parent_ID", insertable = false, updatable = false) })
private Parent parent;}
The problem is the Parent class, the id is not properly mapped and you're mapping the same column twice. Mapping the column twice is not a problem, but it can be avoided by mapping Grandfather as part of the id. You have an example of how to do this on the hibernate annotations documentation.
Edit
I just read the documentation myself and found that Hibernate now supports to have more than one
column annotated with @Id, without defining an Embedded id or IdClass. I reckon that hibernate is having trouble to recognize in which order it should use the 2 integers to map the Parent.
Try adding the parameters referencedColumnName
on both @JoinColumn in Child.parent.
精彩评论