Maintainers
Pricing
Enterprise
More
More
Documentation
Try Live
Rankings
Install
Blog
Docs7
Ask
New
Add Docs
JPAstreamer
https://speedment.github.io/jpa-streamer/jpa-streamer/latest/
Admin
An open source library to instantly process data as Java Streams, providing intuitive
...
Tokens:
23,448
Snippets:
347
Trust Score:
9.7
Update:
4 months ago
Context
Chat
Benchmark
Latest
Show doc for...
Code
Info
Show Results
Tokens:
Raw
Copy
Link
Tokens:
Raw
Copy
Link
### Instantiate and Query with JPAStreamer Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/fetching-data/fetching-data.html Example demonstrating how to create a JPAStreamer instance using a builder and then perform a query to count films whose titles start with 'A'. ```java public static void main(String[] args) { JPAStreamer jpaStreamer = JPAStreamer.createJPAStreamerBuilder("sakila") .build(); long count = jpaStreamer.stream(Film.class) .filter(Film$.title.startsWith("A")) .count(); System.out.format("There are %d films with a title that starts with A", count); } ``` -------------------------------- ### forEach() Example Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/stream-fundamentals/terminal_operations.html Performs a given action for each element in the stream. The order of execution is not guaranteed. ```java Stream.of("B", "A", "C" , "B") .forEach(System.out::print); ``` -------------------------------- ### Basic Java Stream Example Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/stream-fundamentals/stream_basics.html Demonstrates creating a Stream of strings, filtering them by length, sorting them, and collecting them into a list. ```Java Stream.of( "Zlatan", "Tim", "Bo", "George", "Adam", "Oscar" ) .filter(n -> n.length() > 2) .sorted() .collect(Collectors.toList()); ``` -------------------------------- ### findFirst() Example Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/stream-fundamentals/terminal_operations.html Returns an Optional describing the first element of this stream, or an empty Optional if the stream is empty. ```java Stream.of("B", "A", "C", "B") .findFirst(); ``` ```java Stream.<String>empty() .findFirst(); ``` -------------------------------- ### forEachOrdered() Example Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/stream-fundamentals/terminal_operations.html Performs a given action for each element in the stream in the encounter order. This guarantees a specific output order. ```java Stream.of("B", "A", "C" , "B") .forEachOrdered(System.out::print); ``` -------------------------------- ### collect() to List Example Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/stream-fundamentals/terminal_operations.html Collects stream elements into a List. This is a common way to gather results from a stream. ```java Stream.of("B", "A", "C" , "B") .collect(Collectors.toList()); ``` -------------------------------- ### min() Example Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/stream-fundamentals/terminal_operations.html Finds the minimum element in a stream based on a provided Comparator. Returns an empty Optional for an empty stream. ```java Stream.of("B", "A", "C" , "B") .min(String::compareTo); ``` ```java Stream.<String>empty() .min(String::compareTo); ``` -------------------------------- ### collect() to Set Example Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/stream-fundamentals/terminal_operations.html Collects stream elements into a Set, automatically removing duplicates. ```java Stream.of("B", "A", "C" , "B") .collect(Collectors.toSet()); ``` -------------------------------- ### count() Example Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/stream-fundamentals/terminal_operations.html Returns the total number of elements in the stream. For an empty stream, it returns 0. ```java Stream.of("B", "A", "C" , "B") .count(); ``` ```java Stream.empty() .count(); ``` -------------------------------- ### JPA Entity Bean Example Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/how-it-works/how-it-works.html An example of a JPA entity bean named Film, including its fields and annotations. ```java import javax.persistence.*; @Entity @Table(name = "film", schema = "sakila") public class Film { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "film_id", nullable = false, updatable = false, columnDefinition = "smallint(5)") private Integer filmId; @Column(name = "title", nullable = false, columnDefinition = "varchar(255)") private String title; // ... } ``` -------------------------------- ### Peek at Stream Elements for Debugging Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/stream-fundamentals/intermediate_operations.html Use peek to perform an action on each element as it passes through the stream, primarily for debugging. This example prints each element. ```Java Stream.of("B", "A", "C" , "B") .peek(System.out::print) ``` -------------------------------- ### Java Predicate Example Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/predicates/predicates.html Demonstrates using a Java functional interface Predicate to filter strings. This example shows basic predicate usage outside of JPAstreamer. ```java Predicate<String> startsWithA = (String s) -> s.startsWith("A"); Stream.of("Snail", "Ape", "Bird", "Ant", "Alligator") .filter(startsWithA) .forEachOrdered(System.out::println); ``` -------------------------------- ### findAny() Example Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/stream-fundamentals/terminal_operations.html Returns an Optional describing some element of this stream, or an empty Optional if the stream is empty. The element returned may be arbitrary. ```java Stream.of("B", "A", "C", "B") .findAny(); ``` ```java Stream.<String>empty() .findAny(); ``` -------------------------------- ### max() Example Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/stream-fundamentals/terminal_operations.html Finds the maximum element in a stream based on a provided Comparator. Returns an empty Optional for an empty stream. ```java Stream.of("B", "A", "C" , "B") .max(String::compareTo); ``` ```java Stream.<String>empty() .max(String::compareTo); ``` -------------------------------- ### Apply Query Hints with StreamConfiguration Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/fetching-data/fetching-data.html Use `StreamConfiguration.of()` and chain `withHint()` calls to pass query hints to the underlying JPA provider. This example sets a query timeout and a read-only hint. ```java StreamConfiguration sc = StreamConfiguration.of(Film.class) .withHint("javax.persistence.query.timeout", 50) .withHint("org.hibernate.readOnly", true); List<Film> films = jpaStreamer.stream(sc) .filter(Film$.title.startsWith("A")) .sorted(Film$.length) .limit(10) .collect(Collectors::toList); ``` -------------------------------- ### Fetch Second Page of Films Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/fetching-data/stream-examples.html Example of how to call the filmPage method to fetch the second page of films, sorted by title in descending order. ```java filmPage(1, Film$.title.reversed()); ``` -------------------------------- ### collect() to Map Example Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/stream-fundamentals/terminal_operations.html Collects stream elements into a Map, using a key extractor and a value extractor. Ensure keys are unique or handle collisions. ```java Stream.of("I", "am", "a", "stream") .collect( Collectors.toMap( s -> s.toLowerCase(), // Key extractor s -> s.length()) // Value extractor ) ``` -------------------------------- ### Stream Query Example Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/introduction/introduction.html Demonstrates a JPAstreamer query to filter films by rating, sort them, skip records, and limit the results. The Film$ class is automatically generated by JPAstreamer. ```java jpaStreamer.stream(Film.class) .filter(Film$.rating.equal("G")) .sorted(Film$.length.reversed().thenComparing(Film$.title.comparator())) __**(1)** .skip(10) .limit(5) .forEach(System.out::println); ``` -------------------------------- ### Map Stream Elements to Lowercase Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/stream-fundamentals/intermediate_operations.html Use map to transform each element in the stream. This example converts all elements to their lowercase representation. ```Java Stream.of("B", "A", "C" , "B") .map(s -> s.toLowerCase()) ``` -------------------------------- ### Find Films by Title Prefix Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/predicates/string-predicates.html Finds and prints films where the title starts with a specified prefix. Useful for searching by the beginning of a title. ```Java jpaStreamer.stream(Film.class) .filter(Film$.title.startsWith("ALABAMA")) .forEachOrdered(System.out::println); ``` -------------------------------- ### noneMatch() Example Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/stream-fundamentals/terminal_operations.html Checks if no elements in the stream match the given predicate. Returns true if no match is found, false otherwise. ```java Stream.of("B", "A", "C", "B") .noneMatch("A"::equals); ``` ```java Stream.of("B", "A", "C", "B") .noneMatch("Z"::equals); ``` -------------------------------- ### Select Multiple Fields into a Custom Class (SELECT) Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/fetching-data/sql-equivalents.html Create a custom class to hold multiple selected fields and map stream elements to instances of this class. This example creates a TitleLength object. ```java public static final class TitleLength { private final String title; private final int length; public TitleLength(Film film) { this.title = film.getTitle(); this.length= film.getLength(); } public String title() { return title; } public int length() { return length; } @Override public String toString() { return "TitleLength{" + "title='" + title + "'" + ", length=" + length + '}'; } } jpaStreamer.stream(Film.class) .map(TitleLength::new) .forEach(System.out::println); ``` -------------------------------- ### Get Distinct Values (DISTINCT) Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/fetching-data/sql-equivalents.html Use the .distinct() stream operation to retrieve unique values from a stream. This example finds all distinct film ratings. ```java Set<String> ratings = jpaStreamer.stream(Film.class) .map(Film$.rating) .distinct() .collect(Collectors.toSet()); ``` -------------------------------- ### Filter films with length between two values (fully inclusive) Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/predicates/comparable-predicates.html Use `between` with an `Inclusion` enum to specify range inclusivity. This example demonstrates a fully inclusive range (start and end included). ```Java jpaStreamer.stream(Film.class) .filter(Film$.length.between(60, 120, Inclusion.START_INCLUSIVE_END_INCLUSIVE)) ``` -------------------------------- ### anyMatch() Example Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/stream-fundamentals/terminal_operations.html Checks if at least one element in the stream matches the given predicate. Returns true if a match is found, false otherwise. ```java Stream.of("B", "A", "C", "B") .anyMatch("A"::equals); ``` ```java Stream.of("B", "A", "C", "B") .anyMatch("Z"::equals); ``` -------------------------------- ### Limit Stream Size Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/stream-fundamentals/intermediate_operations.html Use limit to truncate the stream to a specified number of elements. This example keeps only the first two elements. ```Java Stream.of("B", "A", "C" , "B") .limit(2) ``` -------------------------------- ### Skip Stream Elements Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/stream-fundamentals/intermediate_operations.html Use skip to discard a specified number of elements from the beginning of the stream. This example skips the first element. ```Java Stream.of("B", "A", "C" , "B") .skip(1) ``` -------------------------------- ### Configure Maven Compiler Plugin for JPA Streamer Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/quick-start/quick-start.html Configures the Maven Compiler Plugin to set the package name, prefix, and suffix for generated entities. This example sets the package to 'com.speedment.jpastreamer.test', prefix to 'A', and suffix to 'B'. ```xml <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.10.1</version> <configuration> <compilerArgs> <arg>-AjpaStreamerPackage=com.speedment.jpastreamer.test</arg> <arg>-AjpaStreamerPrefix=A</arg> <arg>-AjpaStreamerSuffix=B</arg> </compilerArgs> </configuration> </plugin> ``` -------------------------------- ### Filter Films by Less Than Length Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/predicates/comparable-predicates.html Prints films with a length less than a specified value using the `lessThan` predicate. This example demonstrates filtering based on a numerical range. ```java jpaStreamer.stream(Film.class) .filter(Film$.length.lessThan(120)) .forEachOrdered(System.out::println); ``` -------------------------------- ### Limit Records Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/fetching-data/sql-equivalents.html Controls the number of records returned by the stream. This example limits the output to the first 3 films sorted by title. ```java jpaStreamer.stream(Film.class) .sorted(Film$.title) .limit(3) .forEachOrdered(System.out::println); ``` -------------------------------- ### Efficient Combined Filtering (UNION) Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/fetching-data/sql-equivalents.html An efficient alternative to UNION, this example uses the .or() predicate to combine conditions within a single stream operation. ```java jpaStreamer.stream(Film.class) .filter(Film$.length.greaterThan(120).or(Film$.rating.equal("PG-13"))) .forEachOrdered(System.out::println); ``` -------------------------------- ### JPAstreamer Metamodel Generation Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/how-it-works/how-it-works.html Example of a generated metamodel class Film$ corresponding to the Film entity. It represents columns as Fields for predicate composition. ```java public class Film$ { public static final ComparableField<Film, Integer> filmId = ComparableField.create( Film.class, __**(1)** "filmId", __**(2)** Film::getFilmId, __**(3)** false __**(4)** ); public static final StringField<Film> title = StringField.create( Film.class, "title", Film::getTitle, false ); // ... } ``` -------------------------------- ### Filter Films Starting With a String (Case-Insensitive) Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/predicates/string-predicates.html Use `startsWithIgnoreCase` to find films whose titles begin with a specified prefix, ignoring the case of the characters. This is helpful when the casing of the input data may vary. ```Java jpaStreamer.stream(Film.class) .filter(Film$.title.startsWithIgnoreCase("ala")) .forEachOrdered(System.out::println); ``` -------------------------------- ### Sort Stream Elements Naturally Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/stream-fundamentals/intermediate_operations.html Use sorted() to arrange stream elements in their natural order. This example sorts the elements alphabetically. ```Java Stream.of("B", "A", "C" , "B") .sorted() ``` -------------------------------- ### Skip Records with Offset Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/fetching-data/sql-equivalents.html Skips a specified number of records before processing. This example skips the first 100 films when sorting by title. ```java films.stream() .sorted(Film$.title) .skip(100) .forEachOrdered(System.out::println); ``` -------------------------------- ### JPAstreamer Example with Entities Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/stream-fundamentals/stream_basics.html Shows how to use JPAstreamer to create a Stream of JPA Entities (Film objects), extract specific fields (titles), filter them by length, sort them, and collect the results into a list. This stream is rendered to a database query. ```Java jpaStreamer.stream(Film.class) .map(Film$.title) .filter(s -> s.length() > 4) .sorted() .collect(Collectors.toList()); ``` -------------------------------- ### Filter Stream Elements Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/stream-fundamentals/intermediate_operations.html Use filter to retain only elements that match a given Predicate. This example keeps elements equal to 'B'. ```Java Stream.of("B", "A", "C" , "B") .filter(s -> s.equals("B")) ``` -------------------------------- ### reduce() with identity and BinaryOperation Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/stream-fundamentals/terminal_operations.html Reduces the stream elements to a single value, starting with an identity value. If the stream is empty, the identity value is returned. ```java Stream.of(1, 2, 3, 4) .reduce(100, (a, b) -> a + b); ``` -------------------------------- ### Filter Films Not Starting With a String Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/predicates/string-predicates.html Use `notStartsWith` to exclude films whose titles begin with a specified prefix. This is useful for filtering out unwanted categories of titles. ```Java jpaStreamer.stream(Film.class) .filter(Film$.title.notStartsWith("ALABAMA")) .forEachOrdered(System.out::println); ``` -------------------------------- ### Select Specific Fields (SELECT) Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/fetching-data/sql-equivalents.html Use the .map() operation to select specific fields from entities, similar to a SQL SELECT clause. This example selects only the film ID. ```java final IntStream ids = jpaStreamer.stream(Film.class) .mapToInt(Film$.filmId); ``` -------------------------------- ### Filter Films Not Starting With a String (Case-Insensitive) Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/predicates/string-predicates.html Use `notStartsWithIgnoreCase` to exclude films whose titles begin with a specified prefix, ignoring character case. This ensures that variations in capitalization do not affect the filtering results. ```Java jpaStreamer.stream(Film.class) .filter(Film$.title.notStartsWithIgnoreCase("ala")) .forEachOrdered(System.out::println); ``` -------------------------------- ### Count Records Meeting Criteria Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/fetching-data/sql-equivalents.html Counts the number of records that satisfy a given filter. This example counts films longer than 120 minutes. ```java long noLongFilms = jpaStreamer.stream(Film.class) .filter(Film$.length.greaterThan(120)) .count(); ``` -------------------------------- ### Filter after Grouping (HAVING) Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/fetching-data/sql-equivalents.html Apply a filter after collecting a grouped stream to achieve HAVING clause functionality. This example filters film categories to include only those with more than 200 films. ```java Map<String, List<Film>> filmCategories = jpaStreamer.stream(Film.class) .collect( Collectors.groupingBy( Film$.rating ) ) .entrySet() .stream() .filter(e -> e.getValue().size() > 200) .collect( toMap(Entry::getKey, Entry::getValue) ); ``` -------------------------------- ### Performing JOINs with JPAStreamer Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/fetching-data/sql-equivalents.html This snippet demonstrates how to join multiple tables using JPAStreamer. It starts with a base entity (Film) and then joins related entities (actors, language) based on declared JPA relationships. Stream operations like filtering can then be applied to the joined data. ```java jpaStreamer.stream( StreamConfiguration .of(Film.class) .joining(Film$.actors) .joining(Film$.language) ) .filter(Film$.length.between(100, 120)) .forEach(System.out::println); ``` -------------------------------- ### Maven Build Helper Plugin Configuration Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/quick-start/quick-start.html Configure the build-helper-maven-plugin to add the generated metamodel sources to your project. ```xml <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>build-helper-maven-plugin</artifactId> <version>3.2.0</version> <executions> <execution> <phase>generate-sources</phase> <goals> <goal>add-source</goal> </goals> <configuration> <sources> <source>${project.build.directory}/generated-sources/annotations</source> </sources> </configuration> </execution> </executions> </plugin> </plugins> ``` -------------------------------- ### Film Entity Definition Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/transactions/transactions.html Defines the Film entity with rating and rentalRate fields. This is the entity used in the transaction example. ```java final EntityManagerFactory emf = Persistence.createEntityManagerFactory("sakila"); final EntityManager em = emf.createEntityManager(); final JPAStreamer jpaStreamer = JPAStreamer.of(emf); ``` -------------------------------- ### Initialize JPAStreamer Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/introduction/introduction.html Initializes JPAStreamer with a specified persistence unit name. This is the first step to operating on database elements using JPAstreamer. ```java JPAStreamer jpaStreamer = JPAStreamer.of("sakila"); ``` -------------------------------- ### Gradle Dependencies and Source Configuration Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/quick-start/quick-start.html Add JPAstreamer dependencies and configure source directories in your build.gradle file. ```gradle repositories { mavenCentral() } dependencies { compile "com.speedment.jpastreamer:jpastreamer-core:version" annotationProcessor "com.speedment.jpastreamer:fieldgenerator-standard:version" } sourceSets { main { java { srcDir 'src/main/java' srcDir 'target/generated-sources/annotations' } } } ``` -------------------------------- ### Select Specific Columns with StreamConfiguration Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/fetching-data/sql-equivalents.html Limit the columns fetched from the database before Stream operations begin by using 'selecting' with a Projection. This is an efficient way to retrieve only necessary data. ```Java StreamConfiguration<Film> sc = StreamConfiguration.of(Film.class).selecting(Projection.select(Film$.filmId, Film$.title)); jpaStreamer.stream(sc) .sorted(Film$.length.reversed()) .limit(3) .forEach(System.out::println); ``` -------------------------------- ### Initialize JPAStreamer Source: https://speedment.github.io/jpa-streamer Initializes JPAStreamer with a specific persistence unit name. ```java import com.speedment.jpastreamer.application.JPAStreamer; JPAStreamer jpaStreamer = JPAStreamer.of("sakila"); ``` -------------------------------- ### Stream Joining with StreamConfiguration Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/fetching-data/stream-examples.html Configure stream joins for Film entities, including actors and language, using StreamConfiguration to specify join types and prevent N+1 select problems. ```java import static com.speedment.jpastreamer.streamconfiguration.StreamConfiguration.*; ... jpaStreamer.stream(of(Film.class).joining(Film$.actors).joining(Film$.language)) .filter(Film$.length.between(100, 120)) .forEach(System.out::println); ``` -------------------------------- ### Initialize JPAStreamer within PanacheRepository Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/fetching-data/fetching-data.html Demonstrates initializing JPAStreamer using the repository's EntityManager in a CDI environment. ```java @ApplicationScoped public class FilmRepository implements PanacheRepository<Film> { private final JPAStreamer jpaStreamer = JPAStreamer.of(this::getEntityManager); } ``` -------------------------------- ### Combine Offset and Limit Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/fetching-data/sql-equivalents.html Fetches a specific range of records by combining skip and limit operations. Note that the order of operations differs from SQL: `.skip(y).limit(x)`. ```java films.stream() .sorted(Film.TITLE) .skip(100) .limit(50) .forEachOrdered(System.out::println); ``` -------------------------------- ### Using Metamodel for Filtering Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/how-it-works/how-it-works.html Demonstrates how to use the generated metamodel (Film$) to filter entities. The startsWith operator on StringField creates a predicate for the query. ```java long nrOfFilmsStartingWithA = jpaStreamer.stream(Film.class) .filter(Film$.title.startsWith("A")) __**(1)** .count(); ``` -------------------------------- ### Flatten Nested Lists to Stream Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/stream-fundamentals/intermediate_operations.html Use flatMap to convert elements into streams and concatenate them. This example flattens lists of strings into a single stream. ```Java Stream.of( Arrays.asList("B", "A"), Arrays.asList("C", "B") ) .flatMap(l -> l.stream()) ``` -------------------------------- ### Close a Stream and Release Resources Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/stream-fundamentals/other_operations.html Demonstrates closing a stream after use to release associated resources. For streams that require explicit closing, such as those from files, use the try-with-resources statement for automatic resource management. ```java Stream<String> stream = Stream.of("B", "A", "C", "B"); stream.forEachOrdered(System.out::println); stream.close(); ``` ```java try (Stream<String> s = Stream.of("B", "A", "C", "B")) { s.forEachOrdered(System.out::println); } ``` -------------------------------- ### Flatten Nested Streams Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/stream-fundamentals/intermediate_operations.html Use flatMap to convert each element of a stream into another stream, then concatenate these streams. This example flattens streams of strings. ```Java Stream.of( Stream.of("B", "A"), Stream.of("C", "B") ) .flatMap(Function.identity()) .forEachOrdered(System.out::println); ``` -------------------------------- ### JPA Persistence Unit Configuration Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/quick-start/quick-start.html Configure your persistence.xml file with database connection details. ```xml <?xml version="1.0" encoding="UTF-8"?> <persistence version="2.2" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd"> <persistence-unit name="sakila" transaction-type="RESOURCE_LOCAL"> <description>MySQL Sakila Example Database</description> <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> <properties> <!-- Configuring The Database Connection Details --> <property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver" /> <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/sakila" /> <!-- ... --> </properties> </persistence-unit> </persistence> ``` -------------------------------- ### Configure Build Helper Maven Plugin Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/get-jpa-streamer/install-maven.html Configure the build-helper-maven-plugin to add the generated metamodel sources to your project's build path. This is necessary for JPAStreamer to access generated code. ```xml <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>build-helper-maven-plugin</artifactId> <version>3.2.0</version> <executions> <execution> <phase>generate-sources</phase> <goals> <goal>add-source</goal> </goals> <configuration> <sources> <source>${project.build.directory}/generated-sources/annotations</source> </sources> </configuration> </execution> </executions> </plugin> ``` -------------------------------- ### reduce() for parallel streams Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/stream-fundamentals/terminal_operations.html Reduces stream elements in parallel, starting with an identity value. The third lambda combines results from parallel computations. ```java Stream.of(1, 2, 3, 4) .parallel() .reduce( 0, (a, b) -> a + b, (a, b) -> a + b ); ``` -------------------------------- ### Standard Lambda vs. JPAstreamer Field Predicate Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/predicates/predicates.html Illustrates the difference between using a standard Java lambda for filtering and using JPAstreamer's metamodel fields. Using fields is highly encouraged for query optimization. ```java films.stream() .filter(f -> f.getTitle().startsWith("A")) .forEachOrdered(System.out::println); ``` -------------------------------- ### Initialize JPAStreamer with EntityManager Supplier Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/fetching-data/fetching-data.html Use this when JPAStreamer should not manage the EntityManager lifecycle. Useful in environments like PanacheRepository. ```java EntityManagerFactory emf = Persistence.createEntityManagerFactory("sakila"); JPAStreamer jpaStreamer = JPAStreamer.of(emf::createEntityManager); ``` -------------------------------- ### toList() operation Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/stream-fundamentals/terminal_operations.html Returns a List containing the elements of the stream. Requires Java 16 or later. ```java Stream.of("B", "A", "C", "B") .toList(); ``` -------------------------------- ### Inject JPAStreamer via Field Injection Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/get-jpa-streamer/spring_integration.html Example of injecting JPAStreamer directly into a service class using @Autowired. Field injection is generally not recommended. ```java @Service public class DummyService { @Autowired private final JPAStreamer jpaStreamer; } ``` -------------------------------- ### Remove Duplicate Stream Elements Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/stream-fundamentals/intermediate_operations.html Use distinct to keep only unique elements in the stream based on their equals() method. This example removes duplicate 'B's. ```Java Stream.of("B", "A", "C" , "B") .distinct() ``` -------------------------------- ### Eagerly Join Related Entities with StreamConfiguration Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/fetching-data/sql-equivalents.html Configure a Stream to eagerly load related entities using 'joining' to prevent N+1 SELECT problems. This is useful when you know you'll need fields from joined tables. ```Java StreamConfiguration<Film> configuration = StreamConfiguration.of(Film.class) .joining(Film$.actors) .joining(Film$.language); ``` -------------------------------- ### Sort Stream Elements with Comparator Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/stream-fundamentals/intermediate_operations.html Use sorted(Comparator) to sort stream elements according to a specific Comparator. This example sorts elements in reverse alphabetical order. ```Java Stream.of("B", "A", "C" , "B") .sorted(Comparator.reverseOrder()) ``` -------------------------------- ### sum() for IntStream Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/stream-fundamentals/terminal_operations.html Returns the sum of all elements in an IntStream. ```java IntStream.of(1, 2, 3, 4) .sum(); ``` -------------------------------- ### Configure Build Helper Maven Plugin to Add Generated Sources Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/troubleshooting/troubleshooting.html Add the generated metamodel source folder to your project's recognized sources using the Build Helper Maven Plugin. This ensures your IDE can find and import the generated classes. ```xml <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>build-helper-maven-plugin</artifactId> <version>3.4.0</version> <executions> <execution> <phase>generate-sources</phase> <goals> <goal>add-source</goal> </goals> <configuration> <sources> <source> ${project.build.directory}/generated-sources/annotations </source> </sources> </configuration> </execution> </executions> </plugin> ``` -------------------------------- ### Group Films by Rating and List All Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/fetching-data/sql-equivalents.html Groups films by their rating and collects them into a map where keys are ratings and values are lists of films. This pulls the entire table into memory. ```java Map<String, List<Film>> filmCategories = jpaStreamer.stream(Film.class) .collect( Collectors.groupingBy( Film$.rating ) ); map.forEach((k, v) -> System.out.format( "Rating %-5s maps to %d films %n", k, v.size() ) ); ``` -------------------------------- ### Map One-to-Many Relationship (Language to Films) Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/fetching-data/stream-examples.html Maps languages to a set of their associated films using a OneToMany join. This prevents extra queries when accessing films for each language. ```java Map<Language, Set<Film>> languageFilmMap = jpaStreamer.stream(of(Language.class).joining(Language$.films)) .collect(toMap( Function.identity(), Language::getFilms ) ); ``` -------------------------------- ### Filter films with length not between two values (fully inclusive) Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/predicates/comparable-predicates.html Use `notBetween` with an `Inclusion` enum to specify range inclusivity for the excluded range. This example demonstrates a fully inclusive excluded range. ```Java jpaStreamer.stream(Film.class) .filter(Film$.length.notBetween(60, 120, Inclusion.START_INCLUSIVE_END_INCLUSIVE)) .forEachOrdered(System.out::println); ``` -------------------------------- ### Initialize JPAStreamer from EntityManagerFactory Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/fetching-data/fetching-data.html Initialize JPAStreamer by reusing an existing EntityManagerFactory. In this case, JPAStreamer does not manage the lifecycle of the factory. ```java EntityManagerFactory emf = Persistence.createEntityManagerFactory("sakila"); JPAStreamer jpaStreamer = JPAStreamer.of(emf); ``` -------------------------------- ### Stream All Entities from a Table Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/fetching-data/sql-equivalents.html Create a Stream of all entities from a specific table by referencing its corresponding Entity class. This is the equivalent of a 'FROM' clause in SQL. ```Java jpaStreamer.stream(Film.class); ``` -------------------------------- ### summaryStatistics() for IntStream Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/stream-fundamentals/terminal_operations.html Returns a summary of statistics (count, sum, min, average, max) for elements in an IntStream. Provides default values for empty streams. ```java IntStream.of(1, 2, 3, 4) .summaryStatistics(); ``` -------------------------------- ### Map Many-to-One Relationship (Film to Language) Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/fetching-data/stream-examples.html Maps films to their spoken language for films with a specific rating (PG-13), using a ManyToOne join. This efficiently retrieves the language for each matching film. ```java Map<Film, Language> languageMap = jpaStreamer.stream(of(Film.class).joining(Film$.language)) __**(1)** .filter(Film$.rating.equal("PG-13")) .collect( Collectors.toMap(Function.identity(), __**(2)** Film::getLanguage __**(3)** ) ); ``` -------------------------------- ### Create an Unordered Stream Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/stream-fundamentals/intermediate_operations.html Use unordered() to indicate that the stream's encounter order is not significant. Elements may be encountered in any order. ```java Stream.of("B", "A", "C" , "B") .unordered() ``` -------------------------------- ### Map Actors to Films (Many-to-Many) Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/fetching-data/stream-examples.html Collects a map where keys are actors and values are lists of films they starred in. This is useful for displaying an actor's filmography. ```java Map<Actor, List<Film>> filmography = jpaStreamer.stream(of(Actor.class).joining(Actor$.films)) \ .collect( Collectors.toMap(Function.identity(), \ Actor::getFilms \ ) ); ``` -------------------------------- ### average() for IntStream Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/stream-fundamentals/terminal_operations.html Returns the average of all elements in an IntStream. Returns OptionalDouble.empty() if the stream is empty. ```java IntStream.of(1, 2, 3, 4) .average(); ``` -------------------------------- ### Configure Gradle Source Sets for Metamodel Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/get-jpa-streamer/install-gradle.html Configure your build.gradle file to include the generated metamodel sources in the main Java source set. This ensures that JPAStreamer can access the generated model classes. ```gradle sourceSets { main { java { srcDir 'src/main/java' srcDir 'target/generated-sources/annotations' } } } ``` -------------------------------- ### reduce() with BinaryOperation Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/stream-fundamentals/terminal_operations.html Reduces the stream elements to a single value using a BinaryOperation. Returns an Optional if the stream might be empty. ```java Stream.of(1, 2, 3, 4) .reduce((a, b) -> a + b); ``` -------------------------------- ### Fetch Paginated Films Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/fetching-data/stream-examples.html Fetches a specific page of Film entities, sorted by a given comparator. Useful for GUI pagination. ```java private static final int PAGE_SIZE = 20; private static List<Film> filmPage(JPAStreamer jpaStreamer, int page, Comparator<Film> comparator) { return jpaStreamer.stream(Film.class) .sorted(comparator) .skip(page * PAGE_SIZE) .limit(PAGE_SIZE) .collect(Collectors.toList()); } ``` -------------------------------- ### Pivot Table: Actor Film Counts by Rating Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/fetching-data/stream-examples.html Creates a pivot table showing the count of films for each rating category an actor has participated in. Requires Java 9+ for Collectors.flatMapping. ```java Map<Actor, Map<String, Long>> pivot = jpaStreamer.stream(of(Actor.class).joining(Actor$.films)) \ .collect( groupingBy(Function.identity(), \ Collectors.flatMapping(a -> a.getFilms().stream(), \ groupingBy(Film::getRating, counting())) \ ) ); ``` -------------------------------- ### Equivalent AND using chained filters Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/predicates/combining-predicates.html Chaining two filter operations achieves the same result as using the `and()` method. ```java jpaStreamer.stream(Film.class) .filter(Film$.length.greaterThan(120)) .filter(Film$.rating.equal("PG-13")) .forEachOrdered(System.out::println); ``` -------------------------------- ### Initial Projection for Film Columns Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/fetching-data/stream-examples.html Limits the stream source to specific columns (filmId, title) using an initial projection. Non-selected fields will be null or default values. ```java jpaStreamer.stream(Projection.select(Film$.filmId, Film$.title)) __**(1)** .sorted(Film$.length.reversed()) .limit(3) .forEach(System.out::println); ``` -------------------------------- ### JPAstreamer Predicate with StringField Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/predicates/predicates.html Shows how to use a StringField from JPAstreamer's metamodel to create an optimized predicate for filtering films by title. This approach allows JPAstreamer to optimize the generated SQL query. ```java jpaStreamer.stream(Film.class) .filter(Film$.title.startsWith("A")) .forEachOrdered(System.out::println); ``` -------------------------------- ### collect() with Supplier, BiConsumer, BiConsumer Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/stream-fundamentals/terminal_operations.html Collects stream elements into a mutable result container. The supplier creates the container, the first BiConsumer adds elements, and the second BiConsumer merges containers (for parallel streams). ```java Stream.of("B", "A", "C", "B") .collect( () -> new StringBuilder(), (sb0, sb1) -> sb0.append(sb1), (sb0, sb1) -> sb0.append(sb1) ); ``` -------------------------------- ### Combine Predicates with AND Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/predicates/combining-predicates.html Use the `and()` method to create a predicate that represents a logical AND of two predicates. The second predicate is only evaluated if the first one is true. ```java Predicate<Film> isLong = Film$.length.greaterThan(120); Predicate<Film> isPG13 = Film$.rating.equal("PG-13"); jpaStreamer.stream(Film.class) .filter(isLong.and(isPG13)) .forEachOrdered(System.out::println); ``` -------------------------------- ### Partition Films by Length Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/fetching-data/stream-examples.html Classifies films into short and long categories based on length using Java's partitioning collector. ```java Map<Boolean, List<Film>> map = jpaStreamer.stream(Film.class) .collect( Collectors.partitioningBy(Film$.length.greaterThan(120)) ); map.forEach((k, v) -> { System.out.format("long is %5s has %d films%n", k, v.size()); }); ``` -------------------------------- ### Create and Reuse StreamSupplier with JPAStreamer Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/fetching-data/fetching-data.html Use `JPAStreamer.createStreamSupplier()` to obtain a reusable `StreamSupplier`. This allows for reusing the `EntityManager` across multiple streams. Use a try-with-resources block to ensure the `StreamSupplier` and `EntityManager` are closed. ```java final JPAStreamer jpaStreamer = JPAStreamer.of("sakila"); try (final StreamSupplier<Film> streamSupplier = jpaStreamer.createStreamSupplier()) { final List<Film> shortFilms = streamSupplier.stream(Film.class) .filter(Film$.length.lessThan(60)) .collect(toList()); final List<Film> longFilms = streamSupplier.stream(Film.class) .filter(Film$.length.greatherThanOrEqual(61)) .collect(toList()); } ``` -------------------------------- ### Stream Query with Filtering and Sorting Source: https://speedment.github.io/jpa-streamer Creates a stream from the Film entity, filters by rating, sorts by length and title, skips records, and limits the result set. ```java import com.speedment.jpastreamer.field.Field; jpaStreamer.stream(Film.class) .filter(Film$.rating.equal("G")) .sorted(Film$.length.reversed().thenComparing(Film$.title.comparator())) __**(1)** .skip(10) .limit(5) .forEach(System.out::println); ``` -------------------------------- ### Apply Limit/Skip on JVM for Join Queries Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/troubleshooting/troubleshooting.html When using join queries, JPAStreamer cannot apply .limit() or .skip() directly due to JPA Criteria API limitations. Apply these operations on the JVM after collecting the results. ```java List<Film> films = jpaStreamer.stream(StreamConfiguration.of(Film.class).joining(Film$.actors)) .filter(Film$.title.startsWith("A")) // .limit(20) __**(1)** .collect(Collectors.toList()); __**(2)** films.stream().limit(20).forEach(System.out::println); __**(3)** ``` -------------------------------- ### toArray() with array constructor Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/stream-fundamentals/terminal_operations.html Returns an array containing the elements of the stream, created by the provided constructor. ```java Stream.of("B", "A", "C", "B") .toArray(String[]::new) ``` -------------------------------- ### Configure Spring Boot Application for JPAStreamer Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/get-jpa-streamer/spring_integration.html Set the scanBasePackages parameter on the @SpringBootApplication annotation to include the JPAStreamer package for dependency injection. ```java @SpringBootApplication(scanBasePackages={"com.speedment.jpastreamer"}) ``` -------------------------------- ### Flatten IntStreams to IntStream Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/stream-fundamentals/intermediate_operations.html Use flatMapToInt() to flatten a stream of IntStreams into a single IntStream. The provided Function transforms each IntStream before flattening. ```java Stream.of( IntStream.of(1, 2), IntStream.of(3, 4) ) .flatMapToInt(s -> s.map(i -> i + 1)) ``` -------------------------------- ### Enable Parallel Stream Processing Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/stream-fundamentals/intermediate_operations.html Use parallel() to enable elements to be processed by different threads. By default, parallel streams use the default ForkJoinPool. ```java Stream.of("B", "A", "C" , "B") .parallel() ``` -------------------------------- ### Create a Stream from an Entity Class Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/fetching-data/fetching-data.html The simplest way to create a Stream, directly querying the table associated with the given JPA entity. ```java Stream<Film> stream = jpaStreamer.stream(Film.class); __**(1)** ``` -------------------------------- ### Combine Streams with Duplicates (UNION ALL) Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/fetching-data/sql-equivalents.html Use Stream.of() with multiple streams and flatMap(Function.identity()) to combine streams, similar to UNION ALL. Duplicates are preserved. ```java Stream.of( jpaStreamer.stream(Film.class).filter(Film$.length.greaterThan(120)), jpaStreamer.stream(Film.class).filter(Film$.rating.equal("PG-13")) ) .flatMap(Function.identity()) .distinct() // Note: distinct() is applied here in the example, but for UNION ALL it would typically be omitted. .forEachOrdered(System.out::println); ``` -------------------------------- ### Retrieve Numerical Column as IntStream Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/fetching-data/stream-examples.html Use mapToInt to retrieve a single numerical column as an IntStream. Note that this currently retrieves all columns from the database and is planned for optimization. ```java int[] filmLengths = jpaStreamer.stream(Film.class) .mapToInt(Film$.length.asInt()) .toArray(); ``` -------------------------------- ### Flatten LongStreams to LongStream Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/stream-fundamentals/intermediate_operations.html Use flatMapToLong() to flatten a stream of LongStreams into a single LongStream. The provided Function transforms each LongStream before flattening. ```java Stream.of( LongStream.of(1, 2), LongStream.of(3, 4) ) .flatMapToLong(s -> s.map(i -> i + 1)) ``` -------------------------------- ### Register a Close Handler Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/stream-fundamentals/intermediate_operations.html Use onClose() to register an action that will be executed when the stream is closed. This is useful for resource cleanup. ```java Stream.of("B", "A", "C", "B") .onClose( () -> System.out.println("The Stream was closed") ) ``` -------------------------------- ### Add JPAStreamer Dependencies to Gradle Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/get-jpa-streamer/install-gradle.html Include the JPAStreamer core and field generator annotation processor dependencies in your build.gradle file. Replace 'version' with the desired JPAStreamer version. ```gradle repositories { mavenCentral() } dependencies { compile "com.speedment.jpastreamer:jpastreamer-core:version" annotationProcessor "com.speedment.jpastreamer:fieldgenerator-standard:version" } ``` -------------------------------- ### Find Films by Title (Case-Insensitive) Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/predicates/string-predicates.html Finds and prints films where the title exactly matches a given string, ignoring case differences. Useful for flexible searching. ```Java jpaStreamer.stream(Film.class) .filter(Film$.title.equalIgnoreCase("AlABama dEVil")) .forEachOrdered(System.out::println); ``` -------------------------------- ### Inject JPAStreamer via Constructor Injection Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/get-jpa-streamer/spring_integration.html Preferred method for injecting JPAStreamer into a service class using constructor injection. ```java @Service public class DummyService { private final JPAStreamer jpaStreamer; @Autowired public DummyService(final JPAStreamer jpaStreamer) { this.jpaStreamer = jpaStreamer; } } ``` -------------------------------- ### Stream Table Entries Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/quick-start/quick-start.html Streams all entries from the 'Film' entity's corresponding table and prints them to the console. Ensure JPAStreamer is initialized before use. ```java jpaStreamer.stream(Film.class) .forEach(System.out::println); ``` -------------------------------- ### Add JPAStreamer Gradle Dependency Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/get-jpa-streamer/spring_integration.html Add this dependency to your Gradle project's build.gradle file for JPAStreamer autoconfiguration. ```gradle repositories { mavenCentral() } dependencies { compile "com.speedment.jpastreamer.integration.spring:spring-boot-jpastreamer-autoconfigure:version" ``` -------------------------------- ### Find Films by Title (Not Equal, Case-Insensitive) Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/predicates/string-predicates.html Finds and prints films where the title does not match a given string, ignoring case differences. Useful for excluding specific titles. ```Java jpaStreamer.stream(Film.class) .filter(Film$.title.notEqualIgnoreCase("AlABama dEVil")) .forEachOrdered(System.out::println); ``` -------------------------------- ### Configure Maven Compiler Plugin to Process Annotations Only Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/troubleshooting/troubleshooting.html Use this configuration to trigger JPAStreamer's annotation processor without compiling the code, which can help resolve issues after deleting the metamodel. ```xml <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.10.1</version> <configuration> <proc>only</proc> __**(1)** </configuration> </plugin> ``` -------------------------------- ### Combine Predicates with OR Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/predicates/combining-predicates.html Use the `or()` method to create a predicate that represents a logical OR of two predicates. The second predicate is only evaluated if the first one is false. ```java Predicate<Film> isLong = Film$.length.greaterThan(120); Predicate<Film> isPG13 = Film$.rating.equal("PG-13"); jpaStreamer.stream(Film.class) .filter(isLong.or(isPG13)) .forEachOrdered(System.out::println); ``` -------------------------------- ### Sort Films by Multiple Columns Source: https://speedment.github.io/jpa-streamer/jpa-streamer/latest/fetching-data/sql-equivalents.html Composes comparators to sort films by length and then by title. The `.comparator()` method must be used for secondary sort fields. ```java .sorted(Film$.length.thenComparing(Film$.title.comparator()) __**(1)** ```