Hi Jasha, any luck getting this to work? The code does seem fine to me. In
the past we've used code similar to that for handling entity deletes:
public static void managedDelete(JpaTemplate jpaTemplate, Object obj) {
if (jpaTemplate == null)
throw new IllegalArgumentException("jpaTemplate can't be null");
if (obj == null)
throw new IllegalArgumentException("object can't be null");
jpaTemplate.remove(jpaTemplate.contains(obj) ? obj :
jpaTemplate.merge(obj));
}
Where JpaTemplate is a org.springframework.orm.jpa.JpaTemplate object. For
those who were involved in the original setup of the persistence layer, is
there any reason why we aren't using the
org.springframework.orm.jpa.JpaTemplate objects to help in our repositories?
Tony
---
Anthony Carlucci | SW App Dev Eng, Sr. | R501 / KW App Development & Maint
e: [email protected] | v: 781.271.2432 | f: 781.271.3299
The MITRE Corporation | 202 Burlington Rd | Bedford, MA 01730-1420
-----Original Message-----
From: Jasha Joachimsthal [mailto:[email protected]]
Sent: Tuesday, October 18, 2011 12:07 PM
To: [email protected]
Subject: Issues when deleting entities
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