0
我挽救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;
}
類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;
類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<>();
類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 (?, ?, ?, ?)