2010-10-22 45 views
0

我有3個表如下:Hibernate的許多一對一的關係刪除工作不正常

  1. 用戶具有用戶id
  2. UserProductEntitlement有帳戶及productId參數和一個布爾
  3. 產品先後的productId

這是我的課表示:

@Table(name="User") 
class User 
{ 
    @OneToMany(fetch=FetchType.EAGER, targetEntity = ProductEntitlement.class, cascade=CascadeType.ALL)  
    @JoinColumn(name = "userId")  
    Set<ProductEntitlement> viewableProducts = new HashSet<ProductEntitlement>(); 
} 

@Table(name = "UserProductEntitlement") 
class ProductEntitlement 
{ 
    @EmbeddedId 
    private ProductEntitlementPk productPk; 

    @Column(name = "X_ENABLED") 
    @Type(type = "yes_no") 
    private Boolean xEnabled; 

     @Embeddable 
    private static class ProductEntitlementPk implements Serializable {   

     ProductEntitlementPk() { 
     } 

     ProductEntitlementPk(User user, Product baseProduct) { 
      this.role = role; 
      this.baseProduct = baseProduct; 
     } 

     @ManyToOne(optional = false, targetEntity = User.class) 
     @ForeignKey(name = "FK_USER") 
     @JoinColumn(name = "userId", nullable = false) 
     private User user; 

     @OneToOne(optional = false, targetEntity = BaseProduct.class) 
     @ForeignKey(name = "FK_Product") 
     @JoinColumn(name = "productId", nullable = false) 
     private Product baseProduct; 

    } 

} 

所以最初當用戶登錄時,他的所有權利都被加載。但是,當我嘗試刪除從viewableProduct集ProductEntitlement,Hibernate是燒成更新語句爲:

update UserProductEntitlement set userId= null where userId=? 

我這裏有兩個問題:

  1. 爲什麼它被髮射更新,而不是刪除?
  2. 這是更大的問題 - where語句是用戶id =?而不是userId =?和productId =?

任何幫助/建議將不勝感激。

回答

0

看起來你忘了在viewableProduct添加@Cascade註解。

查看詳情here

+0

是的,我確實添加了它..Sorry,忘了將它添加在貼出的問題。這也沒有幫助。 – user483788 2010-10-24 22:32:51