title: Hibernate: Doman Model to JPA Entity/DTO & Merge() Design pattern or best practice
body: <p>The recommended way of using merge() is to first get the DTO first before inputting the changes.</p>
  
  <pre><code>public void merge(PersonModel model) {
  
  Person inputDTO = PersonBuilder.build(model)
  
  Person dto = get(pk)
  dto.setName(inputDTO.getName())
  
  dto.getChildren().clear()
  
  Iterator&lt;Child&gt; iter = inputDTO .getChildren().Iterator();
  
  while(iter.hasNext()){
     dto.getChildren().add(iter.next());
  }
  
  dto.merge();
  </code></pre>
  
  <p>}</p>
  
  <p>Is there a more elegant way of performing such operation translating domain model to dto and merging it so that no data are accidentally deleted.</p>
  
  <p>Example of problem: 
  <a href="https://stackoverflow.com/questions/11098266/hibernate-prevent-delete-orphan-when-using-merge">Hibernate: prevent delete orphan when using merge();</a></p>
  
  <p>I find the need to clear the list and adding it very wasteful.</p>
  
  <p>Can someone recommend me a design pattern or a way to code it properly?</p>
  
  <p>Thank you </p>
  
  <p>ADD ON:</p>
  
  <p>1) Possible to use Hibernate Hashset to replace List? Will hibernate hashset replace elements base on primary keys? </p>
  
  <p>any help?</p>