v.persistence.19.1.0.source-code.module-info Maven / Gradle / Ivy
Show all versions of persistence Show documentation
import org.panteleyev.persistence.annotations.Column;
import org.panteleyev.persistence.annotations.Table;
import org.panteleyev.persistence.annotations.RecordBuilder;
import org.panteleyev.persistence.Record;
import org.panteleyev.persistence.DAO;
/**
Persistence library provides annotation classes for database access.
Database
Database manipulation is beyond the scope of this API. Calling code must supply correct {@link javax.sql.DataSource}
and ensure database does exist and proper access control is established.
Table
Class implementing database table is defined by the annotation {@link Table}.
Such class must also implement interface {@link Record}.
API currently supports the following primary key types:
- {@link java.lang.Integer}, int
- {@link java.lang.Long}, long
- {@link java.lang.String}
- {@link java.util.UUID}
In case of {@link java.lang.Integer} one may use {@link DAO#generatePrimaryKey} to generate unique values for the
appropriate table classes. Also make sure that application calls {@link DAO#preload} 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.
{@literal @}Table("book")
class Book implements Record {
{@literal @}PrimaryKey
{@literal @}Column(Column.ID)
private int id;
{@literal @}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 {@link RecordBuilder}
as shown below. Such constructor must have parameters corresponding to table columns.
{@literal @}Table("book")
class Book implements Record {
{@literal @}PrimaryKey
{@literal @}Column(Field.ID)
private final int id;
{@literal @}Column("title")
private final String title;
{@literal @}RecordBuilder
public Book ({@literal @}Column(Column.ID) int id, {@literal @}Column("title") String title) {
this.id = id;
this.title = title;
}
public int getId() {
return id;
}
public String getTitle() {
return title
}
}
Data Types
The following data types are supported:
Data Types
Java SQLite MySQL Comment
int
{@link java.lang.Integer} INTEGER INTEGER
long
{@link java.lang.Long} INTEGER BIGINT
boolean
{@link java.lang.Boolean} BOOLEAN BOOLEAN
{@link java.lang.String}
VARCHAR ( {@link Column#length} )
VARCHAR ( {@link Column#length} )
{@link java.lang.String} with {@link Column#isJson()} = true
BLOB
JSON
{@link java.math.BigDecimal}
VARCHAR ( {@link Column#precision} + 1 )
DECIMAL ( {@link Column#precision}, {@link Column#scale} )
MySQL representation does not guarantee that retrieved value will be equal to original one by means of
{@link java.lang.Object#equals}. Use {@link java.math.BigDecimal#compareTo} instead.
{@link java.util.Date}
INTEGER
BIGINT
Dates are stored as long using {@link java.util.Date#getTime}
{@link java.time.LocalDate}
INTEGER
BIGINT
Local dates are stored as long using {@link java.time.LocalDate#toEpochDay}
byte[]
BLOB
VARBINARY ( {@link Column#length} )
{@link java.util.UUID}
VARCHAR(36)
BINARY(16)
For MySQL the following conversion functions are used between string and binary representation:
BIN_TO_UUID()
and UUID_TO_BIN()
Indexes and Foreign Keys
{@literal @}Table("parent_table")
public class ParentTable implements Record {
{@literal @}Column("data")
{@literal @}Index(value = "data", unique = true)
private String data;
public String getData() {
return data;
}
}
This will produce the following SQL for indexed field:
CREATE UNIQUE INDEX data ON parent_table(data)
{@literal @}Table("child_table")
public class ChildTable implements Record {
{@literal @}Column("parent_data")
{@literal @}ForeignKey(table = ParentTable.class, field = "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 MySQL Data Types
*/
open module org.panteleyev.persistence {
requires java.base;
requires java.sql;
requires java.desktop;
requires java.naming;
exports org.panteleyev.persistence;
exports org.panteleyev.persistence.annotations;
}