Skip to main content
1 de 2
funke
  • 307
  • 5
  • 12

Obter lista de objetos com JPA e Hibernate

Boa tarde pessoal. Sou novo no hibernate e gostaria de uma ajuda. Em meu código o método de inserir o objeto no banco está funcionando, no entanto ao tentar obeter uma lista dos objetos armazenados no banco, recebo o erro que minha tabela não está mapeada. Abaixo encontra-se as classes o xml e o log de erros. Fico no aguardo.

#########Pessoa.java @Entity @Table(name="pessoas") public class Pessoa { private Long id; private Integer idade; public static String NAME = "name"; protected SimpleStringProperty name = new SimpleStringProperty(""); @Id @GeneratedValue @Column(name="pessoas_id") public Long getId() { return id; } public void setId(Long id) { this.id = id; } @Column(name="pessoas_nome") public String getNome() { return name.get(); } public void setNome(String nome) { this.name.set(nome); } @Column(name="pessoas_idade") public Integer getIdade() { return idade; } public void setIdade(Integer idade) { this.idade = idade; } } ##########PessoaDao.java public class PessoaDAO { private EntityManager manager; private EntityManagerFactory factory; public PessoaDAO() { factory = Persistence.createEntityManagerFactory("wehave"); manager = factory.createEntityManager(); } public void insert(Pessoa pessoa) { try { manager.persist(pessoa); manager.getTransaction().begin(); manager.getTransaction().commit(); } catch (Exception ex) { ex.printStackTrace(); manager.getTransaction().rollback(); } } public void remove(Pessoa pessoa) { try { manager.getTransaction().begin(); manager.remove(pessoa); manager.getTransaction().commit(); } catch (Exception ex) { ex.printStackTrace(); manager.getTransaction().rollback(); } } public List getList() { Query query = manager.createQuery("SELECT e FROM pessoas e"); List pessoas = query.getResultList(); return pessoas; } } ##############Teste.java public class Teste { public static void main(String[] args) { Pessoa p = new Pessoa(); p.setNome("Gabriel"); p.setIdade(19); Pessoa p2 = new Pessoa(); p2.setNome("José"); p2.setIdade(72); Pessoa p3 = new Pessoa(); p3.setNome("Maria"); p3.setIdade(27); Pessoa p4 = new Pessoa(); p4.setNome("Rodolfo"); p4.setIdade(16); PessoaDAO dao = new PessoaDAO(); dao.insert(p4); dao.insert(p3); dao.insert(p2); dao.insert(p); List pessoas = dao.getList(); for (int a=0; a ########## pesistence.xml org.hibernate.ejb.HibernatePersistence model.Pessoa set 04, 2014 6:50:31 PM org.hibernate.ejb.HibernatePersistence logDeprecation WARN: HHH015016: Encountered a deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead. set 04, 2014 6:50:31 PM org.hibernate.ejb.HibernatePersistence logDeprecation WARN: HHH015016: Encountered a deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead. set 04, 2014 6:50:31 PM org.hibernate.ejb.HibernatePersistence logDeprecation WARN: HHH015016: Encountered a deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead. set 04, 2014 6:50:31 PM org.hibernate.jpa.internal.util.LogHelper logPersistenceUnitInformation INFO: HHH000204: Processing PersistenceUnitInfo [ name: wehave ...] set 04, 2014 6:50:31 PM org.hibernate.Version logVersion INFO: HHH000412: Hibernate Core {4.3.6.Final} set 04, 2014 6:50:31 PM org.hibernate.cfg.Environment INFO: HHH000206: hibernate.properties not found set 04, 2014 6:50:31 PM org.hibernate.cfg.Environment buildBytecodeProvider INFO: HHH000021: Bytecode provider name : javassist set 04, 2014 6:50:32 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager INFO: HCANN000001: Hibernate Commons Annotations {4.0.5.Final} set 04, 2014 6:50:32 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!) set 04, 2014 6:50:32 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost/wehavescience] set 04, 2014 6:50:32 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH000046: Connection properties: {user=root, password=****} set 04, 2014 6:50:32 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH000006: Autocommit mode: false set 04, 2014 6:50:32 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure INFO: HHH000115: Hibernate connection pool size: 20 (min=1) set 04, 2014 6:50:33 PM org.hibernate.dialect.Dialect INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect set 04, 2014 6:50:33 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory INFO: HHH000397: Using ASTQueryTranslatorFactory set 04, 2014 6:50:34 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute INFO: HHH000228: Running hbm2ddl schema update set 04, 2014 6:50:34 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute INFO: HHH000102: Fetching database metadata set 04, 2014 6:50:34 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute INFO: HHH000396: Updating schema set 04, 2014 6:50:34 PM org.hibernate.tool.hbm2ddl.TableMetadata INFO: HHH000261: Table found: wehavescience.pessoas set 04, 2014 6:50:34 PM org.hibernate.tool.hbm2ddl.TableMetadata INFO: HHH000037: Columns: [pessoas_idade, pessoas_id, pessoas_nome] set 04, 2014 6:50:34 PM org.hibernate.tool.hbm2ddl.TableMetadata INFO: HHH000108: Foreign keys: [] set 04, 2014 6:50:34 PM org.hibernate.tool.hbm2ddl.TableMetadata INFO: HHH000126: Indexes: [primary] set 04, 2014 6:50:34 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute INFO: HHH000232: Schema update complete Hibernate: insert into pessoas (pessoas_idade, pessoas_nome) values (?, ?) Hibernate: insert into pessoas (pessoas_idade, pessoas_nome) values (?, ?) Hibernate: insert into pessoas (pessoas_idade, pessoas_nome) values (?, ?) Hibernate: insert into pessoas (pessoas_idade, pessoas_nome) values (?, ?) Exception in thread "main" java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: pessoas is not mapped [from pessoas] 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 controller.PessoaDAO.getList(PessoaDAO.java:48) at app.Teste.main(Teste.java:38) Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: pessoas is not mapped [from pessoas] 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.(HQLQueryPlan.java:126) at org.hibernate.engine.query.spi.HQLQueryPlan.(HQLQueryPlan.java:88) at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:167) 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:1800) at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:328) ... 2 more Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: pessoas 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:332) at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3678) at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3567) at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:708) at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:564) at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:301) at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:249) at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:278) at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:206) ... 10 more
funke
  • 307
  • 5
  • 12