title: spring hibernate saving entity with onetomany fails
body: <p>I'm working in a project with springmvc and hibernate, I have 3 entites in model, here the code:</p>
  
  <p>Ciclos.java</p>
  
  <pre><code>@Entity
  @Table(name="ciclos") 
  public class Ciclos implements Serializable{
  
      private static final long serialVersionUID = 1L;
  
      @Id
      @Column(name = "id")
      @GeneratedValue(strategy = GenerationType.AUTO)
      private Integer id;
  
      @Column(name = "nombre")
      @NotEmpty(message="{string.empty}")
      @Size(min=5, max=55, message="{string.invalid.size}") 
      private String nombre;
  
      @Column(name = "fecha_inicial")
      @NotNull(message="{date.invalid}")
      private Date fechaInicial;
  
      @Column(name = "fecha_final")
      @NotNull(message="{date.invalid}")
      private Date fechaFinal;
  
      @OneToMany(cascade=CascadeType.ALL, fetch = FetchType.EAGER)
      @Fetch(value = FetchMode.SELECT)
      private List&lt;Niveles&gt; niveles;
      .....
  </code></pre>
  
  <p>Niveles.java</p>
  
  <pre><code>@Entity
  @Table(name="niveles") 
  public class Niveles implements Serializable{
  
      private static final long serialVersionUID = 1L;
  
      @Id
      @Column(name = "id")
      @GeneratedValue(strategy = GenerationType.AUTO)
      private Integer id;
  
      @Column(name = "nombre")
      @NotEmpty(message="{string.empty}")
      @Size(min=5, max=55, message="{string.invalid.size}") 
      private String nombre;
  
      @ManyToOne
      private Ciclos ciclo;
      @OneToMany(cascade=CascadeType.ALL, fetch = FetchType.EAGER)
      @Fetch(value = FetchMode.SELECT)
      private List&lt;Grados&gt; grados;
      ...
  </code></pre>
  
  <p>Grados.java</p>
  
  <pre><code>@Entity
  @Table(name="grados") 
  public class Grados implements Serializable {
  
      private static final long serialVersionUID = 1L;
  
      @Id
      @Column(name = "id")
      @GeneratedValue(strategy = GenerationType.AUTO)
      private Integer id;
  
      @Column(name = "grado")
      @NotNull(message="{grado.invalid}")
      @Min(1) 
      private Integer grado;
  
      @ManyToOne
      private Niveles nivel;
      ...
  </code></pre>
  
  <p>Now, let say I have a ciclo (Ciclos), which owns one Nivel and the Nivel owns 2 grados. Every entity its totally new, not saved yet on DB, I want to save all just saving Ciclos enity, I send to my daoimpl to this method.</p>
  
  <pre><code>@Transactional(readOnly=false)
  public Ciclos saveCiclo(Ciclos ciclo) {
      return em.merge(ciclo);
  }
  </code></pre>
  
  <p>But it always throws:</p>
  
  <pre><code>Hibernate: select ciclos0_.id as id1_0_1_, ciclos0_.fecha_final as fecha_fi2_0_1_, ciclos0_.fecha_inicial as fecha_in3_0_1_,ciclos0_.nombre as nombre4_0_1_, niveles1_.ciclos_id as ciclos_i1_0_3_, niveles2_.id as niveles_2_1_3_, niveles2_.id as id1_3_0_, niveles2_.ciclo_id as ciclo_id3_3_0_, niveles2_.nombre as nombre2_3_0_ from ciclos ciclos0_ left outer join ciclos_niveles niveles1_ on ciclos0_.id=niveles1_.ciclos_id left outer join niveles niveles2_ on niveles1_.niveles_id=niveles2_.id where ciclos0_.id=?
  Hibernate: insert into ciclos (fecha_final, fecha_inicial, nombre) values (?, ?, ?) Hibernate: insert into niveles (ciclo_id, nombre) values (?, ?) Hibernate: insert into grados (grado, nivel_id) values (?, ?) 
  Hibernate: insert into grados (grado, nivel_id) values (?, ?)
  Hibernate: insert into ciclos_niveles (ciclos_id, niveles_id) values (?, ?) 
  Hibernate: insert into ciclos_niveles (ciclos_id, niveles_id) values (?, ?)   
  org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement   at
  org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:248) at
  org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:214) at
  org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:521)
  </code></pre>
  
  <p>Whats the problem?</p>