Skip to main content

Custom mappers

There are two ways to write a mapper for your own class: build it by hand and register it, or mark a method or constructor of the class with @MappingProvider.

The examples map this class:

public class User {
private final int id;
private final String name;

public User(int id, String name) {
this.id = id;
this.name = name;
}
}

Build a mapper​

RowMapper<User> mapper = RowMapper.forClass(User.class)
.mapper(row -> new User(row.getInt("id"), row.getString("name")))
.addColumns("id", "name")
.build();

registry.register(mapper);

addColumn and addColumns name the columns the mapper needs. A mapper without columns is a wildcard, see How SADU picks a mapper.

You can register several mappers for the same class, each with its own set of columns. For a result with id and name, a mapper that needs both wins over one that only needs id.

Annotations​

Instead of registering a mapper, mark a static method or a constructor with @MappingProvider and list the columns it needs. SADU finds it the first time you call mapAs for that class, or when you call registry.register(User.class).

The method takes no arguments and returns a RowMapping for the class.

public class User {
private final int id;
private final String name;

public User(int id, String name) {
this.id = id;
this.name = name;
}

@MappingProvider({"id", "name"})
public static RowMapping<User> map() {
return row -> new User(row.getInt("id"), row.getString("name"));
}
}

A class can have several annotated methods and constructors. Each of them becomes one mapper.

SADU calls the method or constructor through reflection:

  • Make the class and the annotated method or constructor public.
  • With JPMS, the package of the class has to be exported to (or opened for) de.chojo.sadu.mapper.

If the annotated constructor throws, SADU throws an InvalidMappingException with the exception of the constructor as its cause.