title: one-to-many JPA annotations won't delete orphans
body: <p>I 'm trying to build a user and a contact management project.
  i have a lot of classes so will limit it to what are in concern here. i have a userAccount, userProfile, and group
  here is the UserAccount Mapping</p>
  
  <pre><code>@Id @GeneratedValue
  @Column(name="USER_ACCOUNT_ID")
  private Long ID; 
   ......
   @Column(name="EMAIL", length=100, unique=true)
  private String email;
  
  @OneToOne(targetEntity=UserProfileImpl.class,cascade={CascadeType.ALL})
   @org.hibernate.annotations.Cascade(value=org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
  @JoinColumn(name="USER_PROFILE_ID")
  private UserProfile profile;
  
   @OneToMany(targetEntity=GroupImpl.class, cascade={CascadeType.ALL})
   //    @JoinColumn(name="USER_ACCOUNT_ID")
  @JoinTable(name = "USER_ACCOUNT_CONTACT_GROUP", joinColumns = @JoinColumn(name = "USER_ACCOUNT_ID"), inverseJoinColumns = @JoinColumn(name = "GROUP_ID"))
  @org.hibernate.annotations.Cascade(value=org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
  private Set&lt;Group&gt; groups = new HashSet&lt;Group&gt;();
  ........
  </code></pre>
  
  <p>here is the userProfile</p>
  
  <pre><code>@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
  @Column(name="USER_PROFILE_ID")
  private Long ID;
  .....
   @OneToOne(mappedBy="profile", targetEntity=UserAccountImpl.class)
  private UserAccount userAccount;
  .....
  </code></pre>
  
  <p>here is the group</p>
  
  <pre><code>@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
  @Column(name="GROUP_ID")
  private Long ID;    
  .......
  @Column(name="NAME", length=100, unique=true)
  private String Name;
  
  @ManyToOne(targetEntity=UserAccountImpl.class)
  @JoinColumn(name="USER_ACCOUNT_ID",nullable=false)
  private UserAccount userAccount;
  </code></pre>
  
  <p>so basically this is it.Everything went fine before a did a bidirectional association adding userAccount object to group.so on testing. i create userProfile, userAccount ,and group(using spring @autowired) .<br>
  On the setUP (@Before) method i set few prop to userProfile and add it to useAccount along with its prop and persist it and then set the useAccount to the group.<br>
  on the tearDown(@After) i delete the userACcount .<br>
  on my testSave (saves the group) works fine (using the same session for all the operations) but there is no delete sql query on the console for the group but it deletes the userAccount Though.<br>
  By deleting the userAccount in the teardown method i was hoping group should be deleted to.But that that's not the case.<br>
  worse because of the unique constraint on  name of the group the other tests are failling.I search on the net but everything i try doesn't seem to work.Who can save me :) ? i mean how am i supposed to do it? thanks for reading</p>
  
  <p>this is an update on what i've tried based on response i've got.
  i've added this to <code>group.setUserAccount(null)</code> to my <code>removeGroup</code> which is now this</p>
  
  <pre><code>public void removeGroup(Group group) {
      try{
         if(this.groups.contains(group)){
          group.setUserAccount(null);
          this.groups.remove(group);
         }
      } catch(Exception ex){
          ex.printStackTrace();
      }
  }
  </code></pre>
  
  <p>so for the test i used which has the same behavior ie deletes the userAccount but not the group</p>
  
  <pre><code>ua1.removeGroup(g1);
  uaDao.delete(ua1);
  </code></pre>
  
  <p>i guess it's not entering the if block because i should have the same behavior as this one:</p>
  
  <pre><code> g1.setUserAccount(null);
   uaDao.delete(ua1);
  </code></pre>
  
  <p>this other one throws an error </p>
  
  <blockquote>
    <p>PropertyValueException: not-null property references a null or transient value  </p>
  </blockquote>
  
  <p>so i think i'll delete the group using :</p>
  
  <pre><code>gDao.delete(g1);
  uaDao.delete(ua1);
  </code></pre>
  
  <p>hoping that i might find a way to go about it.Thanks to people who have helped me out especially Pascal if you had another idea or found something wrong about my code i'll be more than glad to correct it.thanks for reading</p>