Skip to content

FirstSteps

Tobias Liefke edited this page Sep 9, 2019 · 13 revisions

First steps with Fastnate

Fastnate currently provides three different parts:

  • The generator itself in fastnate-generator, which takes any JPA annotated entities and generates a SQL file on the fly. Use this one if you want to have full control over the generated SQL file or if you have really a big amount of data that you want to import. We call that the push API, as you have to push your data into Fastnate.
  • The entity importer in fastnate-data, which finds all creators of entities automatically, collects all data and writes the SQL file in the end. Use this if the amount of created entities is small enough to fit into memory at ones and if you would like to start immediately. Thats why we will demonstrate that one first. We call this the pull API, as it asks every provider for the data.
  • Last but not least we've got the fastnate-maven-plugin, which makes it easy to use the entity importer in Maven or Eclipse.

This page will show you a short introduction for using fastnate-data and fastnate-maven-plugin.

Before we can start

We assume that you've got a JPA project already. If you don't - you could use fastnate-examples as blueprint. But remember that this one contains most of the steps already which we will do in the following chapters.

We are using Lombok for generating obvious code snippets like getters, setters and constructors. Even if you don't know Lombok (until now), the used annotations should be self explaining - and you can look up any of them in the feature overview of Lombok. You can replace the annotations with Delombok if you feel that it helps to understand the code.

1. Attach Fastnate to your compile path

Either download and add fastnate-data and fastnate-generator manually or attach fastnate-data to your Maven project - both is described on our download page.

Here is the example of embedding it in your Maven pom.xml:

<dependencies>
	<dependency>
		<groupId>org.fastnate</groupId>
		<artifactId>fastnate-data</artifactId>
		<version>1.4.1</version>
	</dependency>
</dependencies>

2. Define your entity classes

If you haven't created an entity class up to now, here is an example:

package org.fastnate.examples.data;

@Entity
@Getter
@Setter
@NoArgsConstructor
public class Person {

	@Id
	@GeneratedValue
	private Long id;

	private String firstName;

	private String lastName;

	public Person(final String firstName, final String lastName) {
		this.firstName = firstName;
		this.lastName = lastName;
	}
}

3. Build your test data

Implement org.fastnate.data.DataProvider and create you example entities:

package org.fastnate.examples.data;

public class PersonData extends AbstractDataProvider {

	@Getter
	private final List<Person> entities = new ArrayList<>();

	@Override
	public void buildEntities() throws IOException {
		this.entities.add(new Person("Nate", "Smith"));
		this.entities.add(new Person("Natalie", "Smith"));
	}
}

4. Start the generation

Run org.fastnate.data.EntityImporter. Either with a "hand made" classpath or with Maven:

<build>
	<plugins>
		<plugin>
			<groupId>org.fastnate</groupId>
			<artifactId>fastnate-maven-plugin</artifactId>
			<version>1.4.1</version>
			<executions>
				<execution>
					<goals>
						<goal>import-data</goal>
					</goals>
					<configuration>
						<packages>org.fastnate.examples.data</packages>
					</configuration>
				</execution>
			</executions>
		</plugin>
	</plugins>
</build>

Further configuration parameters can be found in the documentation of the maven plugin.

5. Finished

If everything succeeds you will find a new file data.sql - either in your target/classes directory (in Maven) or in your working directory (if you started the EntityImporter manually):

INSERT INTO Person (firstName, lastName) VALUES ('Nate', 'Smith');
INSERT INTO Person (firstName, lastName) VALUES ('Natalie', 'Smith'); 

To go on further read the How-tos or FAQs.

You can’t perform that action at this time.