dmpolvo
Sep 24th, 2009, 12:54 PM
Hello,
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(designI d), 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.getObjectI d());
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((PersistentPh otovoltaicGridTieSystemDesign)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);
}
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(designI d), 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.getObjectI d());
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((PersistentPh otovoltaicGridTieSystemDesign)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);
}