hibernate爷爷,父母,孩子映射问题

时间:2011-05-03 07:34:45

标签: hibernate mapping hibernate-mapping

我有一个问题,我有这三个祖父的父母和孩子 我试图从父母那里得到他所有的孩子,他给了我一半儿童奇怪,因为它似乎我不能理解为什么有人可以帮助我?

情况就是这样:

      parentDao.getChildrenSet();//problem... return half every time
      parentDao.getChildrenWithSqlQuery();//works o.k but it's not the way to work 

我认为我的问题在于制图,因此指导意见表示赞赏 谢谢。

注意:每个实体都得到她的get / set空c'tor hash和equals (没有hash和equals中的映射实体)

编辑:我没有使用嵌入式ID,我的老板干燥有专家和骗子,这就是他所想的......

祖父:

  @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 ...}

父母

  @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>();}

@Entity

@Table(name =“Child”) public class Child扩展了TrackableEntity实现         可序列化{

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;}

1 个答案:

答案 0 :(得分:0)

问题是Parent类,id未正确映射,并且您将同一列映射两次。将列映射两次不是问题,但可以通过将祖父映射为id的一部分来避免它。您有一个如何在hibernate annotations documentation上执行此操作的示例。

修改

我只是自己阅读文档,发现Hibernate现在支持多个

用@Id注释的列,没有定义嵌入式ID或IdClass。 我认为hibernate很难识别它应该使用2个整数来映射Parent。

尝试在Child.parent的@JoinColumn上添加参数referencedColumnName