Enterprise Java Development@TOPIC@

Chapter 69. JPA Query Overview

69.1. EntityManager Query Methods
69.2. Query.getSingleResult()
69.3. Query.getResultList
69.4. Query.getResultStream
69.5. Parameters
69.6. Paging Properties
69.7. Pessimistic Locking
69.8. Bulk Updates
69.9. Named Queries
69.10. Summary





Implement row filtering in SQL where clause when possible

The java.util.stream capability contains many features that perform filtering of results -- emulating the purpose of the SQL where clause. The database where clause is a much better place to implement filters. Avoid using streams in an attempt to override core RDBMS capability when the data is available within the database.


  • :firstName and :lastName act as placeholders for runtime query parameters

  • Runtime parameters supplied using placeholder names

  • A parameter for each placeholder must be supplied - no defaults

  • A placeholder must exist for each parameter supplied - no extras


  • Appended numbers (?1) assign an ordinal value

  • No numbers supplied (?) cause default value based on order


  • Dates are specified as DATE, TIME, or TIMESTAMP


  • Offset and limits passed to database

  • Database provides specified subset of rows


  • Provider adds database-specific technique for lock

  • Lock timeout (in msecs) can be expressed through query hint

Note

Not all databases support lock timeouts


  • Change directly applied to database, not the cached entity

  • Number of entities changed returned



  • Bulk deletes do not trigger cascades

  • Entity instance exists in memory even after deleted from database



  • Keeping stale entities around will produce confusing results

  • "em.clear()" should be avoided except at end of transaction since un-manages everything




  • Example query uses Native SQL to return all columns for table