Giter Club home page Giter Club logo

jpa-criteria-api's Introduction

Build Status

jpa-criteria-api

The main goal of this project is to explore basic query features of JPA 2.1 specification:

  • JPQL
  • JPA Criteria API (typesafe with auto-generated static metamodels)

As a JPA provider we choose Hibernate to test some of HQL.

project structure

technologies used

project description

  • The main idea is to explore JPA Criteria API by writing a querys in JPQL or HQL, then to try re-write them in JPA Criteria API & check result-sets identity.
  • Comparing equality of result sets with JUnit & hamcrest:
    import static org.hamcrest.Matchers.containsInAnyOrder;
    ...
    Assert.assertThat(entityManager.createQuery(cc_query)
                .getResultList(),
        containsInAnyOrder(jpql_query.getResultList().toArray()));
    
    but we decide to use Jupiter & AssertJ (more expressive):
    import static org.assertj.core.api.Assertions.assertThat;
    ...
    assertThat(entityManager.createQuery(cc_query).getResultList())
        .containsExactlyInAnyOrderElementsOf(jpql_query.getResultList());
    

project content

src/main/java: entities, utility classes & META-INF folder with persistence.xml
resources/db/migration: Flyway migrations as SQL scripts
test/java: showcase of JPQL, HQL & Criteria API with tests
target/generated-sources: static metamodels of entities

tests

  • We have to classes with nearly the same content: Tests and TestsWithFullTypeSafe. The only difference is that in the first class we use strings to denote fields while in the letter we use static metamodels.

  • Example:

    • From Tests:
      orderBy(cb.asc(cc_query_root.get("title")));
      
    • From TestsWithFullTypeSafe:
      orderBy(cb.asc(cc_query_root.get(Book_.title)));
      
  • All methods are quite simple & straightforward use of Criteria API.
    The most interesting are:

    • getBookstoresWithMostExpensiveBook() - we show how to use subqueries
    • getBookstoresThatHaveTitle() - we show how to reference a FROM expression of the parent query in the FROM clause of a subquery
    • getBookstoresThatHaveAtLeastOneBookWrittenBy() - we show how to reference a JOIN expression of the parent query in the FROM clause of a subquery
    • countBooksByGenre() - we show how to use Tuple with aliases
    • getBookstoresWithCountBooksAndPriceAverage() - we show how to use multiselect with dedicated class

static metamodels

They are auto-generated by maven-compiler-plugin.
All you need to do is:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
        <compilerArguments>
            <processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor>
        </compilerArguments>
    </configuration>
</plugin>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-jpamodelgen</artifactId>
    <version>5.2.17.Final</version>
    <scope>provided</scope>
</dependency>

Mark target/generated-sources as a Generated Sources Root.
In IntelliJ just left-click on target/generated-sources -> Mark Directory As -> Generated Sources Root.

jpa-criteria-api's People

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.