Skip navigation links

org.panteleyev:persistence 3.1.0 API

Persistence library provides annotation classes for database access.

See: Description

Packages 
Package Description
org.panteleyev.persistence
This package defines persistence API.
org.panteleyev.persistence.annotations
This package defines annotations applied to Java classes implementing database table records.
Persistence library provides annotation classes for database access.

Database

Database manipulation is beyond the scope of this API. Calling code must supply correct DataSource and ensure database does exist and proper access control is established.

Table

Class implementing database table is defined by the annotation Table. Such class must also implement interface Record and at least method Record.getId().

API currently supports only integer as primary key type. Use generatePrimaryKey(java.lang.Class<? extends org.panteleyev.persistence.Record>) to generate unique values for each table class. Also make sure that application calls preload(java.util.Collection<java.lang.Class<? extends org.panteleyev.persistence.Record>>) first.

Mutable Objects

Mutable objects must implement appropriate setters following JavaBean specification.


@Table("book")
class Book implements Record {
    private int id;
    private String title;

    @Field(value = Field.ID, primaryKey = true)
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Field("title")
    public String getTitle() {
        return title
    }

    public void setTitle(String title) {
        this.title = title;
    }
}

Immutable Objects

Immutable objects are supported by annotation RecordBuilder as shown below.


@Table("book")
class Book implements Record {
    private final int id;
    private final String title;

    @RecordBuilder
    public Book (@Field(Field.ID) int id, @Field("title") String title) {
        this.id = id;
        this.title = title;
    }

    @Field(value = Field.ID, primaryKey = true)
    public int getId() {
        return id;
    }

    @Field("title")
    public String getTitle() {
        return title
    }
}

Data Types

The following data types are supported:

JavaSQLiteMySQLComment
int
Integer
INTEGERINTEGER
long
Long
INTEGERBIGINT
bool
Boolean
BOOLEANBOOLEAN
String VARCHAR ( Field.length() ) VARCHAR ( Field.length() )
BigDecimal VARCHAR ( Field.precision() + 1 ) DECIMAL ( Field.precision(), Field.scale() ) MySQL representation does not guarantee that retrieved value will be equal to original one by means of Object.equals(java.lang.Object). Use BigDecimal.compareTo(java.math.BigDecimal) instead.
Date INTEGER BIGINT Dates are stored as long using Date.getTime()

Indexes and Foreign Keys


@Table("parent_table")
public class ParentTable implements Record {
    // ...

    private String data;

    @Field("data")
    @Index(value = "data", unique = true)
    public String getData() {
        return data;
    }
}

This will produce the following SQL for indexed field:
CREATE UNIQUE INDEX data ON parent_table(data)


@Table("child_table")
public class ChildTable implements Record {
    // ...

    @Field("parent_data")
    @ForeignKey(table = ParentTable.class, field = "data",
            onDelete = ReferenceOption.RESTRICT, onUpdate = ReferenceOption.CASCADE)
    public String getParentData() {
        return parentData;
    }
}

This will produce the following SQL for the foreign key:
CREATE FOREIGN KEY(parent_data) REFERENCES parent_table(data) ON DELETE RESTRICT ON UPDATE CASCADE

See Also:
MySQL Data Types, Java Beans Specification
Skip navigation links

Copyright © 2017, Petr Panteleyev