【问题标题】:What is the solution to this strange behavior of JPA Spring EntityManagerJPA Spring EntityManager 这种奇怪行为的解决方案是什么
【发布时间】:2013-05-08 08:10:29
【问题描述】:

我正在使用 JPA、Maven 和 Hibernate 框架下的 Spring MVC 开发一个 webapp,我有这个列表方法:

@Transactional
    public List<Person> list() throws DatabaseException {
        Person person = new Person("Andrea Patricelli", "a@live.it", "3204524292");
        entityManager.persist(person);
        Person p = entityManager.find(Person.class, "Andrea Patricelli");
        System.out.println(" FOUND: -------->" + p.getName() + " " + p.getEmail() + " " + p.
                getCellPhoneNumber());
        Query query = entityManager.createQuery("FROM Persons");
        List<Person> resultList = query.getResultList();
        System.out.println("ECCO UN ELEMENTO DELLA LISTA: ------->" + resultList.iterator().next().getName());
        for (Person next : resultList) {
            System.out.println("next person: " + next);
        }
        return resultList;
    }

当我运行我的示例时,我得到了这个堆栈跟踪:

[INFO] [talledLocalContainer] FOUND: -------->Andrea Patricelli andreapatricelli@gmail.com 0854312119
[INFO] [talledLocalContainer] 08 mag 2013;10:03:10 DEBUG o.h.h.i.ast.QueryTranslatorImpl - parse() - HQL: FROM Persons
[INFO] [talledLocalContainer] 08 mag 2013;10:03:10 DEBUG o.h.h.i.ast.QueryTranslatorImpl - --- HQL AST ---
[INFO] [talledLocalContainer]  \-[QUERY] Node: 'query'
[INFO] [talledLocalContainer]     \-[SELECT_FROM] Node: 'SELECT_FROM'
[INFO] [talledLocalContainer]        \-[FROM] Node: 'FROM'
[INFO] [talledLocalContainer]           \-[RANGE] Node: 'RANGE'
[INFO] [talledLocalContainer]              \-[IDENT] Node: 'Persons'
[INFO] [talledLocalContainer] 
[INFO] [talledLocalContainer] 08 mag 2013;10:03:10 DEBUG o.h.hql.internal.ast.ErrorCounter - throwQueryException() : no errors
[INFO] [talledLocalContainer] 08 mag 2013;10:03:11 DEBUG o.h.h.i.antlr.HqlSqlBaseWalker - select << begin [level=1, statement=select]
[INFO] [talledLocalContainer] 08 mag 2013;10:03:11 DEBUG o.h.e.t.spi.AbstractTransactionImpl - rolling back
[INFO] [talledLocalContainer] 08 mag 2013;10:03:11 DEBUG o.h.e.t.i.jdbc.JdbcTransaction - rolled JDBC Connection
[INFO] [talledLocalContainer] 08 mag 2013;10:03:11 DEBUG o.h.e.t.i.jdbc.JdbcTransaction - re-enabling autocommit
[INFO] [talledLocalContainer] 08 mag 2013;10:03:11 DEBUG o.h.e.j.i.LogicalConnectionImpl - Releasing JDBC connection
[INFO] [talledLocalContainer] 08 mag 2013;10:03:11 DEBUG o.h.e.j.i.LogicalConnectionImpl - Released JDBC connection

我希望这段代码可以打印我数据库中所有人员的列表(3 人),看起来查询格式不正确并且有错误,但我没有错误!或者另一方面,我在理解如何使用 Hibernate 进行查询时犯了一个错误......

【问题讨论】:

  • ECCO UN ELEMENTO NELLA LISTA 是我引用元素的详细模式,并不重要

标签: java spring hibernate jpa maven-3


【解决方案1】:

这行有问题:

Query query = entityManager.createQuery("FROM Persons");

由于您的实体类的名称是Person,并且您在查询中给出了Persons,所以它的行为很奇怪。

Query query = entityManager.createQuery("FROM Person"); // This should fetch everything now, as expected!

【讨论】:

  • 非常感谢,我在创建数据库表 Persons 而不是 Object Person 时出错了!
猜你喜欢
  • 1970-01-01
  • 2013-05-01
  • 2012-01-05
  • 1970-01-01
  • 1970-01-01
  • 2011-09-30
  • 1970-01-01
  • 1970-01-01
  • 2017-03-30
相关资源
最近更新 更多