title: Cascading save not working with Hibernate 4 and @OneToMany
body: <p>I've been having an issue with Hibernate 4  that did not happen in Hibernate 3.  I'm testing a base dao class which has generic methods for saving, querying, etc.. (not it's not homework).  I'm using an embedded derby connection with hibernate.hbm2ddl.auto=create-drop for testing purposes.</p>
  
  <p>EDIT:  Fixing cascadeType.  Still the same behavior.  Even if i add in all the available cascade types from the CascadeType enum, it still fails, unless I put CascadeType.ALL.  Not sure what ALL has that you can't specifically enumerate.</p>
  
  <p>In my tests, I have following mapped classes:</p>
  
  <pre><code>@Entity
  @Table(name="FOO_CHILD")
  public class FooChild {
      @Id
      @Column(name="FOO_ID")
      private Long id;
  
      @Column(name="NAME")
      private String name;
  
      public Long getId() {
          return id;
      }
  
      public void setId(Long id) {
          this.id = id;
      }
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  }
  
  @Entity
  @Table(name="FOO_PARENT")
  public class FooParent {
      @Id
      @Column(name="FOO_PARENT_ID")
      private Long id;
  
      @Column(name="NAME")
      private String name;
  
      @Fetch(FetchMode.SELECT)
      @Type(type="long")
      @OneToMany(cascade={CascadeType.PERSIST, CascadeType.MERGE})
      @JoinTable(name="FOO_PARENT_FOO",
              joinColumns={@JoinColumn(referencedColumnName="FOO_PARENT_ID")},
              inverseJoinColumns={@JoinColumn(referencedColumnName="FOO_ID")})
      private List&lt;Foo&gt; fooChild;
  
      public Long getId() {
          return id;
      }
  
      public void setId(Long id) {
          this.id = id;
      }
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      public List&lt;Foo&gt; getFooChild() {
          return fooChild;
      }
  
      public void setFooChild(List&lt;Foo&gt; fooChild) {
          this.fooChild = fooChild;
      }
  }
  </code></pre>
  
  <p>And then i try to save:</p>
  
  <pre><code>FooChild fooChild = new FooChild();
  fooChild.setId(1L);
  fooChild.setName("fooChild");
  
  FooParent fooParent = new FooParent();
  fooParent.setId(1L);
  fooParent.setName("fooParent");
  fooParent.setFooChild(new ArrayList&lt;FooChild&gt;(Arrays.asList(fooChild)));
  
  session.save(fooParent);
  session.flush();
  session.clear();
  </code></pre>
  
  <p>I get the following trace:</p>
  
  <pre><code>org.hibernate.exception.ConstraintViolationException: INSERT on table 'FOO_PARENT_FOO' caused a violation of foreign key constraint 'FKF18290CAECA19CB8' for key (1).  The statement has been rolled back.
      at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:74)
  ...
  </code></pre>
  
  <p>I noticed the following sql being executed:</p>
  
  <pre><code>Hibernate: 
      insert 
      into
          foo_parent
          (name, foo_parent_id) 
      values
          (?, ?, ?, ?)
  Hibernate: 
      insert 
      into
          foo_parent_foo
          (foo_parent, foo_child) 
      values
          (?, ?)
  </code></pre>
  
  <p>It seems it's trying to insert into the collection before cascading the save down to the FooChild object.</p>
  
  <p>If i change FooParent to have this:</p>
  
  <pre><code>@OneToMany(cascade={CascadeType.ALL})
  </code></pre>
  
  <p>The problem goes away.</p>
  
  <p>As i said, this works with hibernate 3, so i'm not sure why it's behaving differently.</p>