Hibernate @ManyToMany抛出HHH000122:IllegalArgumentException

时间:2016-02-15 23:18:25

标签: hibernate jpa

我尝试构建一个Hibernate多对多关系,但是当我调用EntityManager方法" merge"时,我得到了这个例外。

任何人都知道如何解决它:

来自Hibernate:

Hibernate: select empresa0_.id as id1_1_0_, empresa0_.bairro as bairro2_1_0_, empresa0_.cep as cep3_1_0_, empresa0_.cidade as cidade4_1_0_, empresa0_.cnpj as cnpj5_1_0_, empresa0_.complemento as compleme6_1_0_, empresa0_.endereco as endereco7_1_0_, empresa0_.fantasia as fantasia8_1_0_, empresa0_.numero as numero9_1_0_, empresa0_.razaosoc as razaoso10_1_0_, empresa0_.telefone as telefon11_1_0_, empresa0_.uf as uf12_1_0_ from empresas empresa0_ where empresa0_.id=?
Hibernate: insert into funcionarios (cpf, empresa_id, nome, statusFuncionario) values (?, ?, ?, ?)
2016-02-15 21:10:11,909 ERROR [org.hibernate.property.BasicPropertyAccessor] HHH000122: IllegalArgumentException in class: com.ev.laab.model.Local, getter method of property: id

错误:

Caused by: javax.persistence.PersistenceException: org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of com.ev.laab.model.Local.id
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1387)
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1310)
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1316)
    at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:898)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.jboss.weld.bean.proxy.AbstractBeanInstance.invoke(AbstractBeanInstance.java:45)
    at org.jboss.weld.bean.proxy.ProxyMethodHandler.invoke(ProxyMethodHandler.java:105)
    at org.jboss.weld.proxies.EntityManager$-1204917128$Proxy$_$$_WeldClientProxy.merge(EntityManager$-1204917128$Proxy$_$$_WeldClientProxy.java)
    at com.ev.laab.repository.Funcionarios.guardar(Funcionarios.java:26)
    at com.ev.laab.repository.Funcionarios$Proxy$_$$_WeldSubclass.guardar(Funcionarios$Proxy$_$$_WeldSubclass.java)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.jboss.weld.interceptor.proxy.SimpleInterceptionChain.invokeNextInterceptor(SimpleInterceptionChain.java:85)
    at org.jboss.weld.interceptor.proxy.InterceptorMethodHandler.executeInterception(InterceptorMethodHandler.java:112)
    at org.jboss.weld.interceptor.proxy.InterceptorMethodHandler.invoke(InterceptorMethodHandler.java:88)
    at org.jboss.weld.bean.proxy.CombinedInterceptorAndDecoratorStackMethodHandler.invoke(CombinedInterceptorAndDecoratorStackMethodHandler.java:55)
    at com.ev.laab.repository.Funcionarios$Proxy$_$$_WeldSubclass.guardar(Funcionarios$Proxy$_$$_WeldSubclass.java)
    at com.ev.laab.service.FuncionarioService.salvar(FuncionarioService.java:26)
    at com.ev.laab.service.FuncionarioService$Proxy$_$$_WeldSubclass.salvar(FuncionarioService$Proxy$_$$_WeldSubclass.java)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.jboss.weld.interceptor.proxy.SimpleInterceptionChain.invokeNextInterceptor(SimpleInterceptionChain.java:85)
    at org.jboss.weld.interceptor.proxy.InterceptorInvocationContext.proceed(InterceptorInvocationContext.java:127)
    at com.ev.laab.util.jpa.TransactionInterceptor.invoke(TransactionInterceptor.java:38)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.jboss.weld.interceptor.proxy.SimpleMethodInvocation.invoke(SimpleMethodInvocation.java:30)
    at org.jboss.weld.interceptor.proxy.SimpleInterceptionChain.invokeNextInterceptor(SimpleInterceptionChain.java:69)
    at org.jboss.weld.interceptor.proxy.InterceptorMethodHandler.executeInterception(InterceptorMethodHandler.java:112)
    at org.jboss.weld.interceptor.proxy.InterceptorMethodHandler.invoke(InterceptorMethodHandler.java:88)
    at org.jboss.weld.bean.proxy.CombinedInterceptorAndDecoratorStackMethodHandler.invoke(CombinedInterceptorAndDecoratorStackMethodHandler.java:55)
    at com.ev.laab.service.FuncionarioService$Proxy$_$$_WeldSubclass.salvar(FuncionarioService$Proxy$_$$_WeldSubclass.java)
    at com.ev.laab.controller.FormFuncionarioBean.salvar(FormFuncionarioBean.java:57)
    at com.ev.laab.controller.FormFuncionarioBean$Proxy$_$$_WeldClientProxy.salvar(FormFuncionarioBean$Proxy$_$$_WeldClientProxy.java)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.apache.el.parser.AstValue.invoke(AstValue.java:247)
    at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:267)
    at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:39)
    at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
    ... 29 more
