How do you get a OneToOne item to automatically remove with JPA/Hibernate? I would expect simply setting the OneToOne item to be null in the class that contains would be smart enough to allow Hibernate to delete it.
Given a simple object, simplified:
@Entity
public class Container {
private Item item;
@OneToOne(cascade=CascadeType.ALL)
public Item getItem() { return item; }
public void setItem(Item newItem) { item = newItem; }
}
When an Item is set on Container an Container is persisted with merge a row gets inserted.
Container container = new Container();
container.setItem(new Item());
container = entityManager.merge(container);
// Row count is 1
But when the item is set null, or to another item, the old object still exists in the table.
container.setItem(null);
container = entityManager.merge(container);
// Row count is STILL 1, leaving orphaned rows.
So, how do I remove these OneToOne orphans?