title: JPA (Hibernate) issue saving ManyToOne relationship
body: <p>I'm experiencing a problem: I have an Entity Definition, that has a OneToMany relationship with DefinitionInfo Entities and when I try to load an existing Definition, add a DefinitionInfo to it's infoList collection, saving says it can not find the entity of the DefinitionInfo I just added to the Definition.  To this I say 'DUH!' I am trying to add a new DefintionInfo to the Definition so of course you won't find it!</p>
  
  <p>Here's the code:</p>
  
  <pre><code>@Entity(name = "CohortDefinition")
  @Table(name="cohort_definition")
  @NamedEntityGraph(
      name = "CohortDefinition.withDetail",
      attributeNodes = { @NamedAttributeNode(value = "details", subgraph = "detailsGraph") },
      subgraphs = {@NamedSubgraph(name = "detailsGraph", type = CohortDefinitionDetails.class, attributeNodes = { @NamedAttributeNode(value="expression")})}
  )
  public class CohortDefinition implements Serializable{
  
    private static final long serialVersionUID = 1L;
  ...
   @OneToMany(mappedBy="cohortDefinition", fetch= FetchType.LAZY)
    private Set&lt;CohortGenerationInfo&gt; generationInfoList;
  ...
  + Getters and Setters
  </code></pre>
  
  <p>And the target/dependent object:</p>
  
  <pre><code>Entity(name = "CohortGenerationInfo")
  @Table(name="cohort_generation_info")
  public class CohortGenerationInfo implements Serializable {
    private static final long serialVersionUID = 1L;
  
    public  CohortGenerationInfo()
    {    
    }
  
    public  CohortGenerationInfo(Integer cohortDefinitionId, Integer sourceId)
    {    
      this.id = new CohortGenerationInfoPK(cohortDefinitionId, sourceId);
    }
  
    @EmbeddedId
    private CohortGenerationInfoPK id;
  
    @ManyToOne
    @MapsId("cohortDefinitionId")
    @JoinColumn(name="id")
    private CohortDefinition cohortDefinition;
  </code></pre>
  
  <p>Here is the EmbeddedID class (The Id for the Info consists of the definition's ID + a ID of another entity that represents differetnt types of info.</p>
  
  <pre><code>@Embeddable
  public class CohortGenerationInfoPK implements Serializable {
  
    private static final long serialVersionUID = 1L;
  
    public CohortGenerationInfoPK() {
    }
  
    public CohortGenerationInfoPK(Integer cohortDefinitionId, Integer sourceId) {
      this.cohortDefinitionId = cohortDefinitionId;
      this.sourceId = sourceId;
    }
  
    @Column(name = "id")
    private Integer cohortDefinitionId;
  
    @Column(name = "source_id")
    private Integer sourceId;
  
  + getter/setter
  </code></pre>
  
  <p>Here is what I am doing. This should be so simple, but I just don't understand what is going on:</p>
  
  <pre><code>  Source source = this.getSourceRepository().findBySourceKey(sourceKey);
    CohortDefinition currentDefinition = this.cohortDefinitionRepository.findOne(id);
    CohortGenerationInfo info = findBySourceId(currentDefinition.getGenerationInfoList(), source.getSourceId());
    if (info == null)
    {
      info = new CohortGenerationInfo(currentDefinition.getId(), source.getSourceId());
      currentDefinition.getGenerationInfoList().add(info);
    }
    info.setStatus(GenerationStatus.PENDING)
      .setStartTime(Calendar.getInstance().getTime());
  
    this.cohortDefinitionRepository.save(currentDefinition);
  </code></pre>
  
  <p>Here's the Hibernate select just before it blows up:</p>
  
  <pre><code>Hibernate: select cohortgene0_.id as id1_4_0_, cohortgene0_.source_id as source_i2_4_0_, cohortgene0_.execution_duration as executio3_4_0_, cohortgene0_.is_valid as is_valid4_4_0_, cohortgene0_.start_time as start_ti5_4_0_, cohortgene0_.status as status6_4_0_, cohortdefi1_.id as id1_2_1_, cohortdefi1_.created_by as created_2_2_1_, cohortdefi1_.created_date as created_3_2_1_, cohortdefi1_.description as descript4_2_1_, cohortdefi1_.expression_type as expressi5_2_1_, cohortdefi1_.modified_by as modified6_2_1_, cohortdefi1_.modified_date as modified7_2_1_, cohortdefi1_.name as name8_2_1_ from cohort_generation_info cohortgene0_ inner join cohort_definition cohortdefi1_ on cohortgene0_.id=cohortdefi1_.id where cohortgene0_.id=? and cohortgene0_.source_id=?
  </code></pre>
  
  <p>Here is the stack trace:</p>
  
  <pre><code>javax.persistence.EntityNotFoundException: Unable to find org.ohdsi.webapi.cohortdefinition.CohortGenerationInfo with id org.ohdsi.webapi.cohortdefinition.CohortGenerationInfoPK@3
      at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$JpaEntityNotFoundDelegate.handleEntityNotFound(EntityManagerFactoryBuilderImpl.java:183)
      at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:219)
      at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:275)
      at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:151)
      at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1070)
      at org.hibernate.internal.SessionImpl.internalLoad(SessionImpl.java:989)
      at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:716)
      at org.hibernate.type.EntityType.resolve(EntityType.java:502)
      at org.hibernate.type.EntityType.replace(EntityType.java:366)
      at org.hibernate.type.CollectionType.replaceElements(CollectionType.java:549)
      at org.hibernate.type.CollectionType.replace(CollectionType.java:690)
      at org.hibernate.type.TypeHelper.replace(TypeHelper.java:177)
      at org.hibernate.event.internal.DefaultMergeEventListener.copyValues(DefaultMergeEventListener.java:407)
      at org.hibernate.event.internal.DefaultMergeEventListener.entityIsPersistent(DefaultMergeEventListener.java:219)
      at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:192)
      at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:85)
      at org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:876)
      at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:858)
      at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:863)
      at org.hibernate.jpa.spi.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:1196)
  </code></pre>
  
  <p>I stoppped here at the merge part of the stack trace. It appears to be fetchign the existing entity from the DB and mereg it.  The Definition DOES EXIST, it's just a new DefinitionInfo being added.  </p>
  
  <p>Does anyone have any thoughts?  I have some limitations on how to structure the database, but I'm hoping this isn't a database structure problem, rather just somethign annoying with JPA.</p>