简体   繁体   中英

Update fails on Hibernate with OneToMany

Hey guys I'm trying to updata an entry in my Entity managed by Hibernate, it has 3 relationships ManyToOne non-Indentifying.

When I try to update giving the Id and the new value of quantidade column, throws an exception

Stacktrace:

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 :

@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;
    }
}

How I insert data and update:

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

PS: I'm using Spring, Spring Data JPA, and Hibernate provider for JPA

Thanks for your time!

The property armazemByArmazemId can not be null.

You are creating a new ModeloPeca , but leaving this property as null.

Hence, Hibernate is rejecting this.

You have a choice, either:

Modify the mapping so the value can be null:

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

OR, Actually call modelo.setArmazemByArmazemId before calling saveAndFlush

您将验证结果设为nullable = false ,并在update时传递了null值,因此,它给出的错误是not-null property references a null因此只需在armazem_idarmazemarmazem_idarmazem nullable = false更改为nullable = true

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM