Persist (Java tool): Difference between revisions

Content deleted Content added
mNo edit summary
m Infobox Software: 2-clause BSD License, source https://github.com/r5v9/persist/tree/master
 
(13 intermediate revisions by 10 users not shown)
Line 1:
{{No footnotes|date=June 2023}}
{{Other uses|Persistence (disambiguation)}}
{{Orphan|date=May 2013}}
 
{{Infobox Software
|name = Persist
|logo =
|screenshot =
|caption =
Line 12 ⟶ 10:
|operating_system = [[Cross-platform]]
|genre = [[Persistence Framework]]
|license = [[BSD_licenses#2-clause_license_("Simplified_BSD_License"_or_"FreeBSD_License")|2-clause BSD License]]
|license = [[LGPL|GNU Lesser General Public License]]
|website = https://code.google.com/p/persist/
}}
Line 26 ⟶ 24:
 
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.
<syntaxhighlight lang="java">
// insertsInserts a new customer (the class Customer is mapped to the table customer automatically)
persist.insert(customer);
 
// readsReads a customer by its primary key
// inserts a new customer (the class Customer is mapped to the table customer automatically)
Customer c = persist.readByPrimaryKey(Customer.class, 42);
persist.insert(customer);
 
// fetchRetrieves a customercustomers using a custom query and return(note the resultusage as aof mapvarargs)
// reads a customer by its primary key
CustomerList clist = persist.readByPrimaryKeyreadList(Customer.class, 42"select * from customer where id > ?", 10);
 
// retrievesFetch all customers usingand aassign customthe query (note theResultSet usageto ofan varargs)Iterator
ListIterator listallCustomersIterator = persist.readListreadIterator(Customer.class, "select * from customer where id > ?", 10);
</syntaxhighlight>
// 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.
<syntaxhighlight lang="java">
 
@NoTable
class QueryData {
private int count;
private String concatName;
Line 53 ⟶ 52:
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");
</syntaxhighlight>
 
'''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.
<syntaxhighlight lang="java">
// Fetch a customer using a custom query and return the result as a map
Map<String,Object> customerMap = persist.readMap("select * from customer where id=?", 10);
 
// Fetch all customers and result the results as Map instances in a List
// fetch a customer using a custom query and return the result as a map
List<Map<String,Object>> customerMapcustomerMapList = persist.readMapreadMapList("select * from customer where id=?", 10);
 
// fetchFetch all customers and resultassign the resultsResultSet asto Mapan instancesIterator inwhich amaps Listrows to Map instances
List<Map<String,Object>>Iterator customerMapListallCustomersIterator = persist.readMapListreadMapIterator("select * from customer");
</syntaxhighlight>
// fetch all customers and assign the ResultSet to an Iterator which maps rows to Map instances
Iterator allCustomersIterator = 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):
<syntaxhighlight lang="java">
// Return customer name as String
IteratorString allCustomersIteratorname = persist.readIteratorread(CustomerString.class, "select *name from customer where id=?", 55);
 
// returnFetch all customer nameid's as Stringa list of integers
StringList<Integer> nameids = persist.readreadList(StringInteger.class, "select nameid from customer where id=?", 55);
</syntaxhighlight>
// fetch all customer id's as a list of integers
List<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.
<syntaxhighlight lang="java">
 
// executeExecute arbitrary SQL with parameters
persist.executeUpdate("delete from customer where id in (?,?)", 10, 20);
</syntaxhighlight>
 
==See also==
* [[Hibernate (Java)|Hibernate]]
* [[iBATIS]]
 
==External links References==
{{Reflist}}
*[https://github.com/rufiao/persist Persist @ GitHub]
 
==External links==
* [https://github.com/rufiao/persist Persist @ GitHub]
 
[[Category:Java (programming language)]]
[[Category:Articles with example Java code]]
[[Category:Java (programming language)]]