Hibernate - 实体通过带有附加列的连接表引用自身

时间:2011-09-22 06:39:10

标签: hibernate jpa hibernate-annotations

我有一个带注释的实体,名为Part。它由“id”和一些其他变量组成。我有另一个名为“bomdefinitions”的表。这个表有三列(qty,parent,target)(我理解目标是一个愚蠢的名称,但由于数据库已经存在,我无法更改它)。这个想法是一个部分可以由许多其他部分组成,也可以用于许多其他部分,表“bomdefinitions”表示父母有什么“目标”与它相关联以及它使用了多少。

问题是,如果我在Part.java中注释掉一个OneToMany关系并离开另一个,它工作正常,但如果我在我的代码中都有它们,那么尝试访问其中任何一个会导致无限循环Hibernate查询。

我可以找到将实体映射到自身的示例,使用带有额外列的连接表映射实体的示例,以及执行注释的各种方法的示例,但没有在单个应用程序中完成所有这些操作,以及我能找到的个别例子似乎没有帮助。

任何对Hibernate有更多经验的人都对我做错了什么有任何想法?

编辑(此代码导致无限循环):

getHibernateTemplate().get(Part.class, id).getParentBomDefinitions();

这是输出中的查询 - 它始于:

Hibernate: select part0_.id as id0_3_, childbomde1_.target as target0_5_, childbomde1_.parent as parent5_, childbomde1_.target as target5_, childbomde1_.parent as parent4_0_, childbomde1_.target as target4_0_, childbomde1_.qty as qty4_0_, part2_.id as id0_1_ from parts part0_ left outer join bomdefinitions childbomde1_ on part0_.id=childbomde1_.target left outer join parts part2_ on childbomde1_.parent=part2_.id
Hibernate: select parentbomd0_.parent as parent0_2_, parentbomd0_.parent as parent2_, parentbomd0_.target as target2_, parentbomd0_.parent as parent4_1_, parentbomd0_.target as target4_1_, parentbomd0_.qty as qty4_1_, part1_.id as id0_0_ from bomdefinitions parentbomd0_ left outer join parts part1_ on parentbomd0_.target=part1_.id where parentbomd0_.parent=?

然后这两个查询无限重复:

Hibernate: select parentbomd0_.parent as parent0_2_, parentbomd0_.parent as parent2_, parentbomd0_.target as target2_, parentbomd0_.parent as parent4_1_, parentbomd0_.target as target4_1_, parentbomd0_.qty as qty4_1_, part1_.id as id0_0_ from bomdefinitions parentbomd0_ left outer join parts part1_ on parentbomd0_.target=part1_.id where parentbomd0_.parent=?
Hibernate: select childbomde0_.target as target0_2_, childbomde0_.parent as parent2_, childbomde0_.target as target2_, childbomde0_.parent as parent4_1_, childbomde0_.target as target4_1_, childbomde0_.qty as qty4_1_, part1_.id as id0_0_ from bomdefinitions childbomde0_ left outer join parts part1_ on childbomde0_.parent=part1_.id where childbomde0_.target=?

BomDefinition.java(几乎取自“Java Persistence with Hibernate”)

@Entity
@Table(name = "bomdefinitions")
public class BomDefinition {

@Embeddable
public static class Id implements Serializable {

    @Column(name = "target")
    private String targetId;

    @Column(name = "parent")
    private String parentId;

    public Id() {}
    public Id(String targetId, String parentId) {
        this.targetId = targetId;
        this.parentId = parentId;
    }
    public boolean equals(Object o) {
        if (o != null && o instanceof Id) {
            Id that = (Id) o;
            return this.targetId.equals(that.targetId) && this.parentId.equals(that.parentId);
        } else {
            return false;
        }
    }
    public int hashCode() {
        return targetId.hashCode() + parentId.hashCode();
    }
}

@EmbeddedId
private Id id = new Id();

@Column(name = "qty")
private String quantity;

@ManyToOne
@JoinColumn(name = "parent",
            insertable = false,
            updatable = false)
private Part parent;

@ManyToOne
@JoinColumn(name = "target",
            insertable = false,
            updatable = false)
private Part child;

public BomDefinition() {}

public BomDefinition(String quantity, Part parent, Part child) {
    this.quantity = quantity;
    this.parent = parent;
    this.child = child;

    this.id.parentId = parent.getId();
    this.id.targetId = child.getId();
}

// Getters and Setters
}

Part.java:

@Entity
@Table(name="parts", uniqueConstraints = {@UniqueConstraint(columnNames = {"id"})})
public class Part implements Serializable {

protected final Logger log = Logger.getLogger(getClass());

private String id;
// *** other variables ***

public Part() {}

public Part(String id, /* other variables */) {
    this.id = id;
    // *** other variables ***
}

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public String getId() {
    return this.id;
}
public void setId(String id) {
    this.id = id;
}

private Set parentBomDefinitions = new HashSet(0);
private Set childBomDefinitions = new HashSet(0);

@OneToMany(mappedBy = "parent",
            targetEntity = BomDefinition.class,
            cascade = {CascadeType.PERSIST, CascadeType.MERGE},
            fetch = FetchType.EAGER)
public Set getParentBomDefinitions() {
    return parentBomDefinitions;
}
public void setParentBomDefinitions(Set<BomDefinition> parentBomDefinitions) {
    this.parentBomDefinitions = parentBomDefinitions;
}

@OneToMany(mappedBy = "child",       // target?
            targetEntity = BomDefinition.class,
            cascade = {CascadeType.PERSIST, CascadeType.MERGE},
            fetch = FetchType.EAGER)
public Set getChildBomDefinitions() {
    return childBomDefinitions;
}
public void setChildBomDefinitions(Set childBomDefinitions) {
    this.childBomDefinitions = childBomDefinitions;
}

}

0 个答案:

没有答案