使用OneToMany在Hibernate上更新失败

时间:2014-05-12 01:11:58

标签: java spring hibernate jpa annotations

嘿伙计们我正在尝试更新我在Hibernate管理的实体中的条目,它有3个关系ManyToOne非识别。

当我尝试更新提供 quantdade 列的Id和新值时,抛出异常

堆栈跟踪:

org.hibernate.PropertyValueException: not-null property references a null or transient value : br.com.cheetah.catalog.entity.ModeloPeca.armazemByArmazemIdarmazem
    at org.hibernate.engine.internal.Nullability.checkNullability(Nullability.java:106)
    at org.hibernate.event.internal.DefaultFlushEntityEventListener.scheduleUpdate(DefaultFlushEntityEventListener.java:309)
    at org.hibernate.event.internal.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:160)
    at org.hibernate.event.internal.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:231)
    at org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:102)
    at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:55)
    at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1222)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:1335)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:262)
    at com.sun.proxy.$Proxy31.flush(Unknown Source)
    at org.springframework.data.jpa.repository.support.SimpleJpaRepository.flush(SimpleJpaRepository.java:416)
    at org.springframework.data.jpa.repository.support.SimpleJpaRepository.saveAndFlush(SimpleJpaRepository.java:384)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:358)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:343)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.data.jpa.repository.support.LockModeRepositoryPostProcessor$LockModePopulatingMethodIntercceptor.invoke(LockModeRepositoryPostProcessor.java:105)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
    at com.sun.proxy.$Proxy37.saveAndFlush(Unknown Source)
    at br.com.cheetah.catalog.controller.business.ModelosEmUsoController.addModeloEmUso(ModelosEmUsoController.java:58)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:644)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:301)

实体:

@Entity
@Table(name = "modelopeca")
public class ModeloPeca
{
    private Integer idPeca;
    private String modelo;
    private Date dataAquisicao;
    private String estado;
    private String marca;
    private String detalhe;
    private Double valor;
    private String tipo;
    private Integer quantidade;
    private Armazem armazemByArmazemId;
    private Fornecedor fornecedorByFornecedorId;
    private SubEquipe subequipeBySubEquipeId;
    private Collection<ModelosEmUso> modelosemusosByIdPeca;


    @Id
    @Column(name = "idPeca")
    @GeneratedValue
    public Integer getIdPeca()
    {
        return idPeca;
    }

    public void setIdPeca(Integer idPeca)
    {
        this.idPeca = idPeca;
    }

    @Basic
    @Column(name = "modelo")
    public String getModelo()
    {
        return modelo;
    }

    public void setModelo(String modelo)
    {
        this.modelo = modelo;
    }

    @Basic
    @Column(name = "dataAquisicao")
    public Date getDataAquisicao()
    {
        return dataAquisicao;
    }

    public void setDataAquisicao(Date dataAquisicao)
    {
        this.dataAquisicao = dataAquisicao;
    }

    @Basic
    @Column(name = "estado")
    public String getEstado()
    {
        return estado;
    }

    public void setEstado(String estado)
    {
        this.estado = estado;
    }

    @Basic
    @Column(name = "marca")
    public String getMarca()
    {
        return marca;
    }

    public void setMarca(String marca)
    {
        this.marca = marca;
    }

    @Basic
    @Column(name = "detalhe")
    public String getDetalhe()
    {
        return detalhe;
    }

    public void setDetalhe(String detalhe)
    {
        this.detalhe = detalhe;
    }

    @Basic
    @Column(name = "valor")
    public Double getValor()
    {
        return valor;
    }

    public void setValor(Double valor)
    {
        this.valor = valor;
    }

    @Basic
    @Column(name = "tipo")
    public String getTipo()
    {
        return tipo;
    }

    public void setTipo(String tipo)
    {
        this.tipo = tipo;
    }

    @Basic
    @Column(name = "quantidade")
    public Integer getQuantidade()
    {
        return quantidade;
    }

    public void setQuantidade(Integer quantidade)
    {
        this.quantidade = quantidade;
    }

