Fetching Data
To start fetching data with JPAstreamer you need to initialize an instance of JPAStreamer
. This section describes how that is accomplished.
Obtaining a JPAstreamer instance
JPAstreamer is initialized with the name of the persistence unit like so:
JPAStreamer jpaStreamer = JPAStreamer.of("sakila"); (1)
1 | "sakila" is to be replaced with the name of your persistence unit that can be found in a configuration-file |
In the example, the String "sakila" should refer to the name of your persistance unit. Assuming you are already using a JPA provider, your project should contain an XML-file like the one below, describing the persistence unit:
<?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"> (1)
<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>
1 | The name of the persistence unit, in this case "sakila", is used to initialize JPAstreamer. |
This configuration is just an example configuration for the MySQL Sakila example database. You should use the configuration you already have in place. |
If you have multiple persistence units, you can initiate several instances of JPAStreamer to establish connections with different sources.
|
JPAstreamer does not need any additional configuration and depends solely on this file to establish a database connection. If your starting a project from scratch, make sure to set up your JPA project before trying to use JPAstreamer.
Having obtained a JPAStreamer
instance, you are ready to go. Here is an example that includes both the instantiation and the querying:
public static void main(String[] args) {
JPAStreamer jpaStreamer = JPAStreamer.createJPAStreamerBuilder("sakila") (1)
.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);
}
The next section demonstrates use of the available Stream operators and how they map to SQL constructs.