Persist
Encyclopedia
Persist is a Java
-based ORM
/DAO
tool. It provides only the minimal amount of functionalities necessary to map objects or maps from database queries and to statement parameters.
Persist works around a java.sql.Connection object. This means that it does not care about customer query languages (it uses plain SQL
with placeholders, as PreparedStatement objects use), connection pool handling, transaction handling (for the most part), and so on. This also means it is very flexible, and can be integrated with any code that depends on JDBC (including code that already use another ORM
/DAO
tool).
Persist does not require explicit mappings from POJO
s to database tables. As long as there is some sort of naming conventions that relate database names with POJO
names, Persist will require virtually no mappings. It can, however, be instructed to map Java classes and fields to database tables and columns using annotations.
Persist supports several different mapping strategies:
POJOs mapped to tables
By default, if no annotations specify a given class should not be mapped to a table, Persist will try to find a table that matches that class and create a mapping between fields and columns.
// inserts a new customer (the class Customer is mapped to the table customer automatically)
persist.insert(customer);
// reads a customer by its primary key
Customer c = persist.readByPrimaryKey(Customer.class, 42);
// retrieves customers using a custom query (note the usage of varargs)
List list = persist.readList(Customer.class, "select * from customer where id > ?", 10);
// fetch all customers and assign the ResultSet to an Iterator
Iterator allCustomersIterator = persist.readIterator(Customer.class, "select * from customer");
POJOs not mapped to tables
If a class is annotated with @NoTable, Persist will not try to map it to a table, and the class will only be able to hold data produced by queries.
@NoTable
class QueryData {
private int count;
private String concatName;
public long getCount { return count; }
public void setCount(long count) { this.count = count; }
public String getConcatName { return concatName; }
public void setConcatName(String concatName) { this.concatName = concatName; }
}
QueryData qd1 = persist.read(QueryData.class, "select 1 as count, 'hello' as concat_name from dual");
java.util.Map's
Map's can be used to hold data from queries. Persist will convert values returned from the query to Java types. Keys in the table are the names of the columns returned in lower case.
// fetch a customer using a custom query and return the result as a map
Map customerMap = persist.readMap("select * from customer where id=?", 10);
// fetch all customers and result the results as Map instances in a List
List
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...
-based ORM
Object-relational mapping
Object-relational mapping in computer software is a programming technique for converting data between incompatible type systems in object-oriented programming languages. This creates, in effect, a "virtual object database" that can be used from within the programming language...
/DAO
Data Access Object
In computer software, a data access object is an object that provides an abstract interface to some type of database or persistence mechanism, providing some specific operations without exposing details of the database. It provides a mapping from application calls to the persistence layer...
tool. It provides only the minimal amount of functionalities necessary to map objects or maps from database queries and to statement parameters.
Persist works around a java.sql.Connection object. This means that it does not care about customer query languages (it uses plain SQL
SQL
SQL is a programming language designed for managing data in relational database management systems ....
with placeholders, as PreparedStatement objects use), connection pool handling, transaction handling (for the most part), and so on. This also means it is very flexible, and can be integrated with any code that depends on JDBC (including code that already use another ORM
Object-relational mapping
Object-relational mapping in computer software is a programming technique for converting data between incompatible type systems in object-oriented programming languages. This creates, in effect, a "virtual object database" that can be used from within the programming language...
/DAO
Data Access Object
In computer software, a data access object is an object that provides an abstract interface to some type of database or persistence mechanism, providing some specific operations without exposing details of the database. It provides a mapping from application calls to the persistence layer...
tool).
Persist does not require explicit mappings from POJO
Pojo
Pojo may refer to:* Pohja, the Swedish name for the Finnish municipality* POJO, abbreviation of Plain Old Java Object in computer programming...
s to database tables. As long as there is some sort of naming conventions that relate database names with POJO
Pojo
Pojo may refer to:* Pohja, the Swedish name for the Finnish municipality* POJO, abbreviation of Plain Old Java Object in computer programming...
names, Persist will require virtually no mappings. It can, however, be instructed to map Java classes and fields to database tables and columns using annotations.
Persist supports several different mapping strategies:
POJOs mapped to tables
By default, if no annotations specify a given class should not be mapped to a table, Persist will try to find a table that matches that class and create a mapping between fields and columns.
// inserts a new customer (the class Customer is mapped to the table customer automatically)
persist.insert(customer);
// reads a customer by its primary key
Customer c = persist.readByPrimaryKey(Customer.class, 42);
// retrieves customers using a custom query (note the usage of varargs)
List list = persist.readList(Customer.class, "select * from customer where id > ?", 10);
// fetch all customers and assign the ResultSet to an Iterator
Iterator allCustomersIterator = persist.readIterator(Customer.class, "select * from customer");
POJOs not mapped to tables
If a class is annotated with @NoTable, Persist will not try to map it to a table, and the class will only be able to hold data produced by queries.
@NoTable
class QueryData {
private int count;
private String concatName;
public long getCount { return count; }
public void setCount(long count) { this.count = count; }
public String getConcatName { return concatName; }
public void setConcatName(String concatName) { this.concatName = concatName; }
}
QueryData qd1 = persist.read(QueryData.class, "select 1 as count, 'hello' as concat_name from dual");
java.util.Map's
Map's can be used to hold data from queries. Persist will convert values returned from the query to Java types. Keys in the table are the names of the columns returned in lower case.
// fetch a customer using a custom query and return the result as a map
Map
// fetch all customers and result the results as Map instances in a List
List