开发者

intermediate Table with EmbeddedId causes StackOverflow

I have a problem with my hibernate mapping using EmbeddedId. My code looks like these:

Inventory:

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

User:

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

InventoryUser

@Entity
@Table(name = "Inventory_User")
public class InventoryUser {    
p开发者_StackOverflow社区rivate 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;

    }

Now persisting data works fine, but when I search for Inventories I always get a StackOverflow, even if there is only one entry in DB.

Any ideas what this is about?

Greets Chris


I think it's because of the circular reference between InventoryUser and ProductUserPK.

Either InventoryUser.pk or ProductUserPK.user should be lazy.


Taking a stab without the extra details... it looks like your Inventory object is eagerly fetching an InventoryUser, which contains a primary key of ProductUser, which contains Inventory. This is circular and is probably causing your overflow.


Hmmm, seem that it has something to do with the criteria-search. I tried a simple hql statment and i works... Thanks for the reply, i put me on the right way ;)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