Persist is a
Java
Java is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea (a part of Pacific Ocean) to the north. With a population of 156.9 million people (including Madura) in mid 2024, proje ...
-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
Structured Query Language (SQL) (pronounced ''S-Q-L''; or alternatively as "sequel")
is a domain-specific language used to manage data, especially in a relational database management system (RDBMS). It is particularly useful in handling s ...
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
Java Database Connectivity (JDBC) is an application programming interface (API) for the Java (programming language), Java programming language which defines how a client may access a database. It is a Java-based data access technology used for Java ...
(including code that already use another
ORM/
DAO tool).
Persist does not require explicit mappings from
POJOs 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
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 primitive types
If a query returns a single column, Persist can map data directly into primitive types (either single values or lists):
// Return customer name as String
String name = persist.read(String.class, "select name from customer where id=?", 55);
// Fetch all customer id's as a list of integers
List ids = persist.readList(Integer.class, "select id from customer");
Custom queries with no returning data
Arbitrary queries that return no data can be easily executed.
// Execute arbitrary SQL with parameters
persist.executeUpdate("delete from customer where id in (?,?)", 10, 20);
See also
*
Hibernate
Hibernation is a state of minimal activity and metabolic reduction entered by some animal species. Hibernation is a seasonal heterothermy characterized by low body-temperature, slow breathing and heart-rate, and low metabolic rate. It is most ...
*
iBATIS
References
{{Reflist
External links
Persist @ GitHub
Articles with example Java code
Java (programming language)