title: Hibernate inserting duplicate parent on insertion of child object
body: <p>I have a parent record that already exists in my database.  I am trying to add a child and associate it to this parent.  They share the same class.  When I attempt to add the child, it remakes the parent entity.  It seems like Hibernate is mistakenly thinking that I am making a new parent.  Is it a Hibernate tag or maybe there is something getting passed by value instead of reference?</p>
  
  <p><strong>Entity Definition</strong></p>
  
  <pre><code>@Id
  @GeneratedValue(generator="entitySequence", strategy=GenerationType.AUTO)
  @SequenceGenerator(name="entitySequence", initialValue=1,
  sequenceName="entity_sequence", schema="user_portal")
  private Long id;
  
  ...
  
  
  @ManyToOne(fetch=FetchType.LAZY, cascade={CascadeType.PERSIST, 
  CascadeType.MERGE, CascadeType.REMOVE})
  @JoinColumn(name="parent_id", referencedColumnName="id")
  @JsonBackReference
  private Entity parentEntity;
  
  
  @OneToMany(mappedBy="parentEntity", fetch=FetchType.LAZY, 
  targetEntity=Entity.class, 
  cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE})
  @JsonManagedReference
  private List&lt;Entity&gt; childEntities;
  </code></pre>
  
  <p><strong>Controller Code</strong></p>
  
  <pre><code>@RequestMapping(
  value = "/add",
      method = RequestMethod.POST,
      headers = HTTP_ACCEPT_JSON_HEADER, 
      produces = HTTP_RESPONSE_JSON_HEADER)
  public @ResponseBody WebResponse addEntity(@RequestBody Entity s) {
      WebResponse wr = new WebDataResponse();
  
      try {
          s = EntityService.saveEntity(s);
  
          if (s != null) {
              handleSuccess(wr, s, 1);
          } else {
              handleSuccess(wr, null, "Error adding Entity.", 0);
          }
  
      } catch (Exception ex) {
          handleException(wr, ex);
      }
  
      return wr;
  }
  </code></pre>
  
  <p><strong>Service Code</strong></p>
  
  <pre><code>@Override
  @Transactional
  public Entity saveEntity(Entity s) {
  
      String entityNumber = s.getEntityNumber();
      boolean isAddendum = StringUtils.countOccurrencesOf(entityNumber, "-") == 4;
  
  
      if(isAddendum) {
  Entity parent = getEntityByNumber(entityNumber.substring(0,entityNumber.lastIndexOf("-")));
          if(parent !=null) {
              s.setParentEntity(parent);
          }else {
              return null;
          }
      }
  
      s.setCreatedBy("admin");
      s.setCreatedDate(new Date());
  
      s = entityRepository.save(s);
  
      return s;
  }
  
  @Override
  @Transactional(readOnly = true)
  public Entity getEntityByNumber(String entityNum) {
      Entity s = entityRepository
              .findByEntityNumber(entityNum);
  
      return s;
  }
  </code></pre>
  
  <p>Any help is greatly appreciated! Thank you!</p>