Jump to content

User:Nerdwiz/sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Nerdwiz (talk | contribs) at 15:42, 11 December 2017. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
Speedment
Developer(s)Speedment, Inc.
RepositoryMaven
Written inJava
LicenseApache 2.0 and Enterprise License
Websitewww.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.

[1]

  1. ^ Per, Minborg. [javamagazine.mozaicreader.com/MayJune2017/Default/1/0#&pageSet=34&page=0&contentItem=0 "Oracle Java Magazine, May/June 2017"]. Oracle Java Magazine. {{cite news}}: Check |url= value (help); Cite has empty unknown parameter: |dead-url= (help)