Back to mapping overview

Programmatic DTOs

Use programmatic registration to to map custom DTOs to tables.

Using DTOs as entities

Litebridge allows the use of raw/unaltered DTO classes as entities; these can be typical Java beans with getters/setters or custom POJOs.

Imports
Person.java / Account.java
// Person.java
public class Person {
    private Long id;
    private String name;
    private String surname;
    private int age;
    private List<Account> accounts;

    public Long getId() { return id; }
    public void setId(Long id) { this.id = id; }

    public String getName() { return name; }
    public void setName(String name) { this.name = name; }

    public String getSurname() { return surname; }
    public void setSurname(String surname) { this.surname = surname; }

    public int getAge() { return age; }
    public void setAge(int age) { this.age = age; }

    public List<Account> getAccounts() { return accounts; }
    public void setAccounts(List<Account> accounts) { this.accounts = accounts; }
}

// Account.java
public class Account {
    private Long id;
    private String accountNumber;
    private BigDecimal balance;
    private Person owner;

    public Long getId() { return id; }
    public void setId(Long id) { this.id = id; }

    public String getAccountNumber() { return accountNumber; }
    public void setAccountNumber(String accountNumber) { this.accountNumber = accountNumber; }

    public BigDecimal getBalance() { return balance; }
    public void setBalance(BigDecimal balance) { this.balance = balance; }

    public Person getOwner() { return owner; }
    public void setOwner(Person owner) { this.owner = owner; }
}

Mapping DTOs to tables

DTO classes are registered using the register method and specifying the table, column and relationship mapping using a fluent API.

RegistrationExample.java
import org.example.dto.Person;
import org.example.dto.Account;
// Map the Person class' to the "LB.PERSON" table
// with the "id" field mapped to the "PERSON_ID" column and auto-generated using a sequence
// and the "accounts" relationship mapped to the "LB.ACCOUNT" table.
litebridge.register(Person.class, rc -> rc.mapToTable("LB.PERSON")
    .with(spec -> spec.mapField("id").toColumn("PERSON_ID")
        .generateUsingSequence("LB.PERSON_SEQ"))
    .with(spec -> spec.mapField("name").toColumn("FIRST_NAME"))
    .with(spec -> spec.mapField("accounts")
        .oneToMany(c -> c.mappedByField("owner"))));

// Map the Account class's fields to the "LB.ACCOUNT" table
// with the "id" field mapped to the "ACCOUNT_ID" column and auto-generated using a sequence
// and the "owner" relationship mapped to the "LB.PERSON" table, joined on shared "PERSON_ID" column
litebridge.register(Account.class, rc -> rc.mapToTable("LB.ACCOUNT")
    .with(spec -> spec.mapField("id").toColumn("ACCOUNT_ID").generateUsingSequence("LB.ACCOUNT_SEQ"))
    .with(spec -> spec.mapField("name").toColumn("ACCOUNT_NAME"))
    .with(spec -> spec.mapField("balance").toColumn("BALANCE"))
    .with(spec -> spec.mapField("owner").toColumn("PERSON_ID").joinUsing()));