removing item from collection

View: New views
3 Messages — Rating Filter:   Alert me  

removing item from collection

by polvo :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hey, when I remove a design from a project, the relationship is deleted from the database but not from the persistence context - it still appears as part of the project unless I restart the server. What am I doing wrong?


project.getDesigns().remove(persistentDesign);
                design.setProject(null);
                project = getDao().update(project);
                persistentDesignManager.updateDesign((PhotovoltaicGridTieSystemDesign) design);

thx in advance.

Re: removing item from collection

by Gordon Yorke-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Are you sure you are removing the correct instance from the list of
designs?  You example code uses "persistentDesign" and "design".  Are
you sure you are merging the correct object into the Persistence Context?

--Gordon

polvo wrote:

> Hey, when I remove a design from a project, the relationship is deleted from
> the database but not from the persistence context - it still appears as part
> of the project unless I restart the server. What am I doing wrong?
>
>
> project.getDesigns().remove(persistentDesign);
> design.setProject(null);
> project = getDao().update(project);
> persistentDesignManager.updateDesign((PhotovoltaicGridTieSystemDesign)
> design);
>
> thx in advance.
>  

Re: removing item from collection

by polvo :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

yes. Here's more details:


here's the set-up:
* I'm using JPA, Spring's JPA Support Daos, the LocalContainerEntityManagerFactoryBean, transactions are configured via annotations.

* All daos are wired with with entitymanager in spring app context file.

* A Project can have any number of designs. In the Project class the designs property is annotated with Cascade all.

Here's the Controller with output explained in comments. The problem is explained after the code.


//project has 5 designs, we are deleting one.

project = projectManager.removeDesign(Long.parseLong(designId), project.getId());
design = null;


// output is "showForm() - updated project returned by entityManager has 4 designs" - THIS IS CORRECT
log.debug("showForm() - updated project returned by entityManager has " + project.getDesigns().size() + " designs.");
                               
Project projectQueried = projectManager.getProjectWithId(project.getObjectId());
int size = projectQueried.getDesigns().size();


// output is "showForm() - projectQueried has 5 designs"  - THIS IS NOT CORRECT. Should be 4.
log.debug("showForm() - projectQueried has " + size + " designs.");


Problem: Why is projectQueried  showing 5 designs and not 4???

If I look in MySQL i can see that the design was indeed deleted from the database.


projectManagerImp:


        @Transactional
        public Project removeDesign(long designId, long projectId) {
               
               
                log.debug("removeDesign() - removing design with id '" + designId + "'");
                //List<PersistentDesign> projectsNewDesigns = new ArrayList<PersistentDesign>();
               
                Project project = dao.findWithId(projectId);
                Iterator<PersistentDesign> it = project.getDesigns().iterator();
                log.debug("removeDesign() - project has " + (project.getDesigns()!= null ? project.getDesigns().size() : '0') + " designs");
                while (it.hasNext()) {
                        PersistentDesign persistentDesign = it.next();
                        long persistentDesignId = persistentDesign.getObjectId();
                        if (persistentDesignId == designId) {
                                it.remove();
                                persistentDesign.setProject(null);
                                persistentDesignManager.deleteDesign((PersistentPhotovoltaicGridTieSystemDesign)persistentDesign);
                                break;
                        }
                }
                project.setLastUpdatedDate(new Date());
                return dao.update(project);
        }

daos:

deleting design:

        public void delete(T object) {
                object = getJpaTemplate().merge(object);
                getJpaTemplate().remove(object);
        }

updating project:




        @Override
        public Project update(Project project) {
                return super.save(project);
               
        }


        super#save


        public T save(T object) {
                return getJpaTemplate().merge(object);
        }


Gordon Yorke-2 wrote:
Are you sure you are removing the correct instance from the list of
designs?  You example code uses "persistentDesign" and "design".  Are
you sure you are merging the correct object into the Persistence Context?

--Gordon

polvo wrote:
> Hey, when I remove a design from a project, the relationship is deleted from
> the database but not from the persistence context - it still appears as part
> of the project unless I restart the server. What am I doing wrong?
>
>
> project.getDesigns().remove(persistentDesign);
> design.setProject(null);
> project = getDao().update(project);
> persistentDesignManager.updateDesign((PhotovoltaicGridTieSystemDesign)
> design);
>
> thx in advance.
>