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.
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
Thanks for the information
ReplyDeletesoftware development|Web development Company
This post is really nice and pretty well maintained, thanks for it and keep updating your posts.
ReplyDeleteWeb Designing course in palam
This comment has been removed by the author.
ReplyDeletethis is very nice.your this articles is very heplfull.thankyou for shareing.
ReplyDeleteprofessional-web-design-company
this is nice post ,i come to know about this so thankyou for shareing such a helpfull articles.
ReplyDeleteprofessional-web-design-company
Thank you for showing your thoughts by posting article. It is really nice one.
ReplyDeleteWeb design companies in Houston
Web design company Houston
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
ReplyDeleteYour article is one of its kind which explained every bit of Create Your Own Website. looking for further valuable articles from you
ReplyDeleteThis is just what I was looking for. Thank you for sharing this essential information. It is quite beneficial.Web Design Company USA
ReplyDeleteGreat post. Thank you for sharing
ReplyDeleteWeb Development Company In Kerala
Thank you so much for sharing this vital information with us. This is fantastic.
ReplyDeleteCustom Website Build
ReplyDeleteI adore your work, and it greatly motivates me.
Search Engine Marketing Services
Thank you for providing such an insightful perspective; the written content is rigorous, which is why I read it carefully.
ReplyDeleteOn Demand Developers