title: Hibernate - ManyToOne / OneToOne with Cascade options
body: <p>Say I have a <code>class A</code>, which has an attribute of <code>class B</code>. </p>
  
  <p>I know that there will only ever be one <code>class B</code> for every <code>class A</code>, so I map this with <code>OneToOne</code>. If I mistakenly add the same <code>class B</code> to a second <code>class A</code>, both <code>class A</code>s reference the same <code>class B</code> (and so, with <code>CascadeType.ALL</code> set, a delete on one of these objects will remove the address referenced by the other). This all makes sense.</p>
  
  <p>However, if I know that there may be multiple (same) instances of <code>class B</code> in the database, and I want any <code>class A</code>s to use this persisted attribute if it exists, I thought I could map this entity as <code>ManyToOne</code> and JPA / hibernate would take care of any necessary checks to see if the data is already in the database (loading and then using equality). It seems this isn't the case.</p>
  
  <p>The <code>ManyToOne</code> relationship works in exactly the same way as the <code>OneToOne</code> above - i.e. as soon as I delete one of the <code>class A</code> objects that references a <code>class B</code> object, the 'class B' object gets deleted, too (even if referenced elsewhere). I would expect JPA / hibernate to check this <code>class B</code> object isn't referenced by any other <code>class A</code> object before deleting (even with <code>CascadeType.ALL</code> set - as it is marked up as a <code>ManyToOne</code> relationship).</p>
  
  <p>Am I missing something here?</p>