开发者

Hibernate many-to-one relationship delete not working as expected

I have 3 tables as following:

  1. User has userId
  2. UserProductEntitlement has userId and productId and a boolean
  3. Product has productId

This is my class representation :

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

    }   

}

So initially when a user logs in all his entitlements are loaded. But when I try to remove a ProductEntitlement from view开发者_JAVA百科ableProduct set, hibernate is firing a update statement as:

update UserProductEntitlement set userId= null where userId=?

I have two issues here:

  1. why it is firing update instead of delete?
  2. This is the bigger problem -- where statement is userId=? instead of userId=? and productId=?

Any help/suggestion on this would be appreciated.


Looks like your forgot to add @Cascade annotation at viewableProduct.

See details here.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