Hibernate 多对一关系删除未按预期工作
我有如下 3 个表:
- 用户有 userId
- UserProductEntitlement 有 userId 和 ProductId ,布尔
- 产品有 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=?
我这里有两个问题:
- 为什么它会触发更新而不是删除?
- 这是更大的问题——userId=语句在哪里?而不是 userId=?和产品 ID=?
对此的任何帮助/建议将不胜感激。
I have 3 tables as following:
- User has userId
- UserProductEntitlement has userId and productId and a boolean
- 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:
- why it is firing update instead of delete?
- This is the bigger problem -- where statement is userId=? instead of userId=? and productId=?
Any help/suggestion on this would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您忘记在 viewableProduct 上添加 @Cascade 注释。
请参阅此处详细信息。
Looks like your forgot to add @Cascade annotation at viewableProduct.
See details here.