java 8 in action.jinq.v.1.3
TRANSCRIPT
About author• Works in IT since 2000
• 11 year of Java SE/EE experience
• Regular speaker on Java User Group conferences
• Java evangelist
Agenda
• Java 8 overview
• Libraries support
• Jinq
• Q&A
Java 8 overview
Java 8 overview
• Lambda expressions
• Streams API
• Java time
• Default & static methods
• Method references
• Repeating & type annotations
• Hashorn
• Metaspace
Native support
• Lambda expressions
• Default methods
• Method references
• Nashorn
• Metaspace
Type annotations
• @Nullable
• @NonNull
• @ReadOnly
Repeatable annotations
• Spring Framework
Java time
• Joda replacement
• Supported in Spring 4
• Ignored by Hibernate, partially supported by JPA
Hibernate
JPA
JPA
Streams API
Streams API
File storage
Data access
Data access
• JDBC
• Spring JDBC
• ORM (Hibernate)
• JPA
• Functional programming
Recently
• SQL
• HQL/JPQL
• Hibernate/JPA criteria
• Custom API
Jinq
• Initially developed by Dr. Ming-Yee
• Open-source project
• Functional database queries in Java and Scala
• Inspired by LINQ
• Required JPA & Java 8
Jinq flow
• Translated Java code and Run it as database query
statements
• Run as ordinary Java code
Configuration
• Map entities
• JPA provider
• JPA configuration
Configuration
Configuration
Count
select count(city.id) from City city
Filtering
Jinq
select city.id, city.name, city.population from City city
limit ?
Where
select count(city.id) from City city where
city.population>100
Variables
select count(city.id) from City city where
city.population > ?
Select
select city.name from City city limit ?
Comparison
select count(city.id) from City city where
city.name='Odessa'
Comparison
select count(city.id) from City city where city.name is
not null
Concatenation
select concat('City:', city.name) from City city limit ?
Concatenation
Aggregate
select max(city.population) from City city
Methods
Sorting
select city.id, city.name, city.population as from City
city order by city.population ASC, city.name ASC limit
?
Country
Associations
select count(city.id) from City city cross join Country
country where city.countryId=country.id and
country.name='Ukraine'
Pairs
select city.id, country.id, city.countryId, city.name,
city.population, country.name from City city inner join
Country country on city.countryId=country.id limit ?
Join
• join()
• joinList()
• selectAll()
Grouping
• select city.countryId, sum(city.population),
count(city.id), country.id, country.name from City
city inner join Country country on city.countryId=
country.id group by city.countryId limit ?
Limitations
• Your code cannot contain any loops
• Your code can call other methods but only those
from a restricted list with known side-effects
• Your code can read and modify local variables
(since these changes will be discarded once the
function exits)
• Your code can read but not modify non-local
variables
• Your code cannot contain casting between
different data types
Data types
Java type Supported operations
String equals()
BigDecimal, BigInteger, Integer, int, Double, double, Long, long
==, <, <=, >, >=, !=, +, -, *, /
Boolean, boolean !, &&, ||, ==
java.util.Date, java.sql.Date, java.sql.Time, java.sql.Timestamp, java.util.Calendar
equals(), before(), after()
enums ==, !=
entities ==, !=
java.util.Collection contains(), JPQL.isIn()
Functions
Java JPQL alternative
Math.abs() / BigDecimal.abs() / BigInteger.abs()
ABS()
Math.sqrt() SQRT()
% MOD()
String.toUpperCase() / String.toLowerCase()
UPPER() / LOWER()
String.trim() TRIM()
String.length() LENGTH()
String.indexOf() LOCATE()
Pro
• Brings up functional style
• Database & provider independent
• SQL-92 - compatible
Cons
• Requires JPA
• Limited to JPA functionality
• No stored procedures
• Lacks NoSQL support
• Hard to debug
To be continued?
• XML
• JSON
• JDBC
Jooq
• Java object oriented querying
• Lightweight alternative
• JDBC wrapper
• SQL building, code generation & SQL execution
• Jinq provides basic support for Jooq
• Open-source & paid version
References