Getting Started
To help you configure your project, Speedment provides a project Initializer. Once you fill out the details of your project, it provides you with a zip-file containing apom.xml
with the needed dependencies and a Main.java
starter.
The Speedment Initializer can be used to configure a Speedment project.
Once you have clicked “download”, unzip the file and open the project in your IDE as a Maven project. In IntelliJ, the easiest way to do that is to choose File -> Open and then select the pom.xml-file in the unzipped project folder.
If you rather want to use Speedment in an existing project, configure your project via the Initializer to make sure you get all needed dependencies. Then simply merge the provided pom.xml with your current one and reimport Maven.
As you may recall from the previous articles, Speedment relies on an automatically generated Java domain model. Hence, before we can write our application, we need to generate the required classes. This is done using the Speedment Tool which is started by running mvn speedment:tool
in the terminal or by running the same target via the IDE:s built-in Maven menu.
Firstly, you will be asked to register for a free license and connect to your database. A free license can be used for all open-source databases (unlimited use) and commercial databases (up to 500 MB and doesn’t require any billing information).
A free license can be used with all open-source databases (unlimited) and commercial databases (up to 500 MB and does not require billing information.)
Once you complete the registration, you will be asked to provide credentials for your database (make sure you selected the correct DB-type in the initializer). Either use a local database of your own or run some tests with the Sakila database we used in the exercises.
Sakila Database Credentials
Type: MariaDB
Host: 35.203.190.83
Port: 3306
Database name: sakila
User: sakila
Password: sakila
Fill out the database credentials to connect to your data source. (Note: Speedment never stores your database password).
A click on the “Connect”-button will launch the Speedment Tool. It presents the database structure to the left-hand side and settings for the selected table or column on the right-hand side. In this case, the default settings are sufficient meaning we can go ahead and press “Generate” (If your application doesn’t require all the tables and/or columns you can disable these before generating).
The Speedment Tool visualizes the data structure and allows customizations of the generated code.
Next, Speedment will analyze the database metadata and generate the entire Java domain model. Once this process is completed you are ready to write your application. If you check out the Main.java-file, you will find a project starter containing something like this:
public class Main { public static void main(final String... args) { Speedment app = new MyApplicationBuilder() .withUsername("your-dbms-username") .withPassword("your-dbms-password") .build(); app.stop(); } }From here, you are ready to build your application using the examples we have provided in the previous articles. Thereby, we can close the circle by fetching a Manager for the Film table (a handle to the content of the film table) by typing:
FilmManager films = app.getOrThrow(FilmManager.class);Using the Manager we can now query our connected database as we have shown:
List<Film> filmsTitleStartsWithA = films.stream() .filter(Film.TITLE.startsWith("A")) .sorted(Film.LENGTH) .collect(Collectors.toList()); filmsTitleStartsWithA: [ FilmImpl { filmId=15, title=ALIEN CENTER, …, rating=NC-17, length = 46, FilmImpl { filmId=2, title=ACE GOLDFINGER, …, rating=G, length = 48, … ]
Exercises
This week there is no associated GitHub repo for you to play with. Instead, we encourage you to integrate Speedment in a new or an existing database application to try out your newly acquired skills.Extra Exercise
When you are ready with your project, we encourage you to try out HyperStream, especially if you have a large database and want to increase the reading performance. HyperStream goes beyond Stream and adds in-JVM-memory capabilities which boost application speed by orders of magnitude. You only need to add a few lines of code in your existingpom.xml
and your Main.java
file:
.withBundle(InMemoryBundle.class) // add to the app builder ... // Load data from database into materialized view app.getOrThrow(DataStoreComponent.class) .load();
Read more in the user-guide. The Stream API remains the same but performance is vastly increased.
Conclusion
During the past six weeks, we have demonstrated the usefulness of the Java Stream API and how it can be leveraged for writing type-safe database applications in pure Java. If you wish to learn more about Speedment, check out the user guide which also contains a more thorough guide on Java Streams.Lastly - thank you for taking interest in our article series, it has been truly great to see that many of you have been following along with the provided exercises. Happy coding!