Skip to content

Latest commit

 

History

History
183 lines (140 loc) · 7.78 KB

getting-started.md

File metadata and controls

183 lines (140 loc) · 7.78 KB

Getting started

Nova 2.37.0 is available from Maven Central. Nova runs on Java 17 and newer runtimes; the CI matrix verifies Temurin 17, 21, 25, and 26. Use the Gradle wrapper for development; Gradle 9.4 or newer is required when launching the build on Java 26. Entities are mapped with the standard jakarta.persistence annotations (Nova exports jakarta.persistence-api transitively); the move off the 1.x io.nova.annotation clones happened in the breaking 2.0.0 release. Everything since 2.0.0 has been additive: composite keys (@EmbeddedId / @IdClass, including as relation targets), the full relationship set (@OneToOne / @OneToMany / @ManyToMany / @ElementCollection), inheritance, @Convert, a reactive EntityManager, JPQL and the Criteria API, @NamedQuery / @NamedEntityGraph / @SqlResultSetMapping / @StoredProcedureQuery, and a 2nd-level cache. Reactive equivalents return Mono / Flux rather than blocking. See JPA compatibility for the full support matrix and Entities for the annotation reference.

1. Add dependencies

The fastest path is the aggregate module io.github.bssm-oss:nova plus an R2DBC driver for the database you target. The aggregate pulls in core, the R2DBC adapter, and every bundled dialect (PostgreSQL / MySQL / MariaDB / H2 / Oracle).

// build.gradle.kts
repositories {
    mavenCentral()
}

dependencies {
    implementation("io.github.bssm-oss:nova:2.37.0")

    // The R2DBC driver for your database (pick one)
    runtimeOnly("io.r2dbc:r2dbc-h2:1.0.0.RELEASE")
    // runtimeOnly("org.postgresql:r2dbc-postgresql:1.0.7.RELEASE")
    // runtimeOnly("io.asyncer:r2dbc-mysql:1.3.0")
}

To pull in only a specific dialect instead of the aggregate, depend on nova-core + nova-r2dbc + the dialect module directly:

// build.gradle.kts
dependencies {
    implementation("io.github.bssm-oss:nova-core:2.37.0")
    implementation("io.github.bssm-oss:nova-r2dbc:2.37.0")
    implementation("io.github.bssm-oss:nova-dialect-postgresql:2.37.0")
    runtimeOnly("org.postgresql:r2dbc-postgresql:1.0.7.RELEASE")
}

Groovy DSL:

// build.gradle
dependencies {
    implementation 'io.github.bssm-oss:nova:2.37.0'
    runtimeOnly 'io.r2dbc:r2dbc-h2:1.0.0.RELEASE'
}

Source builds: the development default is 2.37.0-SNAPSHOT. Build from source with that version or use the Central snapshots repository.

repositories {
    mavenCentral()
    maven("https://central.sonatype.com/repository/maven-snapshots/")
}

2. Define an entity

@Entity
@Table(name = "accounts")
public class Account {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "email_address")
    private String email;

    @Column(nullable = false)
    private boolean active;

    public Account() {}

    public Account(Long id, String email, boolean active) {
        this.id = id;
        this.email = email;
        this.active = active;
    }

    public Long getId() { return id; }
    public String getEmail() { return email; }
    public void setEmail(String email) { this.email = email; }
    public boolean isActive() { return active; }
    public void setActive(boolean active) { this.active = active; }
}

For the full annotation reference, see Entities.

3. Run operations

Nova.create(connectionFactory) reads the R2DBC driver metadata to auto-detect the dialect (PostgreSQL / MySQL / MariaDB / H2 / Oracle) and assembles a ReactiveEntityOperations. For drivers that are not auto-mapped, inject a dialect explicitly with Nova.create(cf, dialect).

import io.nova.Nova;
import io.nova.core.ReactiveEntityOperations;
import io.r2dbc.spi.ConnectionFactories;
import io.r2dbc.spi.ConnectionFactory;

ConnectionFactory cf = ConnectionFactories.get(
        "r2dbc:h2:mem:///nova-smoke?options=DB_CLOSE_DELAY=-1");
ReactiveEntityOperations operations = Nova.create(cf);

operations.save(new Account(null, "user@example.com", true))
          .flatMap(saved -> operations.findById(Account.class, saved.getId()))
          .subscribe(System.out::println);

4. Initialize the schema (optional)

In production, prefer a migration tool such as Flyway or Liquibase. For integration tests and demo seeding, Nova.schemaInitializer(cf) returns a one-liner that auto-detects the dialect and issues idempotent DDL:

import io.nova.Nova;
import io.nova.schema.SchemaInitializer;

SchemaInitializer schema = Nova.schemaInitializer(cf);

schema.create(Account.class)
      .then(operations.save(new Account(null, "user@example.com", true)))
      .block();

By default, statements are emitted as CREATE TABLE IF NOT EXISTS so re-running the bootstrap is safe. This also preserves existing @TableGenerator counters: only a missing generator row is seeded, so a restart never reuses identifiers. Multiple @TableGenerator declarations can share one physical generator table when they use the exact same pkColumnName and valueColumnName; give each declaration a distinct pkColumnValue so it owns a separate counter row. Reusing the same row is accepted only for identical complete definitions. Conflicting column layouts or row definitions are rejected before create or recreate emits DDL. Pass SchemaOptions.defaults().withIfNotExists(false) to force a raw CREATE TABLE instead.

Batch and lifecycle variants:

schema.create(Author.class, Book.class);        // emits parent then child
schema.drop(Book.class, Author.class);          // drops child then parent
schema.recreate(Author.class, Book.class);      // drop + recreate, FK-safe ordering

For lower-level control, the raw dialect.schemaGenerator() DDL strings stay available — see Dialects & Schema.

With Spring Boot

In a Spring Boot application, the nova-spring-boot-starter reads your ConnectionFactory bean, auto-detects the dialect, registers ReactiveEntityOperations, and discovers ReactiveCrudRepository interfaces below the @SpringBootApplication package. No explicit Nova.create(...) call or @EnableNovaRepositories annotation is needed. The starter also integrates Nova operations with Spring's reactive @Transactional, so an error or cancellation from an annotated Mono/Flux rolls the shared R2DBC transaction back.

The starter also exposes a SchemaInitializer bean and supports JPA-style nova.ddl-auto configuration. Set nova.ddl-auto=create (or create-drop) and the starter scans for @Entity classes and provisions the schema on startup — perfect for integration tests:

nova:
  ddl-auto: create-drop
  entity-packages: com.example.domain   # optional; defaults to @SpringBootApplication's package

The same effective entity packages are the only packages scanned for Jakarta @Converter classes. The starter registers every discovered converter before it preloads any entity metadata, so @Converter(autoApply = true) applies deterministically at startup. A converter outside those packages is not registered merely because it is on the classpath. Standalone Nova.create(connectionFactory) also does no classpath scan: register an auto-apply converter on an EntityMetadataFactory before requesting any entity metadata, then use that factory in your manually assembled operations. An explicit @Convert(converter = ...) does not require registration.

See Spring for the full property reference.

Next steps

  • Entities — annotations, relationship mapping, composite types, indexes
  • Queries — Query DSL, Updater, Projection, Aggregations, pagination
  • Transactions — propagation, pessimistic locking, retry
  • Spring — Spring Boot starter, Spring Data-style repositories