Easy database queries for Java

Legacy Hibernate

Jinq can be used with Hibernate's legacy Session and SessionFactory APIs. If you have a legacy Hibernate project that has not yet been upgraded to use Hibernate's JPA-compliant APIs, you can still use Jinq in your project.

Build

To use Jinq with your JPA code, you need to

Set-Up

When Jinq is used with Hibernate's Session API, it behaves identically to the JPA version of the API, but with thes changes:

For example, below is the code for creating a JinqHibernateStreamProvider. The constructor takes a SessionFactory as a parameter. The stream provider is intended to be a singleton in your application that only needs to be created once.

// Create a SessionFactory
Configuration configuration = 
  new Configuration().configure("hibernate.cfg.xml");
  
ServiceRegistry serviceRegistry = 
  new StandardServiceRegistryBuilder()
    .applySettings(configuration.getProperties())
    .build();
	
SessionFactory sessionFactory = 
  configuration.buildSessionFactory(serviceRegistry);

// Create a provider of Jinq streams  
streams = new JinqHibernateStreamProvider(sessionFactory);

Queries

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

JinqStream<Customer> customers = 
  streams.streamAll(session, Customer.class);

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

List<Customer> customers = streams
  .streamAll(session, Customer.class)
  .where( c -> c.getName().equals("Bob") )
  .toList();

More information about Jinq queries are available in the query guide.