title: Hibernate: How to set mapped by attribute when the Many side of the Many to one relationship is maintained in an object in an embedded collection
body: <p>I have a 2 entities and an embedded object. To keep the discussion generic, I am going to call them Project, ProjectAssociation (embedded object), employee.</p>
  
  <p>Employees are associated to Project using a project association.</p>
  
  <p>My business requirement is such that I should be able to lookup projectAssociation from employee. Furthermore, in the future we can expect the projectAssociation to gain more attributes. The lifecycle of projectAssociation will always be tied to project.</p>
  
  <p>The Project and employee entities have a many to many relationship between them via the projectAssociation embedded object.</p>
  
  <p>How do I have the mapped by configured from project to project association.?</p>
  
  <pre><code>Employee {
      Project projects;
  
      @OneToMany(mappedBy="What to set here?")
      public Set&lt;Project&gt; getProjects() {
          return projects;
      }
  
      public void setProjects(Set&lt;Project&gt; projects) {
          this.projects= projects;
      }
  }
  
  Project {
      Set&lt;ProjectAssociation&gt; projectAssocs;
  
      @Embedded
      @ElementCollection
      public Set&lt;ProjectAssociation&gt; getProjectAssociation() {
          return projectAssocs;
      }
  
      public void setProjectAssociation(Set&lt;ProjectAssociation&gt; hostRecovery) {
          this.projectAssocs = projectAssocs;
      }
  }
  
  ProjectAssociation {
  // Parent association variable and methods are omitted for brevity
      Employee  employee;
  
      @ManyToOne
      public Employee getEmployee() {
          return employee;
      }
  
      public void setEmployee(Employee employee) {
          this.employee = employee;
      }
  }
  </code></pre>