| java.lang.Object net.sf.persist.Persist
Persist | final public class Persist (Code) | | The main class for the persistence engine.
A Persist instance is bound to a
java.sql.Connection object.
Internally, Persist caches table-object mappings under cache names
that allow for different mappings (most likely from different database
schemas) to coexist. The default cache name is used if no cache name is
specified in the constructor.
Persist instances are not thread safe, in particular because
java.sql.Connection objects are not thread safe.
Persist instances are created with the following defaults:
- closePreparedStatementsAfterRead=true This will work for most reads
(select queries) that do not return long-lasting objects, such as streams or
LOB handlers. If a query returns InputStream, Reader, Blob or Clob objects,
closePreparedStatementsAfterRead should be set to false, and closing the
PreparedStatement must be controlled manually. This is because those
datatypes stream data from database after the PreparedStatement execution.
- updateAutoGeneratedKeys=false This means that objects that are inserted
either using insert() or executeUpdate() (with autoGeneratedKeys option) will
not have their primary keys automatically updated against the generated keys
in the database. Please consult your JDBC driver support for querying
auto-generated keys in
java.sql.PreparedStatement
-
DefaultNameGuesser which will take names in the form
CompoundName (for classes) or compoundName (for fields) and return a set of
guessed names such as [compound_name, compound_names, compoundname,
compoundnames].
See Also: TableMapping |
Constructor Summary | |
public | Persist(Connection connection) Creates a Persist instance that will use the default cache for
table-object mappings. | public | Persist(String cacheName, Connection connection) Creates a Persist instance that will use the given cache name for
table-object mappings. |
Method Summary | |
public void | closeLastPreparedStatement() Closes the last
java.sql.PreparedStatement used by the engine. | public void | closePreparedStatement(PreparedStatement statement) Closes a
java.sql.PreparedStatement . | public void | commit() Commits the
java.sql.Connection Connection associated with this
Persist instance. | public int | delete(Object object) Deletes an object in the database. | public int[] | deleteBatch(Object... objects) Updates a batch of objects in the database. | public Result | executeUpdate(Class objectClass, String sql, String[] autoGeneratedKeys, Object... parameters) Executes an update and return a
Result object containing the
number of rows modified and auto-generated keys produced.
Parameters will be set according with the correspondence defined in
Persist.setParameters(PreparedStatement,int[],Object[]) Parameters: objectClass - Class of the object related with the query. | public int | executeUpdate(String sql, Object... parameters) Executes an update and returns the number of rows modified. | public Connection | getConnection() Returns the
java.sql.Connection Connection associated with this
Persist instance. | public PreparedStatement | getLastPreparedStatement() Returns the last
java.sql.PreparedStatement used by the engine. | public Mapping | getMapping(Class objectClass) Returns the mapping for the given object class. | public PreparedStatement | getPreparedStatement(String sql, String[] autoGeneratedKeys) Creates a
java.sql.PreparedStatement , setting the names of the
auto-generated keys to be retrieved. | public PreparedStatement | getPreparedStatement(String sql) Creates a
java.sql.PreparedStatement with no parameters. | public static Object | getValueFromResultSet(ResultSet resultSet, int column, Class type) Reads a column from the current row in the provided
java.sql.ResultSet and returns an instance of the specified Java
java.lang.Class containing the values read.
This method is used while converting
java.sql.ResultSet rows to
objects. | public static Object | getValueFromResultSet(ResultSet resultSet, int column, int type) Reads a column from the current row in the provided
java.sql.ResultSet and return a value correspondent to the SQL
type provided (as defined in
java.sql.Types java.sql.Types ).
This method is used while converting results sets to maps. | public int | insert(Object object) Inserts an object into the database. | public int[] | insertBatch(Object... objects) Inserts a batch of objects into the database. | public boolean | isClosePreparedStatementsAfterRead() Returns true if
java.sql.PreparedStatement instances are
automatically closed after read (select or otherwise) queries. | public boolean | isUpdateAutoGeneratedKeys() Returns true if updating auto-generated keys is enabled. | public static Map<String, Object> | loadMap(ResultSet resultSet) Reads a row from the provided
java.sql.ResultSet and converts it
to a map having the column names as keys and results as values. | public Object | loadObject(Class objectClass, ResultSet resultSet) Reads a row from the provided
java.sql.ResultSet and converts it
to an object instance of the given class. | public T | read(Class<T> objectClass, String sql) Reads a single object from the database by mapping the results of the SQL
query into an instance of the given object class. | public T | read(Class<T> objectClass, String sql, Object... parameters) Reads a single object from the database by mapping the results of the
parameterized SQL query into an instance of the given object class. | public T | read(Class<T> objectClass, PreparedStatement statement, Object... parameters) Reads a single object from the database by mapping the results of the
execution of the given PreparedStatement into an instance of the given
object class. | public T | read(Class<T> objectClass, ResultSet resultSet) Reads a single object from the database by mapping the content of the
ResultSet current row into an instance of the given object class. | public T | readByPrimaryKey(Class<T> objectClass, Object... primaryKeyValues) Reads an object from the database by its primary keys. | public Iterator<T> | readIterator(Class<T> objectClass, ResultSet resultSet) Returns an
java.util.Iterator for a list of objects from the
database that map the contents of the ResultSet into instances of the
given object class. | public Iterator<T> | readIterator(Class<T> objectClass, PreparedStatement statement, Object... parameters) Returns an
java.util.Iterator for a list of objects from the
database that map the contents of the execution of the given
PreparedStatement into instances of the given object class. | public Iterator<T> | readIterator(Class<T> objectClass, String sql, Object... parameters) Returns an
java.util.Iterator for a list of objects from the
database that map the contents of the execution of the given SQL query
into instances of the given object class. | public Iterator<T> | readIterator(Class<T> objectClass, String sql) Returns an
java.util.Iterator for a list of objects from the
database that map the contents of the execution of the given SQL query
into instances of the given object class. | public Iterator<T> | readIterator(Class<T> objectClass) Returns an
java.util.Iterator for a list of all objects in the
database mapped to the given object class. | public List<T> | readList(Class<T> objectClass, ResultSet resultSet) Reads a list of objects from the database by mapping the content of the
ResultSet into instances of the given object class. | public List<T> | readList(Class<T> objectClass, PreparedStatement statement, Object... parameters) Reads a list of objects from the database by mapping the results of the
execution of the given PreparedStatement into instances of the given
object class. | public List<T> | readList(Class<T> objectClass, String sql, Object... parameters) Reads a list of objects from the database by mapping the results of the
parameterized SQL query into instances of the given object class. | public List<T> | readList(Class<T> objectClass, String sql) Reads a list of objects from the database by mapping the results of the
SQL query into instances of the given object class. | public List<T> | readList(Class<T> objectClass) Reads a list of all objects in the database mapped to the given object
class. | public Map<String, Object> | readMap(String sql) Reads a single object from the database by mapping the results of the SQL
query into an instance of
java.util.Map . | public Map<String, Object> | readMap(String sql, Object... parameters) Reads a single object from the database by mapping the results of the SQL
query into an instance of
java.util.Map . | public Map<String, Object> | readMap(PreparedStatement statement, Object... parameters) Reads a single object from the database by mapping the results of the
PreparedStatement execution into an instance of
java.util.Map . | public Map<String, Object> | readMap(ResultSet resultSet) Reads a single object from the database by mapping the results of the
current ResultSet row into an instance of
java.util.Map . | public Iterator | readMapIterator(ResultSet resultSet) Returns an
java.util.Iterator for a list of
java.util.Map instances containing data from the provided ResultSet rows. | public Iterator | readMapIterator(PreparedStatement statement, Object... parameters) Returns an
java.util.Iterator for a list of
java.util.Map instances containing data from the execution of the provided
PreparedStatement. | public Iterator | readMapIterator(String sql, Object... parameters) Returns an
java.util.Iterator for a list of
java.util.Map instances containing data from the execution of the provided parametrized
SQL. | public Iterator | readMapIterator(String sql) Returns an
java.util.Iterator for a list of
java.util.Map instances containing data from the execution of the provided SQL. | public List<Map<String, Object>> | readMapList(ResultSet resultSet) Reads a list of objects from the database by mapping the ResultSet rows
to instances of
java.util.Map . | public List<Map<String, Object>> | readMapList(PreparedStatement statement, Object... parameters) Reads a list of objects from the database by mapping the
PreparedStatement execution results to instances of
java.util.Map . | public List<Map<String, Object>> | readMapList(String sql, Object... parameters) Reads a list of objects from the database by mapping the SQL execution
results to instances of
java.util.Map . | public List<Map<String, Object>> | readMapList(String sql) Reads a list of all objects in the database mapped to the given object
class and return each result as an instance of
java.util.Map . | public void | rollback() Rolls back the
java.sql.Connection Connection associated with
this Persist instance. | public void | setAutoCommit(boolean autoCommit) Sets the auto commit behavior for the
java.sql.Connection Connection associated with this Persist
instance. | public void | setAutoGeneratedKeys(Object object, Result result) Set auto-generated keys (returned from an insert operation) into an
object. | public void | setClosePreparedStatementsAfterRead(boolean closePreparedStatementsAfterRead) Sets the behavior for closing
java.sql.PreparedStatement instances after execution. | public static void | setNameGuesser(String cacheName, NameGuesser nameGuesser) Sets the
NameGuesser for a given mappings cache. | public static void | setNameGuesser(NameGuesser nameGuesser) Sets the name guesser for the default mappings cache. | public static void | setParameters(PreparedStatement stmt, Object[] parameters) Sets parameters in the given prepared statement. | public void | setUpdateAutoGeneratedKeys(boolean updateAutoGeneratedKeys) Sets the behavior for updating auto-generated keys. | public int | update(Object object) Updates an object in the database. | public int[] | updateBatch(Object... objects) Updates a batch of objects in the database. |
Persist | public Persist(Connection connection)(Code) | | Creates a Persist instance that will use the default cache for
table-object mappings.
Parameters: connection - java.sql.Connection object to be used since: 1.0 |
Persist | public Persist(String cacheName, Connection connection)(Code) | | Creates a Persist instance that will use the given cache name for
table-object mappings.
Parameters: cacheName - Name of the cache to be used Parameters: connection - java.sql.Connection object to be used since: 1.0 |
delete | public int delete(Object object)(Code) | | Deletes an object in the database. The object will be identified using
its mapped table's primary key. If no primary keys are defined in the
mapped table, a PersistException will be thrown.
since: 1.0 |
deleteBatch | public int[] deleteBatch(Object... objects)(Code) | | Updates a batch of objects in the database. The objects will be
identified using their matched table's primary keys. If no primary keys
are defined in a given object, a PersistException will be thrown.
since: 1.0 |
executeUpdate | public Result executeUpdate(Class objectClass, String sql, String[] autoGeneratedKeys, Object... parameters)(Code) | | Executes an update and return a
Result object containing the
number of rows modified and auto-generated keys produced.
Parameters will be set according with the correspondence defined in
Persist.setParameters(PreparedStatement,int[],Object[]) Parameters: objectClass - Class of the object related with the query. Used todetermine the types of the auto-incremented keys. Only important ifautoGeneratedKeys contains values. Parameters: sql - SQL code to be executed. Parameters: autoGeneratedKeys - List of columns that are going to beauto-generated during the query execution. Parameters: parameters - Parameters to be used in the PreparedStatement. since: 1.0 |
executeUpdate | public int executeUpdate(String sql, Object... parameters)(Code) | | Executes an update and returns the number of rows modified.
Parameters will be set according with the correspondence defined in
Persist.setParameters(PreparedStatement,int[],Object[]) Parameters: sql - SQL code to be executed. Parameters: parameters - Parameters to be used in the PreparedStatement. since: 1.0 |
getValueFromResultSet | public static Object getValueFromResultSet(ResultSet resultSet, int column, Class type)(Code) | | Reads a column from the current row in the provided
java.sql.ResultSet and returns an instance of the specified Java
java.lang.Class containing the values read.
This method is used while converting
java.sql.ResultSet rows to
objects. The class type is the field type in the target bean.
Correspondence between class types and ResultSet.get methods is as
follows:
- Boolean/boolean: getBoolean
- Byte/byte: getByte
- Short/short: getShort
- Integer/int: getInt
- Long/long: getLong
- Float/float: getFloat
- Double/double: getDouble
- Character/char: getString
- Character[]/char[]: getString
- Byte[]/byte[]: setBytes
- String: setString
- java.math.BigDecimal: getBigDecimal
- java.io.Reader: getCharacterStream
- java.io.InputStream: getBinaryStream
- java.util.Date: getTimestamp
- java.sql.Date: getDate
- java.sql.Time: getTime
- java.sql.Timestamp: getTimestamp
- java.sql.Clob: getClob
- java.sql.Blob: getBlob
null's will be respected for any non-native types. This means that if a
field is of type Integer it will be able to receive a null value from the
ResultSet; on the other hand, if a field is of type int it will receive 0
for a null value from the
java.sql.ResultSet .
Parameters: resultSet - java.sql.ResultSet (positioned in the row to beprocessed) Parameters: column - column index in the result set (starting with 1) Parameters: type - java.lang.Class of the object to be returned since: 1.0 |
getValueFromResultSet | public static Object getValueFromResultSet(ResultSet resultSet, int column, int type)(Code) | | Reads a column from the current row in the provided
java.sql.ResultSet and return a value correspondent to the SQL
type provided (as defined in
java.sql.Types java.sql.Types ).
This method is used while converting results sets to maps. The SQL type
comes from the
java.sql.ResultSetMetaData ResultSetMetaData for a
given column.
Correspondence between
java.sql.Types java.sql.Types and
ResultSet.get methods is as follows:
- ARRAY: getArray
- BIGINT: getLong
- BIT: getBoolean
- BLOB: getBytes
- BOOLEAN: getBoolean
- CHAR: getString
- CLOB: getString
- DATALINK: getBinaryStream
- DATE: getDate
- DECIMAL: getBigDecimal
- DOUBLE: getDouble
- FLOAT: getFloat
- INTEGER: getInt
- JAVA_OBJECT: getObject
- LONGVARBINARY: getBytes
- LONGVARCHAR: getString
- NULL: getNull
- NCHAR: getString
- NUMERIC: getBigDecimal
- OTHER: getObject
- REAL: getDouble
- REF: getRef
- SMALLINT: getInt
- TIME: getTime
- TIMESTAMP: getTimestamp
- TINYINT: getInt
- VARBINARY: getBytes
- VARCHAR: getString
- [Oracle specific] 100: getFloat
- [Oracle specific] 101: getDouble
null's are respected for all types. This means that if a column is of
type LONG and its value comes from the database as null, this method will
return null for it.
Parameters: resultSet - java.sql.ResultSet (positioned in the row to beprocessed) Parameters: column - Column index in the result set (starting with 1) Parameters: type - type of the column (as defined injava.sql.Types java.sql.Types) since: 1.0 |
insert | public int insert(Object object)(Code) | | Inserts an object into the database.
since: 1.0 |
insertBatch | public int[] insertBatch(Object... objects)(Code) | | Inserts a batch of objects into the database.
since: 1.0 |
isClosePreparedStatementsAfterRead | public boolean isClosePreparedStatementsAfterRead()(Code) | | Returns true if
java.sql.PreparedStatement instances are
automatically closed after read (select or otherwise) queries.
since: 1.0 |
isUpdateAutoGeneratedKeys | public boolean isUpdateAutoGeneratedKeys()(Code) | | Returns true if updating auto-generated keys is enabled.
|
read | public T read(Class<T> objectClass, String sql)(Code) | | Reads a single object from the database by mapping the results of the SQL
query into an instance of the given object class. Only the columns
returned from the SQL query will be set into the object instance. If a
given column can't be mapped to the target object instance, a
PersistException will be thrown.
since: 1.0 |
read | public T read(Class<T> objectClass, String sql, Object... parameters)(Code) | | Reads a single object from the database by mapping the results of the
parameterized SQL query into an instance of the given object class. Only
the columns returned from the SQL query will be set into the object
instance. If a given column can't be mapped to the target object
instance, a
PersistException will be thrown.
Parameters will be set according with the correspondence defined in
Persist.setParameters(PreparedStatement,int[],Object[]) since: 1.0 |
read | public T read(Class<T> objectClass, PreparedStatement statement, Object... parameters)(Code) | | Reads a single object from the database by mapping the results of the
execution of the given PreparedStatement into an instance of the given
object class. Only the columns returned from the PreparedStatement
execution will be set into the object instance. If a given column can't
be mapped to the target object instance, a
PersistException will be thrown.
Parameters will be set according with the correspondence defined in
Persist.setParameters(PreparedStatement,int[],Object[]) since: 1.0 |
read | public T read(Class<T> objectClass, ResultSet resultSet)(Code) | | Reads a single object from the database by mapping the content of the
ResultSet current row into an instance of the given object class. Only
columns contained in the ResultSet will be set into the object instance.
If a given column can't be mapped to the target object instance, a
PersistException will be thrown.
since: 1.0 |
readByPrimaryKey | public T readByPrimaryKey(Class<T> objectClass, Object... primaryKeyValues)(Code) | | Reads an object from the database by its primary keys.
since: 1.0 |
readIterator | public Iterator<T> readIterator(Class<T> objectClass, ResultSet resultSet)(Code) | | Returns an
java.util.Iterator for a list of objects from the
database that map the contents of the ResultSet into instances of the
given object class. Only columns contained in the ResultSet will be set
into the object instances. If a given column can't be mapped to a target
object instance, a
PersistException will be thrown.
since: 1.0 |
readIterator | public Iterator<T> readIterator(Class<T> objectClass, PreparedStatement statement, Object... parameters)(Code) | | Returns an
java.util.Iterator for a list of objects from the
database that map the contents of the execution of the given
PreparedStatement into instances of the given object class. Only columns
contained in the ResultSet will be set into the object instances. If a
given column can't be mapped to a target object instance, a
PersistException will be thrown.
Parameters will be set according with the correspondence defined in
Persist.setParameters(PreparedStatement,int[],Object[]) since: 1.0 |
readIterator | public Iterator<T> readIterator(Class<T> objectClass, String sql)(Code) | | Returns an
java.util.Iterator for a list of objects from the
database that map the contents of the execution of the given SQL query
into instances of the given object class. Only columns contained in the
ResultSet will be set into the object instances. If a given column can't
be mapped to a target object instance, a
PersistException will
be thrown.
since: 1.0 |
readIterator | public Iterator<T> readIterator(Class<T> objectClass)(Code) | | Returns an
java.util.Iterator for a list of all objects in the
database mapped to the given object class.
since: 1.0 |
readList | public List<T> readList(Class<T> objectClass, ResultSet resultSet)(Code) | | Reads a list of objects from the database by mapping the content of the
ResultSet into instances of the given object class. Only columns
contained in the ResultSet will be set into the object instances. If a
given column can't be mapped to a target object instance, a
PersistException will be thrown.
since: 1.0 |
readList | public List<T> readList(Class<T> objectClass, PreparedStatement statement, Object... parameters)(Code) | | Reads a list of objects from the database by mapping the results of the
execution of the given PreparedStatement into instances of the given
object class. Only the columns returned from the PreparedStatement
execution will be set into the object instances. If a given column can't
be mapped to a target object instance, a PersistException will be
thrown.
Parameters will be set according with the correspondence defined in
Persist.setParameters(PreparedStatement,int[],Object[]) since: 1.0 |
readList | public List<T> readList(Class<T> objectClass, String sql, Object... parameters)(Code) | | Reads a list of objects from the database by mapping the results of the
parameterized SQL query into instances of the given object class. Only
the columns returned from the SQL query will be set into the object
instance. If a given column can't be mapped to the target object
instance, a
PersistException will be thrown.
Parameters will be set according with the correspondence defined in
Persist.setParameters(PreparedStatement,int[],Object[]) since: 1.0 |
readList | public List<T> readList(Class<T> objectClass, String sql)(Code) | | Reads a list of objects from the database by mapping the results of the
SQL query into instances of the given object class. Only the columns
returned from the SQL query will be set into the object instance. If a
given column can't be mapped to the target object instance, a
PersistException will be thrown.
since: 1.0 |
readList | public List<T> readList(Class<T> objectClass)(Code) | | Reads a list of all objects in the database mapped to the given object
class.
since: 1.0 |
setAutoGeneratedKeys | public void setAutoGeneratedKeys(Object object, Result result)(Code) | | Set auto-generated keys (returned from an insert operation) into an
object.
Parameters: object - java.lang.Object to have fields mapped toauto-generated keys set Parameters: result - Result containing auto-generated keys since: 1.0 |
setClosePreparedStatementsAfterRead | public void setClosePreparedStatementsAfterRead(boolean closePreparedStatementsAfterRead)(Code) | | Sets the behavior for closing
java.sql.PreparedStatement instances after execution. This will only affect reads, since any update
operations (insert, delete, update) will always have their
java.sql.PreparedStatement instances automatically closed.
If a query returns InputStream, Reader, Blob or Clob objects, this should
be set to false, and closing the PreparedStatement must be controlled
manually. This is because those datatypes stream data from database after
the PreparedStatement execution.
Parameters: closePreparedStatementsAfterRead - if true,java.sql.PreparedStatement instances for read queries will beautomatically closed since: 1.0 |
setNameGuesser | public static void setNameGuesser(String cacheName, NameGuesser nameGuesser)(Code) | | Sets the
NameGuesser for a given mappings cache.
Parameters: cacheName - Name of the cache to be used Parameters: nameGuesser - NameGuesser implementation since: 1.0 |
setNameGuesser | public static void setNameGuesser(NameGuesser nameGuesser)(Code) | | Sets the name guesser for the default mappings cache.
Parameters: nameGuesser - NameGuesser implementation since: 1.0 |
setParameters | public static void setParameters(PreparedStatement stmt, Object[] parameters)(Code) | | Sets parameters in the given prepared statement.
Parameters will be set using PreparedStatement set methods related with
the Java types of the parameters, according with the following table:
- Boolean/boolean: setBoolean
- Byte/byte: setByte
- Short/short: setShort
- Integer/integer: setInt
- Long/long: setLong
- Float/float: setFloat
- Double/double: setDouble
- Character/char: setString
- Character[]/char[]: setString
- Byte[]/byte[]: setBytes
- String: setString
- java.math.BigDecimal: setBigDecimal
- java.io.Reader: setCharacterStream
- java.io.InputStream: setBinaryStream
- java.util.Date: setTimestamp
- java.sql.Date: setDate
- java.sql.Time: setTime
- java.sql.Timestamp: setTimestamp
- java.sql.Clob : setClob
- java.sql.Blob: setBlob
Parameters: stmt - java.sql.PreparedStatement to have parameters setinto Parameters: parameters - varargs or Object[] with parameters values throws: RuntimeSQLException - if a database access error occurs or thismethod is called on a closed PreparedStatement; if a parameter type doesnot have a matching set method (as outlined above) throws: RuntimeIOException - if an error occurs while reading data from aReader or InputStream parameter since: 1.0 |
setUpdateAutoGeneratedKeys | public void setUpdateAutoGeneratedKeys(boolean updateAutoGeneratedKeys)(Code) | | Sets the behavior for updating auto-generated keys.
Parameters: updateAutoGeneratedKeys - if set to true, auto-generated keys willbe updated after the execution of insert or executeUpdate operations thatmay trigger auto-generation of keys in the database since: 1.0 |
update | public int update(Object object)(Code) | | Updates an object in the database. The object will be identified using
its mapped table's primary key. If no primary keys are defined in the
mapped table, a
PersistException will be thrown.
since: 1.0 |
updateBatch | public int[] updateBatch(Object... objects)(Code) | | Updates a batch of objects in the database. The objects will be
identified using their mapped table's primary keys. If no primary keys
are defined in the mapped table, a
PersistException will be
thrown.
since: 1.0 |
|
|