1

我有一个名为 Part 的带注释的实体。它由一个“id”和一些其他变量组成。我有另一个名为“bomdefinitions”的表。该表有三列(数量、父级、目标)(我知道目标是一个愚蠢的名称,但我无法更改它,因为数据库已经存在)。这个想法是一个 Part 可以由许多其他部分组成,也可以在许多其他部分中使用,并且表“bomdefinitions”说明了哪个父级与它相关联的“目标”以及它使用了多少。

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

我可以找到将实体映射到自身的示例,使用带有额外列的连接表映射实体的示例,以及进行注释的各种方法的示例,但在单个应用程序中没有做所有这些,以及我的各个示例can find 似乎在这里没有帮助。

任何在 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
}

部分.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;
}

}
4

0 回答 0