On 19 October 2011 16:09, Franklin, Matthew B. <[email protected]> wrote:

> >-----Original Message-----
> >From: Jasha Joachimsthal [mailto:[email protected]]
> >Sent: Wednesday, October 19, 2011 9:32 AM
> >To: [email protected]
> >Subject: Re: Issues when deleting entities
> >
> >Hi Antonio,
> >
> >On 19 October 2011 15:24, Carlucci, Tony <[email protected]> wrote:
> >
> >> 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:
> >>
> >
> >I didn't get nor go any further but I guess it may have something to do
> with
> >"Page contains Region's which contain RegionWidget's which contain
> >RegionWidgetPreference's".
>
> Deletes should cascade, so long as the cascade properties in the JPA
> annotations are correct.
>

As far as I can see the whole chain has cascade all and delete orphans to
true.


>
> >
> >
> >
> >> 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?
> >>
> >>
> >I don't know why we're not using this, but I'm not the person who set this
> >up.
>
> JpaTemplates, while still in existence are not the preferred model for
> Spring > 3.0.   I do think your workaround is appropriate though.  I would
> be +1 for getting the entity from the entitymanager before deleting.
>

I tried getting the entity in the same transaction as deleting it. That
worked for User but not for Page. My priorities shifted from the user admin
screen to the widget admin screen, so for now I won't be working on it, but
we will get the issue in the future. Maybe someone else has time for it?

Jasha


>
> >
> >Jasha
> >
> >
> >> 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
> >>
>

Reply via email to