What is Java Persistence Query Language (JPQL) and how to create queries using JPQL

What is Java Persistence Query Language (JPQL) and how to create queries using JPQL?

Java Persistence Query Language (JPQL):

Java Persistence Query Language (JPQL) is a platform-independent object-oriented query language defined in Java Persistence API (JPA). JPQL is a method of querying entities from data stored in JPA. JPQL defines queries for entities and their persistent state. JPQL allows writing portable queries that work regardless of the underlying database system.

JPQL is used to make queries against entities stored in a relational database. The query language uses the abstract persistence schemas of entities, including their relationships, for its data model and defines operators and expressions based on this data model. The scope of a query spans the abstract schemas of related entities that are packaged in the same persistence unit. The query language uses an SQL-like syntax to select objects or values based on entity abstract schema types and relationships among them.

Creating Queries Using the Java Persistence Query Language


There are two JPQL methods which are EntityManager.createQuery and EntityManager.createNamedQuery used to query the data stored in the database system.

1- JPQL createQuery:

The purpose of the createQuery method is to create dynamic queries directly within a Java application business logic. Below is an example of creating a Query method:

public List findWithName(String name) {
return em.createQuery(
    "SELECT c FROM Customer c WHERE c.name LIKE :custName")
    .setParameter("custName", name)
    .setMaxResults(10)
    .getResultList();
}

2- JPQL createNamedQuery:

The purpose of the createNamedQuery method is to create static queries which are defined in metadata by using javaX. persistence.NamedQuery annotation.
The @NamedQuery describes the name of the query which will be used with the createNamedQuery method.

The code pattern of the @NamedQuery query is mentioned below:
  
@NamedQuery(
    name="findAllCustomersWithName",
    query="SELECT c FROM Customer c WHERE c.name LIKE :custName"
)

Find below the createNamedQuery example where @NamedQuery is being used
 
@PersistenceContext
public EntityManager em;
...
customers = em.createNamedQuery("findAllCustomersWithName")
    .setParameter("custName", "Smith")
    .getResultList();

For more information click here

13 Comments

Please enter relevant questions and information.

  1. This post is really nice and pretty well maintained, thanks for it and keep updating your posts.

    Web Designing course in palam

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. this is very nice.your this articles is very heplfull.thankyou for shareing.
    professional-web-design-company

    ReplyDelete
  4. this is nice post ,i come to know about this so thankyou for shareing such a helpfull articles.
    professional-web-design-company

    ReplyDelete
  5. Thank you for showing your thoughts by posting article. It is really nice one.
    Web design companies in Houston
    Web design company Houston

    ReplyDelete
  6. I really like your blog.. very nice colors & theme. Did you create this website yourself or did you hire someone to do it for you? Plz reply as I'm looking to create my own blog and would like to know where u got this from. appreciate it. mac service berlin


    ReplyDelete
  7. Your article is one of its kind which explained every bit of Create Your Own Website. looking for further valuable articles from you

    ReplyDelete
  8. This is just what I was looking for. Thank you for sharing this essential information. It is quite beneficial.Web Design Company USA

    ReplyDelete
  9. Taurus Web SolutionsMay 22, 2022 at 10:44 PM

    Great post. Thank you for sharing
    Web Development Company In Kerala

    ReplyDelete
  10. Thank you so much for sharing this vital information with us. This is fantastic.
    Custom Website Build

    ReplyDelete
  11. Thank you for providing such an insightful perspective; the written content is rigorous, which is why I read it carefully.
    On Demand Developers

    ReplyDelete
Previous Post Next Post