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