Package eu.miltema.slimorm
Class Database
- java.lang.Object
-
- eu.miltema.slimorm.Database
-
public class Database extends java.lang.ObjectDatabase link for subsequent CRUD operations
-
-
Constructor Summary
Constructors Constructor Description Database(DatabaseConnectionFactory connectionFactory)Create a database via custom connection factoryDatabase(java.lang.String jndiName)Create database object via JNDI handleDatabase(java.lang.String driverName, java.lang.String jdbcUrl, java.lang.String username, java.lang.String password)Create database with simple non-pooled connectionsDatabase(javax.sql.DataSource dataSource)Create database object via datasource
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <T> java.util.List<T>bulkInsert(java.util.List<T> entities)Insert a collection of entities into database as a batch.booleandelete(java.lang.Class<?> entityClass, java.lang.Object id)Delete an existing recordintdeleteWhere(java.lang.Class<?> entityClass, java.lang.String whereExpression, java.lang.Object... whereParameters)Delete multiple records<T> TgetById(java.lang.Class<? extends T> entityClass, java.lang.Object id)Fetch a single record/entity from a database tableDialectgetDialect()<T> Tinsert(T entity)Insert a single entity into database<T> java.util.List<T>listAll(java.lang.Class<? extends T> entityClass)Fetch all records/entities from a database table into a listSqlQuerysql(java.lang.String sqlSelect, java.lang.Object... whereParameters)Prepare a SELECT query<T> Ttransaction(TransactionStatements<T> statements)Runs a bunch of statements in a single transactionvoidupdate(java.lang.Object entity)Update an existing entity in database.voidupdate(java.lang.Object entity, java.lang.String whereExpression, java.lang.Object... whereParameters)Update an existing entity in database.SqlQuerywhere(java.lang.String whereExpression, java.lang.Object... whereParameters)Prepare a SELECT query with WHERE filter only.
-
-
-
Constructor Detail
-
Database
public Database(javax.sql.DataSource dataSource)
Create database object via datasource- Parameters:
dataSource- data source, which provides database connections
-
Database
public Database(DatabaseConnectionFactory connectionFactory)
Create a database via custom connection factory- Parameters:
connectionFactory- custom connection factory, which provides database connections
-
Database
public Database(java.lang.String jndiName) throws javax.naming.NamingExceptionCreate database object via JNDI handle- Parameters:
jndiName- JNDI name, for example "jdbc/demoDB"- Throws:
javax.naming.NamingException- when JNDI name lookup fails
-
Database
public Database(java.lang.String driverName, java.lang.String jdbcUrl, java.lang.String username, java.lang.String password) throws java.lang.ExceptionCreate database with simple non-pooled connections- Parameters:
driverName- driver class name, for example "org.postgresql.Driver"jdbcUrl- database URL, for example "jdbc:postgresql://localhost:5432/demoDB"username- SQL usernamepassword- SQL password- Throws:
java.lang.Exception- when anything goes wrong
-
-
Method Detail
-
insert
public <T> T insert(T entity) throws java.lang.ExceptionInsert a single entity into database- Type Parameters:
T- entity type- Parameters:
entity- entity to insert- Returns:
- the same entity, with @Id field (if any) being initialized
- Throws:
java.lang.Exception- when anything goes wrong
-
bulkInsert
public <T> java.util.List<T> bulkInsert(java.util.List<T> entities) throws java.lang.ExceptionInsert a collection of entities into database as a batch. Batch insertion is faster than inserting one by one.- Type Parameters:
T- entity type- Parameters:
entities- collection of entities to insert (currently there is a limit on records count that depends on the SQL driver)- Returns:
- the same entities, with @Id field (if any) being initialized
- Throws:
java.lang.Exception- when anything goes wrong
-
update
public void update(java.lang.Object entity) throws java.lang.ExceptionUpdate an existing entity in database. Only entities with @Id field can be updated. When @Id field is missing, use method update(entity, whereExpression, whereParameters)- Parameters:
entity- entity with new attribute values- Throws:
java.lang.Exception- when anything goes wrong
-
update
public void update(java.lang.Object entity, java.lang.String whereExpression, java.lang.Object... whereParameters) throws java.lang.ExceptionUpdate an existing entity in database. If WHERE expression selects multiple records, then all these records will be updated with new attribute values (except the @Id field).- Parameters:
entity- entity with new attribute valueswhereExpression- SQL WHERE expression, for example "name LIKE ?"whereParameters- parameters for WHERE expression- Throws:
java.lang.Exception- when anything goes wrong
-
delete
public boolean delete(java.lang.Class<?> entityClass, java.lang.Object id) throws java.lang.ExceptionDelete an existing record- Parameters:
entityClass- entity class, which indirectly refers to a database tableid- entity id- Returns:
- true, if the record existed before deletion; false, if the record did not exist
- Throws:
java.lang.Exception- when anything goes wrong
-
deleteWhere
public int deleteWhere(java.lang.Class<?> entityClass, java.lang.String whereExpression, java.lang.Object... whereParameters) throws java.lang.ExceptionDelete multiple records- Parameters:
entityClass- entity class, which indirectly refers to a database tablewhereExpression- SQL WHERE expression, for example "name LIKE ?"whereParameters- parameters for WHERE expression- Returns:
- number of records deleted
- Throws:
java.lang.Exception- when anything goes wrong
-
sql
public SqlQuery sql(java.lang.String sqlSelect, java.lang.Object... whereParameters) throws java.lang.Exception
Prepare a SELECT query- Parameters:
sqlSelect- SQL SELECT statementwhereParameters- parameter values for WHERE expression- Returns:
- query object for fetching the results
- Throws:
java.lang.Exception- when anything goes wrong
-
where
public SqlQuery where(java.lang.String whereExpression, java.lang.Object... whereParameters) throws java.lang.Exception
Prepare a SELECT query with WHERE filter only. Subsequent fetch operation (get or list) determines database table for this query- Parameters:
whereExpression- SQL WHERE expression, for example "name LIKE ?"whereParameters- parameter values for WHERE expression- Returns:
- query object for fetching the results
- Throws:
java.lang.Exception- when anything goes wrong
-
listAll
public <T> java.util.List<T> listAll(java.lang.Class<? extends T> entityClass) throws java.lang.ExceptionFetch all records/entities from a database table into a list- Type Parameters:
T- entity type- Parameters:
entityClass- entity class, which indirectly refers to a database table- Returns:
- list of entities
- Throws:
java.lang.Exception- when anything goes wrong
-
getById
public <T> T getById(java.lang.Class<? extends T> entityClass, java.lang.Object id) throws java.lang.ExceptionFetch a single record/entity from a database table- Type Parameters:
T- entity type- Parameters:
entityClass- entity class, which indirectly refers to a database tableid- entity id- Returns:
- entity; returns null, if id refers to non-existing record
- Throws:
java.lang.Exception- when anything goes wrong
-
transaction
public <T> T transaction(TransactionStatements<T> statements) throws java.lang.Exception
Runs a bunch of statements in a single transaction- Type Parameters:
T- entity type- Parameters:
statements- statements to run- Returns:
- the return value from statements
- Throws:
java.lang.Exception- when anything goes wrong
-
getDialect
public Dialect getDialect()
-
-