Search code examples
hibernatetransactionsjava-ee-7jpa-2.1

Error saving object because a null


enter image description here

I'm getting an error at saving ValoracioItem because is idEscalaPregunta is null. In LOG I can see that it is trying to add two inserts the first one in t_valoracioitem that is correct, but the second one in a_escalaresposta that it is already in there, and it shouldn't do anything. And then the transaction is rolled back.

I can not see where the problem is.

Method to save

public Valoracio createValoracio(Escala escala, long idResident, long idUser) {

   Valoracio valoracio = valoracioMgr.findById(8L);

   preguntes = escalaPreguntaMgr.findByEscalaId(escala.getId());

   ValoracioItem vitem = new ValoracioItem(valoracio, preguntes.get(0));
   //pretuntes.get(0).getId() is not null and has id
   vitem.setResposta(new EscalaResposta());

   ValoracioItemMgr.save(vitem);

   return valoracio;
}

Class ValoracioItem

@Entity
@Table(name = "t_valoracioItem")
public class ValoracioItem extends BaseEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @ManyToOne(cascade = CascadeType.PERSIST, optional = false)
    @JoinColumn(name = "idEscalaPregunta", nullable = false)
    private EscalaPregunta pregunta;

    @ManyToOne(cascade = CascadeType.PERSIST)
    @JoinColumn(name = "idEscalaResposta")
    private EscalaResposta resposta;

Class EscalaPregunta

@Entity
@Table(name = "a_escalaPregunta")
public class EscalaPregunta extends BaseEntity {

    static final String PREFIX = "pia.entity.EscalaPregunta.";
    public static final String findAll = PREFIX + "findAll";
    public static final String findById = PREFIX + "findById";
    public static final String findByEscalaId = PREFIX + "findByEscalaId";
    public static final String findByPregunta = PREFIX + "findByPregunta";

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String pregunta;

    @OneToMany(mappedBy = "pregunta", cascade = CascadeType.ALL)
    private Collection<EscalaResposta> respostes;

    @OneToMany(mappedBy = "pregunta", cascade = CascadeType.PERSIST)
    private Collection<ValoracioItem> valoracioItems = new HashSet<>();

Class EscalaResposta

@Entity
@Table(name = "a_escalaResposta")
public class EscalaResposta extends BaseEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @ManyToOne(optional = false)
    @JoinColumn(name = "idEscalaPregunta", referencedColumnName = "id", nullable = false)
    private EscalaPregunta pregunta;

    @OneToMany(mappedBy = "resposta", cascade = CascadeType.PERSIST)
    private Collection<ValoracioItem> valoracioItems = new HashSet<>();

Log

13:54:01,698 INFO   Hibernate: 
13:54:01,698 INFO       /* insert es.business.pia.entity.ValoracioItem
13:54:01,698 INFO           */ insert 
13:54:01,698 INFO           into
13:54:01,698 INFO               t_valoracioItem
13:54:01,698 INFO               (idEscalaPregunta, idEscalaResposta, idValoracio) 
13:54:01,698 INFO           values
13:54:01,698 INFO               (?, ?, ?)
13:54:01,705 INFO   Hibernate: 
13:54:01,705 INFO       /* insert es.business.pia.entity.EscalaResposta
13:54:01,705 INFO           */ insert 
13:54:01,705 INFO           into
13:54:01,705 INFO               a_escalaResposta
13:54:01,705 INFO               (explicacio, idEscalaPregunta, punts, resposta) 
13:54:01,705 INFO           values
13:54:01,705 INFO               (?, ?, ?, ?)

Solution

  • The problem is that it is being modified in class ValoracioItem the properties pregunta and resposta in CascadeType.PERSIST and try to save them instead of saving only the foreign key, and it is created a new EscalaResposta

    @ManyToOne
    @JoinColumn(name = "idEscalaPregunta", nullable = false)
    private EscalaPregunta pregunta;
    
    @ManyToOne
    @JoinColumn(name = "idEscalaResposta")
    private EscalaResposta resposta;