1

我正在使用 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 进行查询时犯了一个错误......

4

1 回答 1

1

这条线给出了问题:

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

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

Query query = entityManager.createQuery("FROM Person"); // This should fetch everything now, as expected!
于 2013-05-08T08:13:40.280 回答