JPA2.0& Hibernate:使用父实体合并删除子表行(在从父子实体集中删除条目时) [英] JPA2.0 & Hibernate: Removing Child table rows with Parent entity merge (On removing entries from parent's child entity set)
问题描述
cascade = CascadeType.ALL,fetch = FetchType.LAZY,
例如,我在数据库表中有以下条目:
用户表
栏:UserID |用户名
row1:User1 |用户名
地址表
列:AddressId |用户ID |地址
row1:Address1 | User1 |实际地址
row2:地址2 | User1 |实际地址
我可以使用userId的find来获取User实体:
User user = entityManager.find(User.class,userId);
还可以使用user.getAddresses()获取答案。 getAddressess()的代码如下所示:
public class User {
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY,mappedBy =user)
public Set< Address> getAddresses(){
return this.addresses;
$ / code $ / pre
现在我正在尝试做什么,一组地址并删除一个条目。假设我在第一次获取地址1和2时获取了地址集。我从地址集中删除了地址2实体。现在,用户实体指向的Set仅有1个用于Address1的条目。
现在,如果我使用下面的方法合并用户实体,我认为,row for address 2应从地址表中删除。但它没有发生。我是否缺少任何东西,或者它不是一种方法。请建议。
entityManager.merge(user);
解决方案只是想添加更多的细节, 。
我用 @OneToMany 关系添加了 orphanRemoval = true :
public class User {
@OneToMany(cascade = CascadeType.ALL,fetch = FetchType.LAZY,orphanRemoval = true,mappedBy =user)
public Set< Address> getAddresses(){
return this.addresses;
$ / code>
更改我的代码后,我重新测试它并得到错误:
引起:org.hibernate.HibernateException:cascade =all-delete-orphan的集合是否为更长的被拥有实体实例引用:
此链接帮助我解决了上述问题:
现在我可以从数据库中删除子实体,如果这些子实体与父实体中已定义的集合断开连接(已删除)。 / p>
I am using JPA2.O with Hibernate. I have one to many relationship between User and Address and I have below configuration with this relationship:
cascade = CascadeType.ALL, fetch = FetchType.LAZY,
For example I have below entries in database tables:
User Table
Column: UserID | UserName
row1: User1 | UserName
Address Table
Column: AddressId| UserID | Address
row1: Address1 | User1 | Actual Address
row2: Address2 | User1 | Actual Address
I can fetch User entity using find by userId:
User user = entityManager.find(User.class, userId);
And also can fetch Answers using user.getAddresses(). Code for getAddressess() is like below:
public class User{
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "user")
public Set<Address> getAddresses() {
return this.addresses;
}
}
Now what I am trying to do, I am fetching the set of addresses and removing one entry. Let say I fetched addressess set first time I have address 1 and 2. I removed address 2 entity from set of addresses. Now User entity is pointing to the Set which is having only 1 entry that is for Address1.
Now If I merge user entity using below method, I think, row for address 2 should be deleted from address table. But it is not happening. Am I missing any thing or it is not a way to do it. Please suggest.
entityManager.merge(user);
解决方案 Just want to add more detail as I got this working.
I added orphanRemoval = true with @OneToMany relationship:
public class User{
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true, mappedBy = "user")
public Set<Address> getAddresses() {
return this.addresses;
}
}
After changing my code I re-tested it and got below error:
Caused by: org.hibernate.HibernateException: A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance:
This link helped me to solve above problem:
Now I can delete the child entities from database, if those are disconnected(removed) from defined collection in parent entity.
这篇关于JPA2.0& Hibernate:使用父实体合并删除子表行(在从父子实体集中删除条目时)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!