DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Databases
  4. Hybrid Queries With JPA 2.1

Hybrid Queries With JPA 2.1

See a practical example of JPA named dynamic queries.

Abhishek Gupta user avatar by
Abhishek Gupta
CORE ·
May. 22, 16 · Tutorial
Like (6)
Save
Tweet
Share
9.32K Views

Join the DZone community and get the full member experience.

Join For Free

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

  • JPA 2.1 specification
  • JPA javadoc
Database

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • RabbitMQ vs. Memphis.dev
  • A Simple Union Between .NET Core and Python
  • Data Mesh vs. Data Fabric: A Tale of Two New Data Paradigms
  • Connecting Your Devs' Work to the Business

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: