简体   繁体   中英

spring hibernate saving entity with onetomany fails

I'm working in a project with springmvc and hibernate, I have 3 entites in model, here the code:

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;
    ...

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.

@Transactional(readOnly=false)
public Ciclos saveCiclo(Ciclos ciclo) {
    return em.merge(ciclo);
}

But it always throws:

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)

Whats the problem?

根据这个你需要的双向关系提供的mappedBy元素。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM