Hibernate 多对一关系删除未按预期工作

发布于 2024-09-28 10:18:49 字数 1692 浏览 5 评论 0原文

我有如下 3 个表:

  1. 用户有 userId
  2. UserProductEntitlement 有 userId 和 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. 这是更大的问题——userId=语句在哪里?而不是 userId=?和产品 ID=?

对此的任何帮助/建议将不胜感激。

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 viewableProduct 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧城空念 2024-10-05 10:18:49

看起来您忘记在 viewableProduct 上添加 @Cascade 注释。

请参阅此处详细信息。

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

See details here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文