Java 嵌入ID的中间表导致堆栈溢出

Java 嵌入ID的中间表导致堆栈溢出,java,hibernate,Java,Hibernate,我在使用EmbeddedId进行hibernate映射时遇到问题。我的代码如下所示: 库存: public class Inventory { private Long id; @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "inventory_id") public Long getId() { return id; } public void setId(Long id) { thi

我在使用EmbeddedId进行hibernate映射时遇到问题。我的代码如下所示:

库存:

public class Inventory {

private Long id;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "inventory_id")
public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

private Set<InventoryUser> inventoryUser = new HashSet<InventoryUser>();

@OneToMany(cascade = CascadeType.PERSIST, fetch = FetchType.EAGER, mappedBy = "pk.inventory")
public Set<InventoryUser> getInventoryUser() {
    return inventoryUser;
}

public void setInventoryUser(Set<InventoryUser> productUser) {
    this.inventoryUser = productUser;
}
}
现在,持久化数据可以很好地工作,但当我搜索库存时,总是会得到StackOverflow,即使数据库中只有一个条目

你知道这是怎么回事吗

问候
克里斯

在没有额外细节的情况下尝试。。。看起来您的Inventory对象正在急切地获取InventoryUser,它包含ProductUser的主键,ProductUser包含Inventory。这是循环的,可能会导致溢出。

我认为这是因为InventoryUser和ProductUserPK之间的循环引用


InventoryUser.pk或ProductUserPK.user都应该是懒惰的。

Hmmm,它似乎与条件搜索有关。我尝试了一个简单的hql语句,我成功了。。。
谢谢你的回复,我走对了路;)

您可能希望添加查询(我在上面的代码中没有看到)和堆栈中的非冗余细节。。。上面代码中的任何内容都没有向我跳出来。循环引用是正常的@hibernate,这不是问题所在。但无论如何,谢谢。您确定不能从库存-->用户-->库存用户-->产品用户获取相同的循环引用吗?在我看来,根据莫里斯的上述说法,你也可以解决这个问题。
@Entity
@Table(name = "User_Table")
public class User implements Comparable{
private String id;

@Id
@Column(name = "user_id")
public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

private Set<InventoryUser> inventoryUser = new LinkedHashSet<InventoryUser>();

@OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.user")
public Set<InventoryUser> getInventoryUser() {
    return inventoryUser;
}

public void setInventoryUser(Set<InventoryUser> inventoryUser) {
    this.inventoryUser = inventoryUser;
}
@Entity
@Table(name = "Inventory_User")
public class InventoryUser {    
private ProductUserPK pk = new ProductUserPK();

@EmbeddedId
@NotNull
public ProductUserPK getPk() {
    return pk;
}

public void setPk(ProductUserPK pk) {
    this.pk = pk;
}

@Embeddable
public static class ProductUserPK implements Serializable {
    public ProductUserPK(){

    }

    private User user;

    @ManyToOne
    @JoinColumn(name = "user_id", insertable = false, nullable = false)
    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
        // this.user.getInventoryUser().add(InventoryUser.this);

    }

    private Inventory inventory;

    @ManyToOne
    @JoinColumn(name = "inventory_id", insertable = false, nullable = false)
    public Inventory getInventory() {
        return inventory;
    }

    public void setInventory(Inventory inventory) {
        this.inventory = inventory;

    }