1 Reply Latest reply on Jul 23, 2006 1:07 AM by cbiswa

    Ex. 9.2 NativeQueries.java is a bit misleading

    cbiswa

      This example, as well as the related section of the book (pages 202 - 204) are a bit misleading. They seem to suggest that it is okay to select only a few properties of an entity - so long as we use @FieldResult to specify those properties in @SqlResultSetMapping/@EntityClass.

      However that does not seem to work in practice. The code in this example (Ex. 9.2 NativeQueries.java) works because the database initialization as well as the queries are done in the same transaction. If however, the entityManager is cleared before starting the queries, the queries fail with "java.sql.SQLException: Column not found: hasGoodCredit".

      To see, make the following changes to main:

      1. after the call to "System.out.println("Initialize DB");", add the lines:
      entityManager.getTransaction().commit();
      entityManager.clear();
      entityManager.getTransaction().begin();

      2. in the finally section, change the "commit" call to
      if (!entityManager.getTransaction().getRollbackOnly())
      entityManager.getTransaction().commit();


      Now because we clear the entityManager before doing the queries,you should get error (atleast I am).


      Please clarify - Thanks.