Hello everyone,
I've class Project which has many-to-many relation with class Resource; Hence i introduced an association class ProjectResource which has the composite id and other attributes.
I've a projectDAO class which provides, among other methods, insertProject. resourceDAO has, among other methods, insertResource. I need a way to added project, resource and related projectresource all in one txn. This way, if one insert fails, everything else should fail.
In my business interface iRMTFacade, i added a method called 'addProjectAndResource' as follows:
Implementation for this is as follows:Code:void addProjectAndResource(Project project, Resource resource);
As the ProjectResource class has a composite id (please see the code for constructor of this class below) of projectId and resourceId, i need to first insert project and resource entries.Code:public void addProjectAndResource(Project project, Resource resource) { this.projectDao.insertProject(project); this.resourceDao.insertResource(resource); }
This ProjectResource class has a composite id whic has two properties projectId, resourceId and the getter/setter methods for it.Code:public ProjectResource(ProjectResource prjres) { // ProjectResource(prjres.getUsername(), prjres.getStartDate(), prjres.getAssignmentDate(), prjres.getProject(), // prjres.getResource()); this.username = prjres.getUsername(); this.startDate = prjres.getStartDate(); this.assignmentDate = prjres.getAssignmentDate(); this.project = prjres.getProject(); this.resource = prjres.getResource(); this.id.projectId = this.project.getId(); this.id.resourceId = this.resource.getId(); project.getProjectResources().add(this); resource.getProjectResources().add(this); }
Now the PROBLEM is that, first i need to insert Project, then Resource into the datastore. Then, logically speaking, if i JUST create a new ProjectResource object, Hibernate should automatically create an entry for this in the ProjectResource TAble.
I wrote a JUnit test code as follows:
Code:public void testInsertProjectAndResource() { Collection projects = rmt.findProjects("eApproval"); int found = projects.size(); assertTrue(found == 0); Collection resources = rmt.findResources("Raghu"); int res_found = resources.size(); assertTrue(found == 0); rmt.addProjectAndResource(project2, resource2); ProjectResource prjres = new ProjectResource("Raghu", new Date(), new Date(), project2, resource2); rmt.insertProjectResource( prjres); assertTrue(!project2.isNew()); projects = rmt.findProject(project2); assertEquals(found + 1, projects.size()); resources = rmt.findResource(resource2); assertEquals(res_found + 1, resources.size()); }
BUT I DON'T SEE ANY ENTRY IN THE ProjectResource table when run the above junit test. Why? Could someone let me know what's that i'm missing.
Also, as per Spring IoC, i'm not supposed to created any object in my code and the autowiring should happen through spring config file. Does it mean that i need to add the configuration to create new ProjectResource object in the Spring config file itself? If yes, how do i do that?
I greatly appreciate any input in this.
Thanks!


Reply With Quote