Results 1 to 2 of 2
  1. #1
    Join Date
    Dec 2007
    Posts
    1
    Rep Power
    0

    Default How to update data for a JPA many-to-many relationship?

    Hi,
    I've 2 entities namely User and Project. Each user may be involved in multiple projects and one project obviously has many users. What is the correct way to update data for an user/a project? While updating user info, I need to update project info and vice-versa.

    Java Code:
    public void updateUserInfo(User user) throws NoSuchUserException,
                NoSuchProjectException {
            if (user == null || user.getId() == null)
                throw new IllegalArgumentException();
            
            findUserByID(user.getId());        
            
            Set<Project> projects = user.getProjects();
            Set<User> members = null;
            
            if (projects != null) {            
                            
                for (Project p : projects) {
                    members = p.getMembers();
                    
                    if (members == null) {
                        members = new HashSet<User>() ;
                    }
                    members.add(user);
                    p.setMembers(members);
                    
                    projectServiceLocal.updateProjectInfo(p);
                }
            }
            
            em.merge(user);
        }
    Java Code:
    public void updateProjectInfo(Project proj) throws NoSuchProjectException {
            if (proj == null)
                throw new IllegalArgumentException();
            
            findProjectByID(proj.getId());
            Set<User> members = proj.getMembers();
            Set<Project> projects = null;
            
            if (members != null) {
               
                for (User user : members) {
                    projects = user.getProjects();
                    
                    if (projects == null) {
                        projects = new HashSet<Project>() ;
                    }
                    projects.add(proj);
                    user.setProjects(projects);
                }
            }
            
            em.merge(proj);
        }
    Regards,
    Abhijit

  2. #2
    kelokebe is offline Member
    Join Date
    Oct 2008
    Posts
    1
    Rep Power
    0

    Default New Memember

    Java Code:
    Hello to Everyone!:o

Similar Threads

  1. JPanel won't update
    By ibanez270dx in forum New To Java
    Replies: 3
    Last Post: 01-06-2009, 08:59 PM
  2. Using sql:update tag
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-13-2008, 11:49 PM
  3. What is this Object relationship?
    By SirRawlins in forum New To Java
    Replies: 3
    Last Post: 12-14-2007, 03:17 PM
  4. problem with ejb 3.0 entity beans with manyToMany relationship
    By makcro in forum Enterprise JavaBeans (EJB)
    Replies: 0
    Last Post: 07-26-2007, 07:37 PM
  5. dynamic update in swt
    By sandor in forum SWT / JFace
    Replies: 0
    Last Post: 05-14-2007, 08:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •