Hybrid queries with JPA 2.1

Before JPA 2.1

There were two types of queries

  • Named (static): these are declared using the @NamedQuery or @NamedQueries annotations.
  • Dynamic: as the name suggests, this type of query is specified by the application logic at runtime

JPQL, Native SQL as well as Stored Procedure based queries support above mentioned modes and each one of them have their own pros and cons

JPA 2.1 supports

the notion of a hybrid style query wherein you can

  • declare a query in the code (as a regular string)
  • bind it to the EntityManagerFactory
  • use it as a regular Named Query

Predominant use case

Useful when a query has the following properties

  • It needs to be executed repeatedly (this one is obvious), and
  • Cannot be pre-determined (else @NamedQuery would have sufficed !) i.e. it depends on run time information


Although it incurs a (one time) processing overhead, after that, it offers the same benefits as a regular named query i.e. does not need to be processed by the JPA provider repeatedly. This is obviously not the case with dynamic JPQL queries which need to processed by the provider (converted to SQL) prior to execution

Further reading


About Abhishek

Currently working as a Senior Product Manager in the Oracle Cloud Application Development team with a focus on Oracle Cloud PaaS portfolio. When not hovering in the clouds, I stay grounded with Java EE
This entry was posted in Java EE and tagged , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s