2013-05-08 58 views
1

我正在開發使用JPA,Maven和Hibernate框架下Spring MVC的web應用程序,我有這個名單的方法:什麼是解決JPA春的這種奇怪的行爲的EntityManager

@Transactional 
    public List<Person> list() throws DatabaseException { 
     Person person = new Person("Andrea Patricelli", "[email protected]", "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 [email protected] 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 ...

+0

ECCO聯合國ELEMENTO內拉LISTA是我詳細模式是指元件,並不重要 – andPat 2013-05-08 08:10:58

回答

1

此行是給問題:

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

因爲你的實體類的名稱是Person,和你在查詢中給出了Persons,它的行爲很奇怪。

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

非常感謝你,我錯在創建數據庫表的人的這一翻譯對象人的! – andPat 2013-05-08 08:42:48

相關問題