2010-01-18 31 views
0

我有一個使用EmbeddedId我的hibernate映射的問題。我的代碼看起來像這樣:中間表與EmbeddedId導致StackOverflow

庫存:

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

用戶:

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

    } 

現在保存數據工作正常,但是當我搜索的存貨,我總是得到一個StackOverflow,即使DB中只有一個條目。

任何想法是什麼?

電賀 克里斯

+0

你可能想添加您的查詢(我沒有看到它在你的代碼上面)和堆棧中的非冗餘細節......上面代碼中的任何內容都不會跳出來。 – PaulP1975 2010-01-18 12:47:43

回答

0

我認爲這是因爲InventoryUser和ProductUserPK之間的循環引用。

InventoryUser.pk或ProductUserPK.user應該是懶惰的。

0

隔空刺沒有額外的細節......它看起來像你的庫存對象急切地取一個InventoryUser,其中包含ProductUser的主鍵,其中包含庫存。這是循環的,可能會導致你的溢出。

+0

循環引用是正常的@Hibernate,這不是問題。但無論如何感謝 – woezelmann 2010-01-18 13:09:54

+0

你確定你不能從庫存 - >用戶 - > InventoryUser - > ProductUser獲取相同的循環引用嗎?根據Maurice上面的說法,在我看來,你也解決了這個問題。 – PaulP1975 2010-01-18 16:33:34

0

嗯,似乎它與標準搜索有關。我嘗試了一個簡單的hql陳述,我工作... 感謝您的答覆,我把我放在正確的方式;)

相關問題