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.  [ 1 post ] 
Author Message
 Post subject: Problem, add new records in a many-to-many relation
PostPosted: Sun Sep 02, 2012 6:22 am 
Newbie

Joined: Sat Sep 01, 2012 9:14 am
Posts: 1
Hi,
I have two entities 'Person' and 'Team' which are mapped by a bidirectional many-to-many relation using annotations, as follow:

Code:
@Entity
public class Perosn implements Serializable {
      ....
      private Collection<Team> teams;
      ....

     @ManyToMany(cascade = {javax.persistence.CascadeType.PERSIST, javax.persistence.CascadeType.MERGE})
     @JoinTable(name = "person_team",
            joinColumns = { @JoinColumn(name = "person_id")},
            inverseJoinColumns = {@JoinColumn(name = "team_id")})
     public Collection<Team> getTeams() {
        return advisers;
     }
}

,
Code:
@Entity
public class Team implements Serializable {
      ....
      private Collection<Person> persons;
      ....

     @ManyToMany(mappedBy = "teams",
            cascade = {javax.persistence.CascadeType.PERSIST, javax.persistence.CascadeType.MERGE})
     public Collection<Person> getPersons() {
        return persons;
     }
}


my service class{1, 2: According to 'Christian Bauer and Gavin King, Java Persistence with Hibernate by Manning Publication Co. Page 301, P 2'}:

Code:
@Service("teamService")
public class TeamService extends DataAccess {
    ....
    public void addTeamToPerson(String teamId, String personId) {

        Team team= getTeam(teamId);
        Person person= personService.getPerson(personId);
        //1.
        person.getTeams().add(team);
        //2.
        team.getPersons().add(person);
        super.update(team);
        super.update(person);
    }
    ....
}


Code:
public abstract class DataAccess<E> {

    private EntityManager manager;
    ....
    public void update(E object) {
        try {
            manager.getTransaction().begin();
            manager.merge(object);
            manager.getTransaction().commit();
        } catch (Exception e) {
            ....
        }
   }
   ....
}


And my problem: Calling method 'addTeamToPerson()' does not cause to add a new record to join table 'person_team'!!!:(
Note: I traced the code line by line, there's no error or exception and the transaction commits.
Please help me solve it.

_________________
Regards.
--------------
Babak Behzadi


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

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.