Reputation: 5537
@ElementCollection(fetch=FetchType.EAGER)
@CollectionTable(name="Something", joinColumns=@JoinColumn(name="profile_id"))
@Column(name="favorite")
private Set<String> something = new HashSet<String>();
I have this relationship in my Profile and it works. A table gets created. When a profile is deleted I wont the lines that belongs to that entity to also be removed from the Collection table. I don't want to use cascading. How can I make this work.
Upvotes: 1
Views: 2688
Reputation: 42114
That happens out of the box. When entity that does have ElementCollection is removed via EntityManager.remove no further actions are needed - persistence provider issues delete from the collection table as well.
Upvotes: 2
Reputation: 28746
You can put the code to delete the rows in a method annotated with @PostRemove in the Profile entity class.
@Entity
public class Profile {
@PostRemove
public void cleanupCollectionTable(){
Query query = em.createQuery("DELETE FROM Something s WHERE s.profile_id = :id");
int deletedCount = query.setParameter(id, profileId).executeUpdate();
}
...
}
Upvotes: 0