Getting Started (JPA)

The full Jinq distribution contains a sample project that demonstrates how to use Jinq. Download it and unpack it somewhere. Jinq requires Java 8. Be sure you have the latest Java 8 JDK from Oracle installed. The Jinq download contains a directory called sample with the sample project. The sample code for this program is in the sample/src directory.

Compiling the Sample Program

You must compile the sample code before running it. You can compile it using Maven, Ant, or manually.

Compile Using Maven

There is a Maven pom.xml in the sample directory. You can compile and execute the sample program using

mvn package exec:java

Compile Using Ant

There is an Ant build.xml build file in the sample directory. You can compile and execute the sample program using

ant

Compile Manually

You can compile the sample program from the command-line or using an IDE. Create a project, and include all the source code in the sample/src directory. Ensure that the .jar files in the sample/lib directory are in your classpath. Compile the project and then run the program defined by the class SampleMain .

Understanding the Sample Program

When run, the sample program does these things:

It creates an in-memory SQL database with some sample data

It connects to this database using JPA to map SQL tables to Java objects

It runs some sample queries using Jinq

The code is laid out so that

The SampleMain class in the default package contains the main query code. These queries are run by the runSampleQueries() method. For each query, Jinq will describe what information it is trying to find; then it will print out the JPQL query it has generated from the sample code; finally, it will print out the results returned by the query.

class in the default package contains the main query code. These queries are run by the method. For each query, Jinq will describe what information it is trying to find; then it will print out the JPQL query it has generated from the sample code; finally, it will print out the results returned by the query. The SampleDbCreator class creates the sample data for the in-memory SQL database.

class creates the sample data for the in-memory SQL database. The JPA entities are defined in the package com.example.jinq.sample.jpa.entities . They define the mapping of SQL tables to Java objects. The mapping is compatible with JPA object-relational mapping tools like Hibernate or EclipseLink.

Below is the code for one of the queries in the sample program. It first prints out that it is running a query to find all the customers from the UK. It then executes the query. The query code gets all the customers from the database. It iterates over those customers and keeps only those where the Country field of the customer is equal to "UK". Finally, the results of the query are displayed on the screen.

out.println("CUSTOMERS FROM THE UK"); customers() .where(c -> c.getCountry().equals("UK")) .forEach( c -> out.println(c.getName() + " " + c.getCountry()));

When the code is run, the sample code displays the text below on the screen. It says that it is looking for customers from the UK. It displays the database query that Jinq derived from the sample code and that it is using to find the results. Then it displays the results it found. The sample database only contains one customer from the UK, who is named Dave.

CUSTOMERS FROM THE UK SELECT A FROM Customer A WHERE A.country = 'UK' Dave UK

Jinq Queries