为什么在尝试保存时检测到idEscala为null?

时间:2016-11-29 11:31:07

标签: hibernate jpa java-ee mapping

我收到此错误列' idEscala'当我试图保存valoracio时,不能为空。但我已经有了Escala的对象ID。

这可能是什么错误?

保存valoracio的方法

class TableView(QtGui.QTableView):

    def __init__(self, parent=None):
        super(TableView, self).__init__(parent)

    def closeEditor(self, editor, hint):
        is_cancelled = (hint == QtGui.QAbstractItemDelegate.RevertModelCache)

        if not is_cancelled:
            for index in self.selectedIndexes():
                if index == self.currentIndex():
                    continue

                # Supply None as the value
                self.model().setData(index, None, QtCore.Qt.EditRole)

        # Reset value for next input
        if self.model()._input_value is not None:
            self.model()._input_value = None

        return QtGui.QTableWidget.closeEditor(self, editor, hint)


class TableModel(QtCore.QAbstractTableModel):

    def __init__(self, parent=None):
        super(TableModel, self).__init__(parent)

        self.main_widget = parent

        self._input_value = None

    def setData(self, index, value, role):
        # Triggers if user cancelled input
        if value is None and self._input_value is None:
            return False

        if self._input_value is None:
            # The last selected cell will pass through here to store the value.
            self._input_value = value
        else:
            # All other cells will pass None, so just grab our stored value.
            value = self._input_value

        # Do whatever you want with value now

        return True

Valoracio实体

public Valoracio createValoracio(Escala escala, long idResident, long idUser) {
    logger.info("----------------initValoracio escala: " + escala);
    Valoracio valoracio = new Valoracio(escala, idResident, idUser);
    logger.info("----------------initValoracio valoracio: " + valoracio);

    preguntes = escalaPreguntaMgr.findByEscalaId(escala.getId());
    logger.info("----------------initValoracio num(" + preguntes.size() + ") pregunta: " + preguntes.get(0));

    ValoracioItem vitem = new ValoracioItem(valoracio, preguntes.get(0));
    vitem.setPregunta(preguntes.get(0));
    valoracio.addValoracioItem(vitem);
    logger.info("----------------initValoracio before save valoracio(idEscala): " + valoracio.getEscala().getId());
    logger.info("----------------initValoracio before save vitem: " + vitem);
    valoracioMgr.save(valoracio);

    return valoracio;
}

Escala实体

@Entity
@Table(name = "t_valoracio")
public class Valoracio extends BaseEntity {

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

    @ManyToOne
    @Basic(optional = false)
    @JoinColumn(name = "idEscala", referencedColumnName = "id")
    private Escala escala;

EscalaManager方法

@Entity
@Table(name = "a_escala")
public class Escala extends BaseEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @OneToMany(mappedBy = "escala")
    private Collection<Valoracio> valoracions = new HashSet();

错误

public Escala save(Escala escala) {
    return this.em.merge(escala);
}

1 个答案:

答案 0 :(得分:0)

问题是方法save()调用实体管理器方法合并到保存对象,在这种情况下因为escala没有保存,所以它已经没有id。 在这种情况下,当对象仍然没有id时,有必要使用 persist 方法。

当对象没有ID时,更改保留的方法保存