User:Nerdwiz/sandbox
Developer(s) | Speedment, Inc. |
---|---|
Repository | Maven |
Written in | Java |
License | Apache 2.0 and Enterprise License |
Website | www.speedment.com |
Pure Java Stream ORM
Speedment is a Java 8 Stream ORM Toolkit and Runtime. The toolkit analyzes the metadata of an existing legacy relational database and creates a Java representation of the data model which together with the Speedment runtime allows the user to create scalable and efficient Java applications using standard Java 8 streams without any specific query language or any new API.
Paradigm
Expressing SQL as Java 8 Streams
The open-source project Speedment was founded with the main objective to remove the polyglot requirement for Java database application developers. There is a strong resemblance between Java streams and SQL as summarized in this simplified table:
SQL | Java 8 Stream Equivalent |
---|---|
FROM
|
stream()
|
SELECT
|
map()
|
WHERE
|
filter() (before collecting)
|
HAVING
|
filter() (after collecting)
|
JOIN
|
flatMap()
|
DISTINCT
|
distinct()
|
UNION
|
concat(s0, s1).distinct()
|
ORDER BY
|
sorted()
|
OFFSET
|
skip()
|
LIMIT
|
limit()
|
GROUP BY
|
collect(groupingBy())
|
COUNT
|
count()
|
Example
One-liner
Search for a long film (of length greater than 120 minutes):
// Searches are optimized in the background!
Optional<Film> longFilm = films.stream()
.filter(Film.LENGTH.greaterThan(120))
.findAny();
Results in the following SQL query:
SELECT
`film_id`,`title`,`description`,`release_year`,
`language_id`,`original_language_id`,`rental_duration`,`rental_rate`,
`length`,`replacement_cost`,`rating`,`special_features`,
`last_update`
FROM
FROM `sakila`.`film
WHERE
(`length` > 120)
No need for manually writing SQL-queies any more, but you can keep to pure Java.