title: Entities from one to many relationship out of date with JPA/EclipseLink
body: <p>I have been working with JPA/EclipseLink for a year but I still have a problem understanding how it updates the managed entities when you add an entity in a one to many relationship.</p>
  
  <p>For example, in my application I have projects which have several analysis inside. The project is first created and then the user can add analysis inside (one by one). My problem is: <strong>what is the proper way to persist a new analysis (and same question for deleting one from the project) so that the analysis is well managed and the project is updated?</strong></p>
  
  <p>Here is the project class :</p>
  
  <pre><code>@Entity
  @Table(name="project")
  public class Project implements Serializable {
  
  
  /** The Constant serialVersionUID. */
  private static final long serialVersionUID = 1L;
  
  /** The id project. */
  @Id
  @GeneratedValue(strategy=GenerationType.IDENTITY)
  @Column(name="id_project", unique=true, nullable=false)
  private Integer idProject;
  
  /** The project name. */
  @Column(name="project_name", nullable=false, length=50)
  private String projectName;
  
  //bi-directional many-to-one association to Analysis
  /** The analysis. */
  @OneToMany(mappedBy="project", cascade={CascadeType.ALL})
  private Set&lt;Analysis&gt; analysis;
  
      ... other stuff
  }
  </code></pre>
  
  <p>The Analysis class:</p>
  
  <pre><code>@Entity
  @Table(name="analyz")
  public class Analysis implements Serializable{
  
  /** The Constant serialVersionUID. */
  private static final long serialVersionUID = 1L;
  
  /** The id analysis. */
  @Id
  @GeneratedValue(strategy=GenerationType.IDENTITY)
  @Column(name="id_analysis", unique=true, nullable=false)
  private Integer idAnalysis;
  
  /** The analysis name. */
  @Column(name="analysis_name", nullable=false, length=50)
  private String analysisName;
  
  //bi-directional many-to-one association to Project
  /** The project. */
  @ManyToOne
  @JoinColumn(name="id_project", nullable=false)
  private Project project;
  
     ... other stuff
  }
  </code></pre>
  
  <p>The function I use for creating a new analysis:</p>
  
  <pre><code>public void createAnalysis(String name, int projectId) {
  
      Project project = daoProject.findById(projectId);
  
      Analysis analysis = new Analysis();
      analysis.setProject(project);
  
      analysis.setNameAnalysis(name);
      project.getAnalysis().add(analysis);
      daoProject.update(project);// I also tried daoAnalysis.create(analysis)
  }
  </code></pre>
  
  <p>If I update the project, my project object is up-to-date with a new analysis but the analysis object does not have its ID...
  If I persist the analysis by itself, when I retrieve the project object later, using its id, I don't get the updates on it and the new analysis is not in the project.</p>
  
  <p>Same problem for deleting an analysis: should I update the project or remove the analysis alone (using "daoAnalysis.delete(analysis)")?</p>
  
  <p>To give all the elements, here is my generic DAO (daoProject and daoAnalysis are instances of it):</p>
  
  <pre><code> public class BasicDAO&lt;T extends Serializable&gt; implements IDao&lt;T&gt; {
  
  /** The class of the entity to be manipulated. */
  private Class&lt;T&gt; entityClass;
  
  /** The entity manager factory. */
  protected EntityManagerFactory emf;
  
  /**
   * Instantiates a new abstract dao.
   *
   * @param c the class
   */
  public BasicDAO(Class&lt;T&gt; c) {
      entityClass = c;
  }
  
  /**
   * Gets the emf.
   *
   * @return the emf
   */
  public EntityManagerFactory getEmf() {
      return emf;
  }
  
  /**
   * Sets the emf.
   *
   * @param emf the new emf
   */
  public void setEmf(EntityManagerFactory emf) {
      this.emf = emf;
  }
  
  
  public T findById(Integer id){
      T result = null;
      EntityManager em = emf.createEntityManager();
      if (id == null || id &lt; 1)
          throw new PersistenceException("Id may not be null or negative");
      result = em.find(entityClass, id);
      em.close();
      return result;
  }
  
  
  public void create(T entity){
      System.out.println("Create de AbstractDAO");
      //First we check that the object is not alreadt in database
      List&lt;T&gt; list = findAll();
      if(list.contains(entity)){
          return;
      }
      EntityManager em = emf.createEntityManager();
      em.getTransaction().begin();
      if(entity == null)
          throw new PersistenceException("Entity to persist may not be null");//throw Persistence exception
      em.persist(entity);
      em.getTransaction().commit();
      em.close();
  }
  
  
  public void delete(T entity){
      EntityManager em = emf.createEntityManager();
      em.getTransaction().begin();
      if (entity == null)
          throw new PersistenceException("Entity to delete may not be null");
  
      em.remove(em.merge(entity));
      em.getTransaction().commit();
      em.close();
  }
  
  
  public T update(T entity){
      T result = null;
      if (entity == null){
          System.out.println("Exception : entity to update may not be null");
          throw new PersistenceException("Entity to update may not be null");
      }
      List&lt;T&gt; list = findAll();
      int numberEquals = 0;
      for(T elem : list){
          if(elem.equals(entity))
              numberEquals++;
      }
      if(numberEquals&gt;1)
          return null;
      EntityManager em = emf.createEntityManager();
      em.getTransaction().begin();
      em.merge(entity);
      result = entity;
      em.getTransaction().commit();
      em.close();
      return result;
  }
  </code></pre>
  
  <p>Thanks in advance for your advice!</p>