-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Bidirectional ManyToMany: No insert on the join table.
PostPosted: Thu Jul 01, 2010 4:19 am 
Newbie

Joined: Tue Jun 01, 2010 10:18 am
Posts: 15
Hello!

I have a n:m relation with Persons and Projects and I have these domain objects.

Person:
Code:
   @ManyToMany(mappedBy="persons", fetch=FetchType.EAGER)
   @Fetch(FetchMode.SELECT)
   private List<Project> projects;

Projects:
Code:
   @ManyToMany(fetch=FetchType.EAGER)
      @JoinTable(name = "Project_Person",
         joinColumns = {
            @JoinColumn(name="project_id", referencedColumnName="id")
         },
         inverseJoinColumns = {
            @JoinColumn(name="person_id", referencedColumnName="id")
         }
      )
   @Fetch(FetchMode.SELECT)
   private List<Person> persons;


Writing a JUnit test method, I will try to add a Person with two projects, see here:
Code:
   @Test
   public void testAddPerson() {
      List<Person> persons = service.getAllPersons();

      Person person = new Person();
      person.setEmail("newMail@test.com");
      Project testProject = new Project();
      testProject.setId(1);
      Project testProject2 = new Project();
      testProject2.setId(2);
      ArrayList<Project> lister = new ArrayList<Project>();
      lister.add(testProject);
      lister.add(testProject2);
      person.setProjects(lister);
      service.addPerson(person);
...   }

My Dao:
Code:
   public Person addPerson(Person p) {
      SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
      Session sess = sessionFactory.getCurrentSession();

      Transaction tx = sess.beginTransaction();
      sess.save(p);

      sess.flush();
      tx.commit();

      return p;
   }


The problem is: A person can be added, that's fine, but there are not insert on the join table! So Persons are added, but noch linked to a project in the Project_Person table.
I tried with chascading anf those things, but I did not get a solution, so I hop you can help me.

Thanks a lot in advance & Best Regards.


Top
 Profile  
 
 Post subject: Re: Bidirectional ManyToMany: No insert on the join table.
PostPosted: Thu Sep 09, 2010 2:00 pm 
Newbie

Joined: Wed Mar 19, 2008 11:19 am
Posts: 15
Location: Orlando
I am experiencing the same issue. I'm guessing that when you make the association on the non-owning side, you need to make it on the owning side as well. Not sure if cascading would be necessary or not though.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.