Easy database queries for Java

Using Jinq with Scala (Deprecated)

Jinq can be used with Scala as well as with Java 8. With Jinq it's easy to use robust JPA-compliant object-relational mappers such as EclipseLink and Hibernate in your Scala projects without having to abandon the functional code style of Scala.

Jinq for Scala currently targets version 2.11 of Scala. It has not been tested with the Scala version 2.12. Due to the major changes in that version of Scala, Jinq may be incompatible with it.

Sample Code

The Jinq download comes with a sample Scala project that uses Jinq. You can compile and run the sample code by going into the sample-scala directory and using Scala's sbt to compile and run the program.

The Scala sample code mirrors the structure of the Java 8 sample code. The Java JPA entities and database initialization code are stored in the src/main/java directory. The Scala code for issuing queries against the in-memory database can be found in the src/main/scala directory.

The sample program runs a series of queries. For each query, it first prints the type of query it will run. Then it prints the actual JPQL database query that Jinq has constructed from the sample code and is executing. Finally, it prints out the results of the query.

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

Build

If you already have a Scala project that includes some JPA entities, you can add Jinq to your project by adding the jinq-jpa-scala dependency to your existing sbt or Maven build.

Set-Up

Jinq queries are written by taking streams of database data and filtering and transforming that data. To use Jinq when using JPA for database access, you need a way to get a Jinq stream of JPA data. You can do this with the org.jinq.jpa.JinqJPAScalaIteratorProvider class.

The JinqJPAScalaIteratorProvider's constructor normally takes a JPA EntityManagerFactory as a parameter. The JinqJPAScalaIteratorProvider is intended to be a singleton in your application that only needs to be created once.

val entityManagerFactory = 
  Persistence.createEntityManagerFactory("JPA");

val streams = new JinqJPAStreamProvider(
  entityManagerFactory);

In some cases, it is easier to get a reference to an EntityManager instead of an EntityManagerFactory. You can also create JinqJPAScalaIteratorProvider objects from an EntityManager whenever you want to perform a Jinq query although this is less efficient.

val streams = new JinqJPAScalaIteratorProvider(
  entityManager.getMetamodel());

Queries

Once you've created a JinqJPAScalaIteratorProvider, you can then use the streamAll() method to get a Jinq stream of JPA entities from a database. For example, if you have a Customer JPA entity and a JPA EntityManager called em, then you can get a stream of Customer objects from your database using the code below:

val customerStream = 
  streams.streamAll(em, classOf[Customer]);

Once you have this stream of entities, you can filter and transform it using the Jinq approach:

val customers = streams
  .streamAll(em, classOf[Customer])
  .where( c => c.getName() == "Bob" )
  .toList;

Jinq queries for Scala have behave similarly to Jinq queries for Java. More information about Jinq queries for Java is available in the query guide.

Differences

Here are the main differences between Jinq for Scala and Jinq for Java:

Limitations

Here are some known limitations of the current implementation of Jinq for Scala: