Hello, how are you?
There are several JPAs “how to” that we can find on the internet, here in this blog, that teaches how to do several tasks with JPA.
Usually I see some people asking questions about Queries with JPA; usually to answer this kind of questions several links are provided trying to find a solution to the question.
Until today I could not find a blog post that puts together a good subject about queries with JPA, tips about performance/utilization, source code to download…
Today we will see:
- Page 2: Model classes and a class that will generate database data
- Page 3: Find method; Use the getReference method to get a better performance, displaying query parameters in the console with the log4j
- Page 4: JPQL: Queries with simple parameters or objects, Joins, Order By, Navigating through relationships
- Page 5: JPQL: Functions: AVG, COUNT, MAX, MIN, TRIM, SUM, UPPER, LOWER, MOD, LENGHT, SQRT; Using HAVING, GROUP BY
- Page 6: JPQL: Filtering Conditions: LIKE, IN, DISTINCT, EMPTY, BETWEEN, NULL, MEMBER OF, EXISTS (Subqueries), ANY, ALL, SOME, CONCAT, CURRENT_DATE, CURRENT_TIME, CURRENT_TIMESTAMP, LOCATE, SIZE, SUBSTRING
- Page 7: JPA: NamedQuery, querying with dates, warnings about the getSingleResult method
- Page 8: JPA: NativeQuery, NamedNativeQuery
- Page 9: JPA: Complex Native Queries
- Page 10: JPA: Optimizing queries with EJB
- Page 11: JPA: Pagination
- Page 12: JPA: Database Hints
- Page 13: JPA: Creating a object from a query
- Page 14: JPQL: Bulk Update and Delete
- Page 15: JPA: Criteria
You will see that in every main class we will invoke the method “CodeGenerator.generateData()”. This class method only creates data in the database; with this data our queries will find the correct results.
In the last page of this post you will find the link to download the source code of this post.
In this post we will use JPA 2.0 with Hibernate as the provider. The database will be the HSQLDB and will be attached to this project. You can download the source code and run the project without the need of any extra configuration. We will not talk about how to set up the HSQLDB because the focus of this post is how to query data of the database.
This post will not use best practices of development in some points. The focus of this post will be to display how the JPA queries works.