-->
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.  [ 4 posts ] 
Author Message
 Post subject: manyTomany howTo update relationshipValues from inverse side
PostPosted: Mon Jun 18, 2007 11:31 am 
Newbie

Joined: Fri May 25, 2007 4:42 am
Posts: 13
need help

have a simple bidirect. manyTomany - hiberAnnotation,
project *-* employee

the project is the owner-side of this manyTomany-relation,
employee is the inverse-side. From Project-Side is public void setEmployees(Collection<Employee> employees) ok. will update ids to association table project_employee .

But from employee-side setProjects(Collection<Project> projects) {....} can not update values to association-Table

Code:
in Project.java  defined:
@ManyToMany(
            targetEntity=Employee.class,
            cascade={CascadeType.PERSIST, CascadeType.MERGE}
     )
//     @JoinTable(
//            name="project_employee_rel",
//            joinColumns={@JoinColumn(name="PROJECT", nullable = false)},
//            inverseJoinColumns = {@JoinColumn( name="EMPLOYEE")}
//     )
     private Collection<Employee> employees = new HashSet<Employee>(0);// * - *


in Employee.java :
@ManyToMany(
          cascade={CascadeType.PERSIST, CascadeType.MERGE},
           mappedBy="employees",
           targetEntity=Project.class
    )
    private Collection<Project> projects = new HashSet<Project>(0);



in hibernate annotaion tutorials
http://www.hibernate.org/hib_docs/annotations/reference/en/html_single/
found:
A many-to-many association is defined logically using the @ManyToMany annotation. You also have to describe the association table and the join conditions using the @JoinTable annotation. If the association is bidirectional, one side has to be the owner and one side has to be the inverse end (ie. it will be ignored when updating the relationship values in the association table)

there are some ways to update the relationship-values from the employee-side(inverse)? except to rebuild all project-Object from employee and project.setEmployees(employees+newEmployee=this). it will be very ugly.

thanx for tipps.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 19, 2007 10:56 am 
Newbie

Joined: Fri May 25, 2007 4:42 am
Posts: 13
anybody know how to solve the problem only with hibernate annotation?

otherwise i just wrote the long code in setter, that is not ideal, any other better ways?

in Employee.java :
/
Code:
**
     * setter method for attribute associatedProjects. Column name is 'PROJECT' in JoinTable "project_employee_rel".
     *
     * @param associatedProjects Associated Projects
     */
    public void setAssociatedProjects(Collection<Project> associatedProjects) {
       //in fact only need this line
        this.associatedProjects = associatedProjects;
       
      // the following code is to update relationships of this employee through project-bean
      // 1. delete  old relationships   2. add new relationships 
        Object[] assProjectsArray = associatedProjects.toArray();
        // 1. delete all old relationships with projects of this Employees
        ProjectDAO dao = new ProjectDAO();
         
      List projectList = dao.findAllProjects();
      for (Iterator it = projectList.iterator(); it.hasNext();) {
         Project pro = (Project) it.next();
         Collection<Employee> involvedEmployeesNew = new HashSet<Employee>(0);
         //pro.getInvolvedEmployees() to get old involvedEmployees
         Object[] em = pro.getInvolvedEmployees().toArray();
         for (int i=0; i< em.length; i++)
         {   
            if (!em.toString().equals(this.getNickname()))
            {
               involvedEmployeesNew.add((Employee) em[i]);
            }
         }
         pro.setInvolvedEmployees(involvedEmployeesNew);
      }   
      
      // 2. add all "new" relationships of this Employees through Project.involvedEmployees.add(thisEmployee)
      for(int i=0; i<assProjectsArray.length; i++)
        {
           Project pr = (Project) assProjectsArray[i];
           Collection<Employee> involvedEmployees = pr.getInvolvedEmployees();
           involvedEmployees.add(this);
           pr.setInvolvedEmployees(involvedEmployees);
        }
    }


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 19, 2007 12:42 pm 
Newbie

Joined: Fri May 25, 2007 4:42 am
Posts: 13
sorry, the methode must be changed to more efficient, because, if nothing changed by Edit Employee, will also do this methode(firstly, delete, then update...)


Top
 Profile  
 
 Post subject: has anybody idea?
PostPosted: Mon Jul 02, 2007 10:53 am 
Newbie

Joined: Fri May 25, 2007 4:42 am
Posts: 13
has anybody idea?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.