I'm usign Spring + JPA + Hibernate + postgres, my problem is with cascade, I'm trying to save a "Criterio" adding a "Justificativa", and then save the criterio by cascading, but the Foreign Key is not setted
"Caused by: org.postgresql.util.PSQLException: ERROR: null value in column "id" violates not-null constraint
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2102)"
Code:
@Entity
public class Criterio extends PersistableImpl {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id", unique=true, nullable=false)
Integer id;
@Column
private String name;
@OneToMany(mappedBy="criterio", cascade= {javax.persistence.CascadeType.ALL} )
private List<Justificativa> justificativas;
.....GETTERS AND SETTERS
Code:
@Entity
public class Justificativa extends PersistableImpl {
private static final long serialVersionUID = -1374632123186791897L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id", unique=true, nullable=false)
Integer id;
@Column(name="description")
private String description;
@ManyToOne()
@JoinColumn(name="criterio_id", nullable=false, insertable=true, updatable=true)
private Criterio criterio;
....GETTERS AND SETTERS
/////////
Code:
Justificativa just = new Justificativa();
just.setDescription("test");
criterio.getJustificativas().add(just);
criterio = service.save(criterio);
////////
The service.save calls the following method
Code:
@Override
@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
public T saveOrUpdate(T t) {
this.em.clear();
this.em.merge(t);
return t;
}
Someone can help me? I've no Idea what is the problem, I tried some solutions, but no one worked for me.
Thanks