保存对象时出错,因为 null

Error saving object because a null

我在保存 ValoracioItem 时遇到错误,因为 idEscalaPregunta 为空。 在 LOG 中,我可以看到它正在尝试添加两个插入,第一个在 t_valoracioitem 中是正确的,但是第二个在 a_escalaresposta 中它已经在那里了,它不应该做任何事情. 然后回滚事务。

我看不出问题出在哪里。

保存方法

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<>();

日志

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               (?, ?, ?, ?)

问题是在classValoracioItem中修改了CascadeType.PERSIST中的属性pregunta和resposta并尝试保存它们而不是只保存外键,并且创建了一个新的EscalaResposta

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

@ManyToOne
@JoinColumn(name = "idEscalaResposta")
private EscalaResposta resposta;