【问题标题】:Error saving object because a null保存对象时出错,因为 null
【发布时间】:2016-11-28 13:16:09
【问题描述】:

我在保存 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;
}

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

【问题讨论】:

    标签: hibernate transactions java-ee-7 jpa-2.1


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多