After @Joincolumn JPA does not find Entity attribute

Asked

Viewed 150 times

0

I have two entities that are directly related to @Entity State and @Entity City And my intention was to rename the foreign key present in the customer identity, for example, from 'city.stade_state' to 'city.idState'. For this I used the Annotation @Joincolumn ,however, it seems that after this change Hibernate looks for an attribute with the name it assigns to the NAME property of the Annotation in question, however, in the entity State I already specified the attribute 'state' as value of mappedBy.

@Entity State

@Entity
public class Estado {

    @Id
    private int idEstado;
    @Column(length=2)
    private String sigla;
    @OneToMany(mappedBy="estado",targetEntity=Cidade.class,cascade=CascadeType.ALL)
    private List<Cidade> cidades;
    public int getIdEstado() {
        return idEstado;
    }
    public void setIdEstado(int idEstado) {
        this.idEstado = idEstado;
    }
    public String getSigla() {
        return sigla;
    }
    public void setSigla(String sigla) {
        this.sigla = sigla;
    }
    public List<Cidade> getCidades() {
        return cidades;
    }
    public void setCidades(List<Cidade> cidades) {
        this.cidades = cidades;
    }
}

@Entity City

@Entity
public class Cidade {
    @Id 
    private int idCidade;
    @Column(length=30)
    private String nomeCidade;
    @ManyToOne
    @JoinColumn(name="idEstado")
    private Estado estado;
    public int getIdCidade() {
        return idCidade;
    }
    public void setIdCidade(int idCidade) {
        this.idCidade = idCidade;
    }
    public String getNomeCidade() {
        return nomeCidade;
    }
    public void setNomeCidade(String nomeCidade) {
        this.nomeCidade = nomeCidade;
    }
    public Estado getEstado() {
        return estado;
    }
    public void setEstado(Estado estado) {
        this.estado = estado;
    }
    public Estado getIdEstado() {
        return estado;
    }
    public void setIdEstado(Estado estado) {
        this.estado = estado;
    }   
}

Consultation

TypedQuery<Cidade> queryCidades = em.createQuery("select c from Cidade c where c.estado =:codigoEstado",Cidade.class);
queryCidades.setParameter("codigoEstado", 1);
List<Cidade> cidades = queryCidades.getResultList();

Error

Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [private int br.com.biblioteca.model.Estado.idEstado] by reflection for persistent property [br.com.biblioteca.model.Estado#idEstado] : 1
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:147)
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:155)
    at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1407)
    at org.hibernate.Query.getResultList(Query.java:427)
    at br.com.biblioteca.model.Principal.main(Principal.java:26)
Caused by: org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [private int br.com.biblioteca.model.Estado.idEstado] by reflection for persistent property [br.com.biblioteca.model.Estado#idEstado] : 1
    at org.hibernate.property.access.spi.GetterFieldImpl.get(GetterFieldImpl.java:71)
    at org.hibernate.tuple.entity.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:224)
    at org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:4662)
    at org.hibernate.persister.entity.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:4373)
    at org.hibernate.engine.internal.ForeignKeys.isTransient(ForeignKeys.java:226)
    at org.hibernate.engine.internal.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:276)
    at org.hibernate.type.EntityType.getIdentifier(EntityType.java:462)
    at org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:161)
    at org.hibernate.param.NamedParameterSpecification.bind(NamedParameterSpecification.java:53)
    at org.hibernate.loader.hql.QueryLoader.bindParameterValues(QueryLoader.java:628)
    at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1956)
    at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1909)
    at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1887)
    at org.hibernate.loader.Loader.doQuery(Loader.java:932)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:349)
    at org.hibernate.loader.Loader.doList(Loader.java:2615)
    at org.hibernate.loader.Loader.doList(Loader.java:2598)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2430)
    at org.hibernate.loader.Loader.list(Loader.java:2425)
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:502)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:371)
    at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:216)
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1473)
    at org.hibernate.query.internal.AbstractProducedQuery.doList(AbstractProducedQuery.java:1426)
    at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1398)
    ... 2 more
Caused by: java.lang.IllegalArgumentException: Can not set int field br.com.biblioteca.model.Estado.idEstado to java.lang.Integer
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
    at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:58)
    at sun.reflect.UnsafeIntegerFieldAccessorImpl.getInt(UnsafeIntegerFieldAccessorImpl.java:56)
    at java.lang.reflect.Field.getInt(Field.java:574)
    at org.hibernate.property.access.spi.GetterFieldImpl.get(GetterFieldImpl.java:58)
    ... 26 more
  • 1

    Can I propose to test using Integer in the attributes idState and idCity? Not as the same answer, but test. This is what stack trace claims as root error

  • I’ll try as soon as I get home.

  • 1

    It worked! Put as answer.

2 answers

1


I was struck by the root cause of the problems:

Caused by: java.lang.IllegalArgumentException: Can not set int field br.com.biblioteca.model.Estado.idEstado to java.lang.Integer

I then suggested to swap the fields declared as primitivo int for Integer, in the hope that the framework will not be lost. In this case, the attributes idEstado and idCidade.

The end result was positive, having solved the current question.

0

Opa, you are using the Typedquery query, the expected in this parameter passage scenario is you use the city state object itself, which Hibernate will solve. Staying:

TypedQuery<Cidade> queryCidades = em.createQuery("select c from Cidade c where c.estado =:estado",Cidade.class);
queryCidades.setParameter("estado", objetoEstado);
List<Cidade> cidades = queryCidades.getResultList();

hope it works.

Att

Browser other questions tagged

You are not signed in. Login or sign up in order to post.