Using Speedment
Maven project creation
File > New > Project… > Java Project
Right-click the project node in Project Explorer select Configure > Convert to Maven Project.
Leave group and artifact Ids as they are.
Edit pom.xml so that the following text is in inserted after the <version> tag :
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<h2.version>1.4.199</h2.version>
<postgresql.version>42.2.5</postgresql.version>
<speedment.version>3.1.16</speedment.version>
</properties>
<dependencies>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>${postgresql.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>${h2.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.speedment.enterprise</groupId>
<artifactId>application</artifactId>
<version>${speedment.version}</version>
</dependency>
</dependencies>
Publicité
<build>
<plugins>
<plugin>
<groupId>com.speedment.enterprise</groupId>
<artifactId>speedment-enterprise-maven-plugin</artifactId>
<version>${speedment.version}</version>
<configuration>
<components>
<component>com.speedment.enterprise.datastore.tool.DataStoreToolBundle</component>
</components>
<appName>${project.artifactId}</appName>
<packageName>${project.groupId}</packageName>
</configuration>
<dependencies>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>${postgresql.version}</version>
<scope>runtime</scope>
[email protected] © 2019
CC BY-NC-ND
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>${h2.version}</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<repositories>
Publicité
<repository>
<id>speedment-enterprise</id>
<name>Speedment Enterprise Repositories</name>
<url>https://repo.speedment.com/nexus/content/repositories/releases/</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>speedment-enterprise</id>
<name>Speedment Enterprise Repositories</name>
<url>https://repo.speedment.com/nexus/content/repositories/releases/</url>
</pluginRepository>
</pluginRepositories>
Notice the dependency on H2 (which is declared twice). The dependency must be version 1.4.199 or
greater (not version 197 for instance).
Copy the H2 database file into the project folder (next to the pom.xml). Under Eclipse, refresh the
project node (F5 or « Refresh » in the context menu). The database file has the extension .mv.db.
Generation of the Speedment object classes
Now right-click the project node in Project Explorer view and select ‘Run As’ > ‘Maven Build…’
NOTE : make sure you select « Maven Build… », not « Maven Build ». Notice the « … » after « Maven
Build » !
This dialog box will show up :
[email protected] © 2019
CC BY-NC-ND
In the Goals field, enter ‘speedment:tool’ and click Run.
In the Console view of Eclipse, a lot of files will be downloaded. This will take 1-2 minutes.
This dialog will eventually open :
Change it to :
[email protected] © 2019
CC BY-NC-ND
That is :
Database Type : PostgreSQL
Username : sa
Publicité
Database and Schema Names : use the name of the file, in uppercase. (This name is the name of the
CATALOG in Data Source Explorer when you create a new connection towards this database).
Use Connection String : « h2database » is the name of the file (extension .mv.db), SCHEMA=<name>
is the name of the SCHEMA. Set this to H2DATABASE.
To sum up : in the example above :
H2 database filename on disk : ../fc.Database.Northwind/northwind
Please enter a RELATIVE file name here, not absolute.
Typically, the database file is somewhere in your workspace directory and can reliably be
accessed with a relative file path. In the example here, the database file is located in a
directory located next to the project folder.
Database Name : NORTHWIND (file name in upper case, with no extension)
Schema : SCHEMA=NORTHWIND
The final connection string is
jdbc:h2:../fc.Database.Northwind/northwind;SCHEMA=NORTHWIND;AUTO_SERVER=TRU
E
[email protected] © 2019
CC BY-NC-ND
Click Connect.
This dialog will now show up :
Quickly check that the database is correctly recognized by the tool, and the tables are indeed listed in
the left pane.
Click Generate. This generates a DatabaseApplication\src\main\json\speedment.json file as well as
all java class files for your database.
There may be NoClassDefFound exception (red console output) when you close the Speedment
generation tool window. This is normal.
When the database schema changes
If your database schema changes, you will need to regenerate the files by using
speedment:generate as a Maven goal.
To recreate the Speedment connection from scratch, delete speedment.json and restart the process
from the beginning.
Writing sample code
You can now create your main application class. You can paste the following source code to connect
to the database and perform a simple Java Stream API query :
Publicité
public static void main(String... params)
{
H2DatabaseApplication app = new
H2DatabaseApplicationBuilder().build();
SuppliersManager suppliers = app.getOrThrow(SuppliersManager.class);
Optional<String> lastName = suppliers.stream()
.filter(Suppliers.FIRST_NAME.equal("Cornelia"))
.map(Suppliers.LAST_NAME)
[email protected] © 2019
CC BY-NC-ND
.findAny();
if (lastName.isPresent())
System.out.println("Present");
else
}
System.out.println("Not present");
When creating a new H2 database
Only read this section if you plan to create your own H2 database for use with Speedment.
When creating the H2 database, if your database file is called ‘mydatabase.mv.db’, make sure that
the SQL script creating the database tables creates everything in the MYDATABASE schema :
create schema if not exists YOURDATABASE;
SET SCHEMA YOURDATABASE;
CREATE TABLE ...
By default, H2 creates a schema called PUBLIC, which Speedment won’t be able to connect to easily.
It is best to have the filename == database name == schema name. It is then possible to specify the
SCHEMA to connect to with the JDBC connection string, as we did it in the section above.
You can check the database file name and SCHEMA using the Data Source Explorer.
[email protected] © 2019
CC BY-NC-ND