    @Override
    public boolean equals(Object o)
    {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        ModeloPeca that = (ModeloPeca) o;

        if (dataAquisicao != null ? !dataAquisicao.equals(that.dataAquisicao)
                    : that.dataAquisicao != null)
            return false;
        if (detalhe != null ? !detalhe.equals(that.detalhe) : that.detalhe !=
                                                                      null)
            return false;
        if (estado != null ? !estado.equals(that.estado) : that.estado != null)
            return false;
        if (idPeca != null ? !idPeca.equals(that.idPeca) : that.idPeca != null)
            return false;
        if (marca != null ? !marca.equals(that.marca) : that.marca != null)
            return false;
        if (modelo != null ? !modelo.equals(that.modelo) : that.modelo != null)
            return false;
        if (quantidade != null ? !quantidade.equals(that.quantidade) : that.quantidade != null)
            return false;
        if (tipo != null ? !tipo.equals(that.tipo) : that.tipo != null)
            return false;
        if (valor != null ? !valor.equals(that.valor) : that.valor != null)
            return false;

        return true;
    }

    @Override
    public int hashCode()
    {
        int result = idPeca != null ? idPeca.hashCode() : 0;
        result = 31 * result + (modelo != null ? modelo.hashCode() : 0);
        result = 31 * result + (dataAquisicao != null ? dataAquisicao
                                                                .hashCode() :
                                        0);
        result = 31 * result + (estado != null ? estado.hashCode() : 0);
        result = 31 * result + (marca != null ? marca.hashCode() : 0);
        result = 31 * result + (detalhe != null ? detalhe.hashCode() : 0);
        result = 31 * result + (valor != null ? valor.hashCode() : 0);
        result = 31 * result + (tipo != null ? tipo.hashCode() : 0);
        result = 31 * result + (quantidade != null ? quantidade.hashCode() : 0);
        return result;
    }

    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name = "armazem_idarmazem",
                referencedColumnName = "idArmazem",
                nullable = false)
    public Armazem getArmazemByArmazemId()
    {
        return armazemByArmazemId;
    }

    public void setArmazemByArmazemId(Armazem armazemByArmazemIdarmazem)
    {
        this.armazemByArmazemId = armazemByArmazemIdarmazem;
    }

    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name = "Fornecedor_idFornecedor",
                referencedColumnName = "idFornecedor",
                nullable = false)
    public Fornecedor getFornecedorByFornecedorId()
    {
        return fornecedorByFornecedorId;
    }

    public void setFornecedorByFornecedorId(Fornecedor
                                                    fornecedorByFornecedorIdFornecedor)
    {
        this.fornecedorByFornecedorId =
                fornecedorByFornecedorIdFornecedor;
    }

    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name = "subEquipe_idSubEquipe",
                referencedColumnName = "idSubEquipe",
                nullable = false)
    public SubEquipe getSubequipeBySubEquipeId()
    {
        return subequipeBySubEquipeId;
    }

    public void setSubequipeBySubEquipeId(SubEquipe
                                                  subequipeBySubEquipeIdSubEquipe)
    {
        this.subequipeBySubEquipeId = subequipeBySubEquipeIdSubEquipe;
    }

    @OneToMany(mappedBy = "modelopecaByModeloPecaIdPeca")
    public Collection<ModelosEmUso> getModelosemusosByIdPeca()
    {
        return modelosemusosByIdPeca;
    }

    public void setModelosemusosByIdPeca(Collection<ModelosEmUso>
                                                 modelosemusosByIdPeca)
    {
        this.modelosemusosByIdPeca = modelosemusosByIdPeca;
    }
}

我如何插入数据和更新:

  ModeloPeca modelo = new ModeloPeca();
        modelo.setIdPeca(idModelo);
        modelo.setQuantidade(restante);
  modeloPecaRepository.saveAndFlush(modelo);

PS:我正在使用Spring,Spring Data JPA和Hibernate提供程序进行JPA

谢谢你的时间!

2 个答案:

答案 0 :(得分:0)

属性armazemByArmazemId不能为空。

您正在创建新的ModeloPeca,但将此属性保留为null。

因此,Hibernate拒绝这一点。

您可以选择:

修改映射,使值可以为null:

@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "armazem_idarmazem",
            referencedColumnName = "idArmazem",
            nullable = true)
public Armazem getArmazemByArmazemId()
{
    return armazemByArmazemId;
}

或者,在致电modelo.setArmazemByArmazemId

之前,请先致电saveAndFlush

答案 1 :(得分:0)

您正在nullable = false进行验证,并在null时传递update值,因此not-null property references a null给出错误,只需将nullable = false更改为{在nullable = true列中{1}}。