Persist (Java tool)

Last updated
Persist
Operating system Cross-platform
Type Persistence Framework
License GNU Lesser General Public License
Website https://code.google.com/p/persist/

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.

Contents

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 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 keyCustomerc=persist.readByPrimaryKey(Customer.class,42);// Retrieves customers using a custom query (note the usage of varargs)Listlist=persist.readList(Customer.class,"select * from customer where id > ?",10);// Fetch all customers and assign the ResultSet to an IteratorIteratorallCustomersIterator=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.

@NoTableclassQueryData{privateintcount;privateStringconcatName;publiclonggetCount(){returncount;}publicvoidsetCount(longcount){this.count=count;}publicStringgetConcatName(){returnconcatName;}publicvoidsetConcatName(StringconcatName){this.concatName=concatName;}}QueryDataqd1=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 mapMap<String,Object>customerMap=persist.readMap("select * from customer where id=?",10);// Fetch all customers and result the results as Map instances in a List List<Map<String,Object>>customerMapList=persist.readMapList("select * from customer");// Fetch all customers and assign the ResultSet to an Iterator which maps rows to Map instancesIteratorallCustomersIterator=persist.readMapIterator("select * from customer");

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 StringStringname=persist.read(String.class,"select name from customer where id=?",55);// Fetch all customer id's as a list of integersList<Integer>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 parameterspersist.executeUpdate("delete from customer where id in (?,?)",10,20);

See also

Related Research Articles

Object–relational mapping in computer science is a programming technique for converting data between a relational database and the heap of an object-oriented programming language. This creates, in effect, a virtual object database that can be used from within the programming language.

<span class="mw-page-title-main">Object–relational database</span> Database management system

An object–relational database (ORD), or object–relational database management system (ORDBMS), is a database management system (DBMS) similar to a relational database, but with an object-oriented database model: objects, classes and inheritance are directly supported in database schemas and in the query language. In addition, just as with pure relational systems, it supports extension of the data model with custom data types and methods.

Java Data Objects (JDO) is a specification of Java object persistence. One of its features is a transparency of the persistence services to the domain model. JDO persistent objects are ordinary Java programming language classes (POJOs); there is no requirement for them to implement certain interfaces or extend from special classes. JDO 1.0 was developed under the Java Community Process as JSR 12. JDO 2.0 was developed under JSR 243 and was released on May 10, 2006. JDO 2.1 was completed in Feb 2008, developed by the Apache JDO project. JDO 2.2 was released in October 2008. JDO 3.0 was released in April 2010.

Hibernate ORM is an object–relational mapping tool for the Java programming language. It provides a framework for mapping an object-oriented domain model to a relational database. Hibernate handles object–relational impedance mismatch problems by replacing direct, persistent database accesses with high-level object handling functions.

<span class="mw-page-title-main">NHibernate</span>

NHibernate is an object–relational mapping (ORM) solution for the Microsoft .NET platform. It provides a framework for mapping an object-oriented domain model to a traditional relational database. Its purpose is to relieve the developer from a significant portion of relational data persistence-related programming tasks. NHibernate is free and open-source software that is distributed under the GNU Lesser General Public License. NHibernate is a port of Hibernate.

iBATIS is a persistence framework which automates the mapping between SQL databases and objects in Java, .NET, and Ruby on Rails. In Java, the objects are POJOs. The mappings are decoupled from the application logic by packaging the SQL statements in XML configuration files. The result is a significant reduction in the amount of code that a developer needs to access a relational database using lower level APIs like JDBC and ODBC.

<span class="mw-page-title-main">SQLAlchemy</span>

SQLAlchemy is an open-source SQL toolkit and object-relational mapper (ORM) for the Python programming language released under the MIT License.

Entity Framework (EF) is an open source object–relational mapping (ORM) framework for ADO.NET. It was originally shipped as an integral part of .NET Framework, however starting with Entity Framework version 6.0 it has been delivered separately from the .NET Framework.

Language Integrated Query is a Microsoft .NET Framework component that adds native data querying capabilities to .NET languages, originally released as a major part of .NET Framework 3.5 in 2007.

Ebean is an object–relational mapping product written in Java. It is designed to be simpler to use and understand than JPA or JDO products.

The Doctrine Project is a set of PHP libraries primarily focused on providing persistence services and related functionality. Its most commonly known projects are the object–relational mapper (ORM) and the database abstraction layer it is built on top of.

The MySQLi Extension is a relational database driver used in the PHP scripting language to provide an interface with MySQL databases.

The Jakarta Persistence Query Language is a platform-independent object-oriented query language defined as part of the Jakarta Persistence specification.

Apache Empire-db is a Java library that provides a high level object-oriented API for accessing relational database management systems (RDBMS) through JDBC. Apache Empire-db is open source and provided under the Apache License 2.0 from the Apache Software Foundation.

Java Database Connectivity (JDBC) is an application programming interface (API) for the Java programming language which defines how a client may access a database. It is a Java-based data access technology used for Java database connectivity. It is part of the Java Standard Edition platform, from Oracle Corporation. It provides methods to query and update data in a database, and is oriented toward relational databases. A JDBC-to-ODBC bridge enables connections to any ODBC-accessible data source in the Java virtual machine (JVM) host environment.

<span class="mw-page-title-main">Actian NoSQL Object Database</span>

Actian NoSQL Database is an object database software product initially developed by Versant Corporation and currently owned by Actian.

<span class="mw-page-title-main">XQuery API for Java</span> Application programming interface

XQuery API for Java (XQJ) refers to the common Java API for the W3C XQuery 1.0 specification.

In database management systems (DBMS), a prepared statement, parameterized statement, or parameterized query is a feature where the database pre-compiles SQL code and stores the results, separating it from data. Benefits of prepared statements are:

The syntax of the SQL programming language is defined and maintained by ISO/IEC SC 32 as part of ISO/IEC 9075. This standard is not freely available. Despite the existence of the standard, SQL code is not completely portable among different database systems without adjustments.

References