I am unable to list the data in the Mysql database using Hibernate JPA

Asked

Viewed 71 times

0

**Classe: Estado**

package entidade;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Estado {
    public static final int size = 0;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;
    private String nome;
    private String sigla;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getSigla() {
        return sigla;
    }

    public void setSigla(String sigla) {
        this.sigla = sigla;
    }

}


**DAOEstado:** 

    public List<Estado> Buscar() {
        try {

            EntityManagerFactory fabrica = Fabrica.get();
            gerenciador = fabrica.createEntityManager();

            return gerenciador.createQuery("from estado").getResultList();

        } catch (Exception e) {
            e.printStackTrace();
            transacao.rollback();
        } finally {
            gerenciador.close();
        }
        return null;

**TelaEstado Código:**

    } else if (opcao == 2) {

                DAOEstado dao = new DAOEstado();

                System.out.println(" --- LISTAR TODOS ESTADOS ---");
                List<Estado> est = dao.Buscar();

                for (Estado estado : est) {
                    System.out.println("Id: "+estado.getId()+" Estado: "+estado.getNome()+" - "+estado.getSigla());
                }

Description of the Error:

java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: estado is not mapped [from estado]
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1750)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1677)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1683)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:331)
    at dao.DAOEstado.Buscar(DAOEstado.java:72)
    at tela.TelaEstado.<init>(TelaEstado.java:57)
    at principal.Principal.main(Principal.java:25)
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: estado is not mapped [from estado]
    at org.hibernate.hql.internal.ast.QuerySyntaxException.generateQueryException(QuerySyntaxException.java:96)
    at org.hibernate.QueryException.wrapWithQueryString(QueryException.java:120)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:234)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:158)
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:126)
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:88)
    at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:190)
    at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:301)
    at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236)
    at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1796)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:328)
    ... 3 more
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: estado is not mapped
    at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:189)
    at org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:109)
    at org.hibernate.hql.internal.ast.tree.FromClause.addFromElement(FromClause.java:95)
    at org.hibernate.hql.internal.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:331)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3633)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3522)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:706)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:562)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:299)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:247)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:278)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:206)
    ... 11 more
Exception in thread "main" java.lang.NullPointerException
    at dao.DAOEstado.Buscar(DAOEstado.java:76)
    at tela.TelaEstado.<init>(TelaEstado.java:57)
    at principal.Principal.main(Principal.java:25)
Show 2 more comments

1 answer

1


You should pack your Query.

Wrong query:

return gerenciador.createQuery("from estado").getResultList();

Correct query:

return gerenciador.createQuery("from Estado").getResultList();

In this case, it is case sensitive! :)

I hope I’ve helped.

Browser other questions tagged

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