For the admin interface I'm implementing a delete user feature. It should
delete the User but also the entities that belongs to the User (Page,
RegionWidget etc).
I added a new method to UserService and its implementation
DefaultUserService:
public void deleteUser(Long userId) {
User user = userRepository.get(userId);
if (user == null) {
return;
}
userRepository.delete(user);
}
When this method is called to delete john,doe an exception is thrown:
<openjpa-2.1.1-r422266:1148538 nonfatal user error>
org.apache.openjpa.persistence.ArgumentException: You cannot perform
operation delete on detached object "org.apache.rave.portal.model.User-2".
This operation only applies to managed objects.
A fix/workaround is to change AbstractJpaRepository#delete:
public void delete(T item) {
if (item == null || item.getEntityId() == null) {
return;
}
T entity = manager.find(type, item.getEntityId());
if (entity != null) {
manager.remove(entity);
}
}
Now deleting a User works. Of course not only the User should be deleted but
also its Page (we're not Facebook) and then other entities that link to the
former User. So DefaultUserService#deleteUser is extended to:
public void deleteUser(Long userId) {
User user = userRepository.get(userId);
if (user == null) {
return;
}
for (Page page : pageRepository.getAllPages(userId)) {
pageRepository.delete(page);
}
userRepository.delete(user);
}
To my surprise I get a similar exception now:
<openjpa-2.1.1-r422266:1148538 nonfatal user error>
org.apache.openjpa.persistence.ArgumentException: You cannot perform
operation delete on detached object "org.apache.rave.portal.model.Page-3".
This operation only applies to managed objects.
Both JpaUserRepository and JpaPageRepository extend AbstractJpaRepository
and neither overrides delete. Is there someone with more OpenJPA/JPA
experioence who can solve this puzzle for me?
Jasha Joachimsthal
Europe - Amsterdam - Oosteinde 11, 1017 WT Amsterdam - +31(0)20 522 4466
US - Boston - 1 Broadway, Cambridge, MA 02142 - +1 877 414 4776 (toll free)
www.onehippo.com