Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Hybrid Queries With JPA 2.1

DZone's Guide to

Hybrid Queries With JPA 2.1

See a practical example of JPA named dynamic queries.

· Java Zone
Free Resource

Microservices! They are everywhere, or at least, the term is. When should you use a microservice architecture? What factors should be considered when making that decision? Do the benefits outweigh the costs? Why is everyone so excited about them, anyway?  Brought to you in partnership with IBM.

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 the above-mentioned modes and each one of them has 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
......
  private static final String FIND_ASSIGNEE = "SELECT a.assignee FROM Approvals a WHERE a.id = :reqId";

  @PersistenceContext(unitName="AccessRequestDataStore")
  private EntityManager em;

  public void test(String reqId) {

    //create once and bind
    Query query = em.createQuery(FIND_ASSIGNEE);
    em.getEntityManagerFactory().addNamedQuery("findAssignee", query);

    //use it
    String assignee = em.createNamedQuery("findAssignee")
    .setParameter("reqId", reqId)
    .getSingleResult();
  }
......

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

Advantages

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 be processed by the provider (converted to SQL) prior to execution

Further Reading

Discover how the Watson team is further developing SDKs in Java, Node.js, Python, iOS, and Android to access these services and make programming easy. Brought to you in partnership with IBM.

Topics:
benefits ,execution ,provider ,query ,dynamic ,jpa ,queries ,regular ,case ,sql

Published at DZone with permission of Abhishek Gupta, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}