Module org.panteleyev.mysqlapi

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 TableRecord.

API currently supports the following primary key types:

  • Integer, int
  • Long, long
  • String
  • UUID

In case of Integer one may use MySqlClient.generatePrimaryKey(java.lang.Class<? extends org.panteleyev.mysqlapi.TableRecord<K>>) to generate unique values for the appropriate table classes. Also make sure that application calls MySqlClient.preload(java.util.Collection<java.lang.Class<? extends org.panteleyev.mysqlapi.TableRecord>>) first.

Deserialization

API supports two ways of object deserialization: by constructor and direct field assignment. Constructor must be used for objects with final fields or in case of additional initialization.

Field Assignment

There must be no-argument constructor, either default or explicit. Setters are not used, i.e. there is no way to define additional deserialization logic in case of field assignment.


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

    public int getId() {
        return id;
    }

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

    public String getTitle() {
        return title
    }

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

Constructor

Constructor deserialization is triggered by RecordBuilder as shown below. Such constructor must have parameters corresponding to table columns.


@Table("book")
class Book implements TableRecord {
    @PrimaryKey
    @Column(Field.ID)
    private final int id;
    @Column("title")
    private final String title;

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

    public int getId() {
        return id;
    }

    public String getTitle() {
        return title;
    }
}
 

Java Records

Java records deserialization is always performed by means of canonical constructor. RecordBuilder annotation is ignored even if it is present on any of the available constructors.

All record components must be annotated with Column, i.e., record must be an exact representation of the database table.


@Table("book")
record Book(
    @PrimaryKey
    @Column(Field.ID)
    int id,
    @Column("title")
    String title
) implements TableRecord {
}
 

Data Types

The following data types are supported:

Data Types
JavaMySQLComment
int
Integer
INTEGER
long
Long
BIGINT
boolean
Boolean
BOOLEAN
String VARCHAR ( Column.length() )
String with Column.isJson() = true JSON
BigDecimal DECIMAL ( Column.precision(), Column.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 BIGINT Dates are stored as long using Date.getTime()
LocalDate BIGINT Local dates are stored as long using LocalDate.toEpochDay()
byte[] VARBINARY ( Column.length() )
UUID BINARY(16) or VARCHAR(36) depending on Column.storeUuidAsBinary() The following conversion functions are used between string and binary representation: BIN_TO_UUID() and UUID_TO_BIN()

Indexes and Foreign Keys


@Table("parent_table")
public class ParentTable implements TableRecord {
    @Column("data")
    @Index(value = "data", unique = true)
    private String data;

    public String getData() {
        return data;
    }
}
 

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


@Table("child_table")
public class ChildTable implements TableRecord {
    @Column("parent_data")
    @ForeignKey(table = ParentTable.class, column = "data",
        onDelete = ReferenceOption.RESTRICT, onUpdate = ReferenceOption.CASCADE)
    private final String parentData;

    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
  • Packages

    Package Exported To Modules Opened To Modules Description
    org.panteleyev.mysqlapi All Modules All Modules
    This package defines Java API for MySQL.
    org.panteleyev.mysqlapi.annotations All Modules All Modules
    This package defines annotations applied to Java classes implementing database table records.