Code Examples

Explore code snippets and patterns for working with Litebridge in Java.

Setting up Litebridge from scratch?

Check out the Getting Started guide for Maven dependencies, provider setup, and project configuration.

Get Started

Browse Examples

Core Concepts

Litebridge's API has 3 basic modes of operation:

  • DTO mode: Full ORM support. Queries return mapped DTOs/entities, and operations are executed against registered DTO/entity metadata. Provides type safety using Litebridge metamodels, but can be used without.
  • SQL mode: Operations are executed directly against tables and columns using the same fluent API; no DTO mapping required.
  • Native SQL: Direct execution of native SQL statements with support for positional or named bind parameters.

Example setup

The examples in this documentation use common imports and initialisation code:

ExampleBase.java
import javax.sql.DataSource;

import org.litebridge.orm.Litebridge;
import org.litebridge.db.h2.H2DatabaseProvider;
import org.litebridge.db.spi.Row;

// Example entities and metamodels
import org.example.dto.Person;
import org.example.entity.Account;
import org.example.entity.meta.PersonMeta;
import org.example.entity.meta.AccountMeta;
// Assuming "dataSource" is a valid javax.sql.DataSource instance
Litebridge litebridge = Litebridge.withDatabaseProvider(
        new H2DatabaseProvider(), dataSource)
    .build();