Skip to content

Instantly share code, notes, and snippets.

@vargeorge
Created August 29, 2014 16:37
Show Gist options
  • Save vargeorge/2f7a727c64e9a7bb093e to your computer and use it in GitHub Desktop.
Save vargeorge/2f7a727c64e9a7bb093e to your computer and use it in GitHub Desktop.
How to delete a child records in a One To Many association using Play Framework / Ebean
This looks like bug / hack that's not very obvious. When you remove a childBean and save the parentBean using Ebean, it doesn't seem to delete the corresponding child record from the database.
For example:
partentBean.childBeans.remove(aChildBean)
Ebean.save(parentBean)
=> Expected to see aChildBean record deleted from DB, however the above doesn't delete it
Solution:
To make this work, Ebean seem to have a hack (not part of JPA) annotation that need to be specified to the association in the ParentBean as below
class ParentBean extnds Model {
@Id
public Long id;
@PrivateOwned
@OneToMany (cascade = CascadeType.ALL)
public List<ChildBean> childBeans = new ArrayList<ChildBean>();
}
The annotation @PrivateOwned (com.avaje.ebean.annotation.PrivateOwned) does the trick will force aChildBean record to be deleted in the above example.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment