title: Hibernate 5 Bytecode Enhancement Association Management works just in one direction
body: <p>I have 2 JPA Entities mapped like this :</p>
  <pre><code>@MappedSuperclass
  public class AbstractEntity {
  
      private static final String INCREMENT_STRATEGY = &quot;increment&quot;;
  
      private static final String INCREMENT_ID_GENERATOR_NAME = &quot;INCREMENT_ID_GENERATOR&quot;;
      
      @Id
      @GenericGenerator(name = INCREMENT_ID_GENERATOR_NAME, strategy = INCREMENT_STRATEGY)
      @GeneratedValue(generator = INCREMENT_ID_GENERATOR_NAME)
      private Long id;
  
      public AbstractEntity() {
          super();
      }
      
      public Long getId() {
          return id;
      }
  }
  
  @Entity
  public class Department extends AbstractEntity{
      
      
      @OneToMany(cascade = CascadeType.ALL, mappedBy = &quot;department&quot;)
      private List&lt;Employee&gt; employees = new ArrayList&lt;Employee&gt;();
  
      public void setEmployees(List&lt;Employee&gt; employees) {
          this.employees = employees;
      }
      
      public List&lt;Employee&gt; getEmployees() {
          return employees;
      }
  
  }
  
  @Entity
  public class Employee extends AbstractEntity {
  
      @ManyToOne(optional = true, cascade= CascadeType.ALL)
      @JoinColumn(name = &quot;DEPARTMENT_ID&quot;)
      private Department department;
  
      public void setDepartment(Department department) {
          this.department = department;
      }
  
      public Department getDepartment() {
          return department;
      }
  
  }
  </code></pre>
  <p>All classes are byte-code enhanced using hibernate enhance maven plugin :</p>
  <pre><code>&lt;build&gt;
      &lt;plugins&gt;
          &lt;plugin&gt;
              &lt;groupId&gt;org.hibernate.orm.tooling&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-enhance-maven-plugin&lt;/artifactId&gt;
              &lt;version&gt;5.2.2.Final&lt;/version&gt;
              &lt;dependencies&gt;
                  &lt;dependency&gt;
                      &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
                      &lt;artifactId&gt;hibernate-entitymanager&lt;/artifactId&gt;
                      &lt;version&gt;5.2.2.Final&lt;/version&gt;
                  &lt;/dependency&gt;
              &lt;/dependencies&gt;
              &lt;configuration&gt;
                  &lt;enableDirtyTracking&gt;true&lt;/enableDirtyTracking&gt;
                  &lt;enableLazyInitialization&gt;true&lt;/enableLazyInitialization&gt;
                  &lt;enableAssociationManagement&gt;true&lt;/enableAssociationManagement&gt;
              &lt;/configuration&gt;
          &lt;/plugin&gt;
      &lt;/plugins&gt;
  &lt;/build&gt;
  </code></pre>
  <p>I run two tests in order to verify that enhanced classes works properly :</p>
  <pre><code>@Test
  public void test1() {
      Department department = new Department();
      Employee employee = new Employee();
      department.getEmployees().add(employee);
      assertThat(employee.getDepartment(), is(not(nullValue())));
  }
  
  @Test
  public void test2() {
      Department department = new Department();
      Employee employee = new Employee();
      employee.setDepartment(department);
      assertThat(department.getEmployees().size(), is(1));
      assertThat(department.getEmployees().get(0), is(employee));
  }
  </code></pre>
  <p>Only the second test passes successfully, hence while the association is manipulated through parent's collection, the child's parent field is not updated, whereas in <a href="http://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html" rel="nofollow noreferrer">Hibernate ORM 5.2.3.Final User Guide</a>  is said</p>
  <blockquote>
  <p>Bytecode-enhanced bi-directional association management makes that first example work by managing the &quot;other side&quot; of a bi-directional association whenever one side is manipulated.</p>
  </blockquote>
  <p>Where referenced &quot;first example&quot; is</p>
  <blockquote>
  <p>Example 204. Incorrect normal Java usage</p>
  </blockquote>
  <p>Why in my test1 case the association management doesn't work ? What did I do wrong?</p>