Well, I have a Restaurant Entity with an Element Collection of Branches . Now my question is how do I remove a branch from a restaurant since an Embeddable object doesn't have an Id . Usually, what i would do If branch was an entity is
entityManager.remove(entityManager.getReference(Branch.class, branchId));
But since Branch is Embeddable object (without ID), I am not sure how to achieve it . Some code examples would be highly appreciated. Thanks in advance.
You need to identify the object you want to remove (obviously without id since there is none, but the combination of other fields should be unique), remove it from owner entity's collection and merge the entity.
Since there is no id, JPA will delete all elements from collection, and insert them again but without the removed one.
Branch toBoRemoved = ...; // find out which element needs to be removed
for (Iterator i = entity.getBranches().iterator(); i.hasNext();) {
Branch b = (Branch)i.next();
if (b.equals(toBeRemoved)) { // you'll need to implement this
i.remove();
}
}
entityManager.merge(entity);