Announcement Announcement Module
No announcement yet.
Many-to-many association, Spring and Hibernate Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Many-to-many association, Spring and Hibernate

    I've two classes Project and Resource which has many-to-many association. I've introduced an association class ProjectResource and made this many-to-many into 2 one-to-many associations.

    In Spring, each method provided in DAO is inside a transaction. ProjectDao class has an insertProject and when i want to insert object Project, i'll call this method. In my JUnit tests, after i insert project, i also want to insert Resource object. Resource Class has a composite id (projectId and resourceId and other attributes as in any association class). Class My question is, DO I NEED TO HAVE A DAO (ProjectResourceDao) for the association class ProjectResource?

    If i had to implement this in Hibernate without Spring, as i control the transaction, this is what i'll do.

    Begin Transaction
    Create Project object
    Insert Project object
    Create Resource Object
    Insert Resource object

    Create ProjectResourceObject
    // NO Insert ProjectResource as the constructor will automatically take care of it.
    End transaction

    As they all are running in the same txn, hibernate will detect the association (bidirectional navigation?? ) and insert an entry in the ProjectResource table).

    In spring, in my Facade interface, i added a method "insertProjectAndResource" which takes the argument project, resource and projectResource. (i added this as i want this all to happen in one transaction). In this, i do the following:

    1. insertProject
    2. insertResource
    3. create an object projectResource

    As projectResource object already has references to projectId and resourceId (composite Id), i would expect Hibernate to insert a corresponding entry for this. But i couldn't see this happening.

    Could some one tell me what's that i'm missing and what i'm doing is correct or NOT? If not correct, how else should i do it?


    Here's how my ProjectResource constructor looks:

    * @param username
    * @param startDate
    * @param endDate
    public ProjectResource( String username, Date startDate, Date assignmentDate, Project proj, Resource res) {
    this.username = username;
    this.startDate = startDate;
    // this.endDate = endDate;
    this.assignmentDate = assignmentDate;

    this.project = proj;
    this.resource = res;

    // Set key values = project.getId(); = resource.getId();

    // Guarantees referential integrity

    Class Project {
    Set projectResources;

    Class Resource {
    Set projectResources;

  • #2
    Could someone please provide answer for my question?