Ultimate JPA Queries and Tips List - Part 1

Discussions

News: Ultimate JPA Queries and Tips List - Part 1

  1. 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: 

    • Model classes and a class that will generate database data
    • Find method; Use the getReference method to get a better performance, displaying query parameters in the console with the log4j
    • JPQL: Queries with simple parameters or objects, Joins, Order By, Navigating through relationships
    • JPQL: Functions: AVG, COUNT, MAX, MIN, TRIM, SUM, UPPER, LOWER, MOD, LENGHT, SQRT; Using HAVING, GROUP BY
    • 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
    • JPA: NamedQuery, querying with dates, warnings about the getSingleResult method
    • JPA: NativeQuery, NamedNativeQuery
    • JPA: Complex Native Queries
    • JPA: Optimizing queries with EJB
    • JPA: Pagination
    • JPA: Database Hints
    • JPA: Creating a object from a query
    • JPQL: Bulk Update and Delete
    • 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.

    Read the rest of the article at the following URL:

    Java Code Geeks : Ultimate JPA Queries and Tips List - Part 1

    Threaded Messages (8)

  2. Plagiarism from JCG[ Go to top ]

    In fact this is taken from this blog, with loads of ads placed around it to make the page load slower

    TSS, I thought you'd got past posting this JCG regurgitation

  3. Hello[ Go to top ]

    I am just curious about which ads you saw at the blog that make it slow to load?

    I am the owner there and I want to make it better. [=

     

    Thanks

  4. He surely meant copy on JCG[ Go to top ]

    Your blog is fine, JCG is full of ads.

  5. Hello[ Go to top ]

    Wow, thank you very much.

    I misunderstood what you said.

     

    C ya! [=

  6. ads[ Go to top ]

    Yes, exactly as Richard said. Your blog is fine, and you could easily publicise it directly here without permitting JCG to bastardise it and put ads around it, since that debases your good work. There are ample places (TSS, JavaZone for example) to get your ideas linked without going that being a JCG prostitute

  7. Enough is Enough[ Go to top ]

    Hello all and thank your for your comments,

    I would just like to present the Java Code Geeks point of view since they are consistently bashed here in TSS. Java Code Geeks is a 2 years old Java developer community that is committed to share their knowledge and experiences so as to help each other.

    We (the Java Code Geeks community founders and editorial team) are Java developers ourselves and have contributed first (and still contributing) with high quality articles, open source projectsa tutorials pagea Java Snippets sub-sitea Java resources collection and much more. That is the reason that we do not want to simply maintain a link aggregator site but a site that will host the most valuable content for the Java developers community.

    By submitting the link pointing to the reposted article (hosted on Java Code Geeks) and not the one pointing to the original author's blog we promote not only the specific author's work but the work of all the Java Code Geeks community; since our site is the entry point for them all. That is our obligation towards our partners.

    As far as content duplication is concerned I just wanted to pinpoint that in the majority of cases we do not just copy and paste content; We perform lexical and syntactical corrections (since many of our JCG partners do not speak English as their prime language) along with article and code snippets formatting so as every article to be as readable as it gets. Furthermore, this is a quite established practice (see DZone etc.)

    As far as advertisement on Java Code Geeks is concerned let me remind you that all major community driven sites host advertisement services, including TSSDZoneInfoQJDJJavaRanch just to mention a few. This specific approach is totally ethical since it is a mean for our community members to express their gratitude for all of our efforts to promote Java and help the community altogether.

    Finally, I believe that comments like "being a JCG prostitute" are not representative of people belonging to the Java developers community. Furthermore, it is degrading to the 250+ people that have joined our efforts and currently participate in the JCG program.

    Closing, I would like to ask from all the people bashing Java Code Geeks to show us what have THEY done to promote Java and help the community.

  8. Not so Ultimate[ Go to top ]

    Some remarks from my code review:

    1. An EntityManager should not be static. EntityManager isn't thread-safe and static makes no sense in the context of JEE/SessionBean.
    2. Many queries look like:

    Query query = em.createQuery('select p from Person p where name = :name', Person.class);
    query.setParameter('name', name);
    return (Person) query.getSingleResult();

    instead it should be:

    TypedQuery<Person> query = em.createQuery("select p from Person p where name = :name", Person.class);
    query.setParameter("name", name);
    return query.getSingleResult();

    The author seems to be unaware of TypedQuery.  Why does he even pass the second argument ('class')? Instead he uses unnecessary casts and @SuppressWarnings('unchecked')! 

     

  9. Not so Ultimate[ Go to top ]

    Hello,

    I am the writer of that post, thanks for your reply.

    Indeed I could change the @SuppressWarnings(‘unchecked’) with the TypedQuery interface.

    When I wrote this tutorial I did not had any knowledge about JPA 2.0.

    Two weeks after I wrote the post I was reading the Pro JPA 2 book and found out about the TypedQuery.

    Thanks for the observation.

    It will help others that pass by here.

    [=