Skip to content
Tobias Liefke edited this page Feb 15, 2017 · 6 revisions

This page shows complex examples for working with Fastnate. The full source code can be found in the fastnate-examples module

For easier questions you should check the FAQ. If you have any other question, don't hesitate to ask (for example here)

How to import data with a DataProvider

When using the fastnate-data module it is very easy to create the entities that will added to a data.sql.

public class PersonData extends AbstractDataProvider {

	private final OrganisationData organisations;

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

	/** Creates the example persons. */
	public void buildEntities() throws IOException {
		final Person nate = new Person("Nate", "Smith");

		final Person john = new Person("John", "Doe");

The referenced OrganisationData class can be found below in the How to import data from CSV files section.

We can reference any data that was created by the injected DataProviders, as buildEntities of the different DataProviders is called in the order of their creation. The buildEntities of any injected DataProvider is always called, before my buildEntities.

In the above example we don't need to add nate to the entities, as john references nate and all references are persisted as well.

How to import data from CSV files

Data from CSV files can be read by extending the AbstractCsvDataProvider:

public class OrganisationData extends AbstractCsvDataProvider<Organisation> {

	public OrganisationData(final File importPath) {
		super(new File(importPath, "organisations.csv"));


As long as the values in the CSV file are separated by ; and the header line contains the property names for every column, there is nothing more to do.

To map a column to a different property, to use a different converter or to ignore a column completely, you can configure the mapping in the constructor:

public OrganisationData(final File importPath) {
	super(new File(importPath, "organisations.csv"));

	// Map the "web" column to the "url" property
	addColumnMapping("web", "url");

	// Add a currency converter for the profit column
	addConverter("profit", new CsvFormatConverter<Float>(NumberFormat.getCurrencyInstance(Locale.ENGLISH)));

	// Add a entity lookup for the parent column
	addConverter("parent", CsvMapConverter.create(this.byName));
	// Ignore a specific column

	// Or ignore all unknown columns

To change the encoding or separator of the CSV files, you will need to overwrite openCsvListReader:

protected CsvListReader openCsvListReader(final File importFile) throws IOException {
	return new CsvListReader(new InputStreamReader(new FileInputStream(importFile), "your charset"), CsvPreference.EXCEL_PREFERENCE));
You can’t perform that action at this time.