andPat
andPat

Reputation: 4503

What is the solution to this strange behavior of JPA Spring EntityManager

I'm developing a webapp with JPA, Spring MVC under Maven and Hibernate Framework, I have this list method:

@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;
    }

when I run my example I got this stack trace:

[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

and I would expect this code to print the list of all Persons in my database (3 persons), it seems like the query is not formatted well and there is an error, but I got NO errors! or on the other hand I'm doing a mistake in understanding how to make queries with Hibernate...

Upvotes: 1

Views: 298

Answers (1)

Rahul
Rahul

Reputation: 45060

This line is giving problems:

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

Since the name of your entity class is Person, and you've given Persons in the query, its behaving oddly.

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

Upvotes: 1

Related Questions