title: Hibernate Save/Update Cascade
body: <p>There is a class A like:</p>
  
  <pre><code>class A {
       @Id
       @GeneratedValue(strategy = GenerationType.IDENTITY)
       @Basic(optional = false)
       @Column(name = "DBId")
       private Long DBId = null;
  
       @OneToMany(cascade = CascadeType.ALL)
       @LazyCollection(LazyCollectionOption.FALSE)
       List&lt;B&gt; list;
  }
  </code></pre>
  
  <p>and a class B like</p>
  
  <pre><code>class B {
       @Id
       @GeneratedValue(strategy = GenerationType.IDENTITY)
       @Basic(optional = false)
       @Column(name = "DBId")
       private Long DBId = null;
  }
  </code></pre>
  
  <p>When I want to persist a new instance of class A, two cases are possible.
  1- All instances of B in A are new, in which case, a simple session.save(A) is ok.
  2- Some or all of the instances of B in A are old. How should I save A in order not to have duplicate Bs. Will saveOrUpdate solve the problem? or it will just decide to choose save or update just by looking at A, and for example if A was new, it will also call save on Bs, and if A was old, it will also call update on Bs?</p>
  
  <p>Furthermore, I saw that I had just a simple session.save(A) in my codes, and it did not produce redundant Bs. Am I wrong? Is this possible?</p>
  
  <p>Regards</p>
  
  <p><strong>UPDATE:</strong> in case of old Bs, the instances of old Bs are attached to the session, and DBId is correctly set.</p>