我在使用springmvc和hibernate的项目中工作,我在模型中有3个参与者,这里是代码:
Ciclos.java
@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<Niveles> niveles;
.....
Niveles.java
@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<Grados> grados;
...
Grados.java
@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;
...
现在,假设我有一个ciclo(Ciclos),它拥有一个Nivel,而Nivel拥有2个grados。每个实体都是全新的,还没有保存在DB上,我想保存所有只是保存Ciclos enity,我发送给我的daoimpl这个方法。
@Transactional(readOnly=false)
public Ciclos saveCiclo(Ciclos ciclo) {
return em.merge(ciclo);
}
但它总是抛出:
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)
问题是什么?