我在保存ValoracioItem时遇到错误,因为idEscalaPregunta为null。 在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 (?, ?, ?, ?)
答案 0 :(得分:0)
问题是它正在ValoracioItem类中修改CascadeType.PERSIST中的属性pregunta和resposta并尝试保存它们而不是仅保存外键,并创建一个新的EscalaResposta
@ManyToOne
@JoinColumn(name = "idEscalaPregunta", nullable = false)
private EscalaPregunta pregunta;
@ManyToOne
@JoinColumn(name = "idEscalaResposta")
private EscalaResposta resposta;