Caused by: org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of com.ev.laab.model.Local.id
    at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:187)
    at org.hibernate.tuple.entity.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:344)
    at org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:4537)
    at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:129)
    at org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:924)
    at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:906)
    at org.hibernate.engine.spi.CascadingAction$6.cascade(CascadingAction.java:288)
    at org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:388)
    at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:331)
    at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:209)
    at org.hibernate.engine.internal.Cascade.cascadeCollectionElements(Cascade.java:418)
    at org.hibernate.engine.internal.Cascade.cascadeCollection(Cascade.java:358)
    at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:334)
    at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:209)
    at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:166)
    at org.hibernate.event.internal.AbstractSaveEventListener.cascadeAfterSave(AbstractSaveEventListener.java:449)
    at org.hibernate.event.internal.DefaultMergeEventListener.entityIsTransient(DefaultMergeEventListener.java:220)
    at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:154)
    at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:76)
    at org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:914)
    at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:898)
    at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:902)
    at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:889)
    ... 78 more
Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:164)
    ... 100 more

第一实体:

package com.ev.laab.model;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;

import org.hibernate.validator.constraints.NotBlank;
import org.hibernate.validator.constraints.br.CPF;

@Entity
@Table(name = "funcionarios")
public class Funcionario implements Serializable {

    private static final long serialVersionUID = 1L;

    private Long id;
    private Empresa empresa;
    private String cpf;
    private String nome;
    private StatusFuncionario statusFuncionario;
    private Set<Local> locais = new HashSet<Local>();

    @Id
    @GeneratedValue
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }

    @NotNull
    @ManyToOne
    @JoinColumn(name = "empresa_id", nullable = false)
    public Empresa getEmpresa() {
        return empresa;
    }
    public void setEmpresa(Empresa empresa) {
        this.empresa = empresa;
    }

    @CPF
    @NotBlank
    @Column(unique = true, nullable = false, length = 11)
    public String getCpf() {
        return cpf;
    }
    public void setCpf(String cpf) {
        this.cpf = cpf;
    }

    @NotBlank
    @Column(nullable = false, length = 100)
    public String getNome() {
        return nome;
    }
    public void setNome(String nome) {
        this.nome = nome;
    }

    @NotNull
    @Enumerated(EnumType.STRING)
    @Column(nullable = false, length = 15)
    public StatusFuncionario getStatusFuncionario() {
        return statusFuncionario;
    }

    public void setStatusFuncionario(StatusFuncionario statusFuncionario) {
        this.statusFuncionario = statusFuncionario;
    }

    @ManyToMany(cascade = {CascadeType.ALL})
    @JoinTable(name="funcionario_local", 
                joinColumns={@JoinColumn(name="funcionario_id")}, 
                inverseJoinColumns={@JoinColumn(name="Local_id")})
    public Set<Local> getLocais() {
        return locais;
    }

    public void setLocais(Set<Local> locais) {
        this.locais = locais;
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Funcionario other = (Funcionario) obj;
        if (id == null) {
            if (other.id != null)
                return false;
        } else if (!id.equals(other.id))
            return false;
        return true;
    }
}

第二实体:

package com.ev.laab.model;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;

import org.hibernate.validator.constraints.NotEmpty;

@Entity
@Table(name = "locais")
public class Local implements Serializable {

    private static final long serialVersionUID = 1L;

    private Long id;
    private Empresa empresa;
    private String local;
    private Set<Funcionario> funcionarios = new HashSet<Funcionario>();

    @Id
    @GeneratedValue
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }

    @NotNull
    @ManyToOne
    @JoinColumn(name = "empresa_id", nullable = false)
    public Empresa getEmpresa() {
        return empresa;
    }
    public void setEmpresa(Empresa empresa) {
        this.empresa = empresa;
    }

    @NotEmpty
    @Column(nullable = false, length = 100)
    public String getLocal() {
        return local;
    }
    public void setLocal(String local) {
        this.local = local;
    }

    @ManyToMany(mappedBy="locais")
    public Set<Funcionario> getFuncionarios() {
        return funcionarios;
    }
    public void setFuncionarios(Set<Funcionario> funcionarios) {
        this.funcionarios = funcionarios;
    }
}

存储库:

package com.ev.laab.repository;

import java.io.Serializable;
import java.util.List;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceException;

import com.ev.laab.model.Funcionario;
import com.ev.laab.util.jpa.Transactional;
import com.ev.laab.util.jsf.FacesUtil;

public class Funcionarios implements Serializable {

    private static final long serialVersionUID = 1L;

    @Inject
    EntityManager manager;

    public Funcionario guardar(Funcionario funcionario) {

        return manager.merge(funcionario);
    }

    @Transactional
    public void remover(Funcionario funcionario) {
        try {
            //Recarrega o usuário usando método do manager, para estar conectado ao BD.
            funcionario = porCPF(funcionario.getCpf());
            manager.remove(funcionario);
            manager.flush();
        } catch (PersistenceException e) {
            FacesUtil.addErrorMessage("Erro ao tentar remover o funcionário.");
        }
    }

    public Funcionario porId(Long id) {
        return manager.find(Funcionario.class, id);
    }

    public Funcionario porCPF(String cpf) {
        Funcionario funcionario = null;
        try {
            funcionario = (Funcionario) manager.createQuery("from Funcionario where cpf = :cpf")
                .setParameter("cpf", cpf)
                .getSingleResult();
        } catch (NoResultException e) {
        }
        return funcionario;
    }
    @SuppressWarnings("unchecked")
    public List<Funcionario> todos() {
        return manager.createQuery("from Funcionario").getResultList();
    }
}

0 个答案:

没有答案