2014-12-02 156 views
0

我在用SpringMVC有一個項目工作和休眠,我在模型3個entites的,這裏的代碼:春天休眠保存實體失敗

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。每個實體都是全新的,沒有保存在數據庫上,我想保存所有的只保存Ciclos的實體,我發送給我的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) 

請告訴我這個問題?

回答

0

根據this您需要爲雙向關係提供mappedBy元素。

+0

感謝這正是我所需要的 – Peter 2014-12-03 01:48:39