Java 8 in action.Jinq
-
Upload
strannik2013 -
Category
Software
-
view
168 -
download
2
Transcript of Java 8 in action.Jinq
About author• Works in IT since 2000
• 11 year of Java SE/EE experience
• Regular speaker on Java User Group conferences
• Java evangelist
Java 8 overview
• Lambda expressions
• Streams API
• Java time
• Default & static methods
• Method references
• Repeating & type annotations
• Hashorn
• Metaspace
Java time
• Joda replacement
• Supported in Spring 4
• Ignored by Hibernate, partially supported by JPA
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
Sorting
select city.id, city.name, city.population as from City
city order by city.population ASC, city.name ASC limit
?
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 ?
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()
Cons
• Requires JPA
• Limited to JPA functionality
• No stored procedures
• Lacks NoSQL support
• Hard to debug
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