org.ujoframework.orm
Class Session

java.lang.Object
  extended by org.ujoframework.orm.Session

public class Session
extends java.lang.Object

The ORM session.
Methods of the session are not thread safe.

Author:
Pavel Ponec

Field Summary
static java.lang.String SQL_ILLEGAL
          Exception SQL message prefix
 
Method Summary
protected  void call(DbProcedure procedure)
          Call the stored procedure
 void clearCache()
          Clear the cache.
 void clearCache(CachePolicy policy)
          Clear cache and change its policy.
 void close()
          Close and release all DB connections.
 void commit()
          Make a commit for all databases.
 void commit(boolean commit)
          Make commit/rollback for all 'production' databases.
protected  Criterion createPkCriterion(OrmUjo bo)
          Returns an criterion by a PrimaryKey
<UJO extends OrmUjo>
Query<UJO>
createQuery(java.lang.Class<UJO> aClass)
          For all rows.
<UJO extends OrmUjo>
Query<UJO>
createQuery(java.lang.Class<UJO> aClass, Criterion<UJO> criterion)
           
<UJO extends OrmUjo>
Query<UJO>
createQuery(Criterion<UJO> criterion)
          The table class is derived from the first criterion column.
<UJO extends OrmUjo>
int
delete(java.lang.Class<UJO> tableClass, Criterion<UJO> criterion)
          Delete all object object by the criterion from parameter.
<UJO extends OrmUjo>
int
delete(Criterion<UJO> criterion)
          Delete all object object by the criterion from parameter.
protected
<UJO extends OrmUjo>
int
delete(MetaTable tableModel, Criterion<UJO> criterion)
          Delete all objects object form parameter
Warning: method does not remove deleted object from internal cache, however you can call method clearCache() to release all objects from the cache.
 boolean delete(OrmUjo bo)
          Delete one object from the parameters.
 OrmUjo findCache(java.lang.Class type, java.lang.Object... pkeys)
          Find object from internal cache
 OrmUjo findCache(java.lang.Class type, java.lang.Object pkey)
          Find object from internal cache
 MetaRelation2Many getBasicColumn(Criterion criterion)
          Returns the first "basic" column of criterion.
 java.sql.Connection getConnection(MetaDatabase database)
          Get connection for a required database with an autocommit na false.
<DB extends OrmUjo>
DB
getDatabase(java.lang.Class<DB> dbType)
          Returns a Database instance.
<DB extends OrmUjo>
DB
getFirstDatabase()
          Returns the first Database instance.
 OrmHandler getHandler()
          Returns a handler
protected  java.util.List<MetaColumn> getOrmColumns(UjoProperty... properties)
          Convert a property array to a column list.
 MetaParams getParameters()
          Returns parameters
<UJO extends OrmUjo>
long
getRowCount(Query<UJO> query)
          Returns a count of rows
 JdbcStatement getStatement(MetaDatabase database, java.lang.CharSequence sql)
          Create new statement
 JdbcStatement getStatement(Query query)
          Run SQL SELECT by query.
 JdbcStatement getStatementCallable(MetaDatabase database, java.lang.String sql)
          Create new statement
 boolean isClosed()
          Is the session closed?
 boolean isRollbackOnly()
          The rollback is allowed only
<UJO extends OrmUjo>
UjoIterator<UJO>
iterateInternal(RelationToMany property, OrmUjo value)
          Iterate property of values
<UJO extends OrmUjo>
UJO
load(java.lang.Class<UJO> tableType, java.lang.Object id)
          Load UJO by a unique id.
<UJO extends OrmUjo>
UJO
loadInternal(UjoProperty relatedProperty, java.lang.Object id, boolean mandatory)
          Load UJO by a unique id.
 void markForRolback()
           
static Session newClosedSession(OrmHandler handler)
          Create the closed session
 ForeignKey readFK(OrmUjo ujo, UjoProperty<?,? extends OrmUjo> property)
          Build new Forign key.
 boolean reload(OrmUjo ujo)
          Reload values of the persistent object.
 void rollback()
          Make a rollback for all databases.
 void save(OrmUjo bo)
          INSERT object into table.
 void saveOrUpdate(OrmUjo bo)
          INSERT or UPDATE object into table.
 int update(OrmUjo bo)
          UPDATE object into table.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SQL_ILLEGAL

public static final java.lang.String SQL_ILLEGAL
Exception SQL message prefix

See Also:
Constant Field Values
Method Detail

getHandler

public final OrmHandler getHandler()
Returns a handler


commit

public void commit()
Make a commit for all databases.


rollback

public void rollback()
Make a rollback for all databases.


commit

public void commit(boolean commit)
Make commit/rollback for all 'production' databases.

Parameters:
commit - if parameters is false than make a rollback.

createQuery

public <UJO extends OrmUjo> Query<UJO> createQuery(java.lang.Class<UJO> aClass)
For all rows.


createQuery

public <UJO extends OrmUjo> Query<UJO> createQuery(java.lang.Class<UJO> aClass,
                                                   Criterion<UJO> criterion)

createQuery

public <UJO extends OrmUjo> Query<UJO> createQuery(Criterion<UJO> criterion)
The table class is derived from the first criterion column.


getBasicColumn

public MetaRelation2Many getBasicColumn(Criterion criterion)
Returns the first "basic" column of criterion.


getFirstDatabase

public final <DB extends OrmUjo> DB getFirstDatabase()
Returns the first Database instance.


getDatabase

public <DB extends OrmUjo> DB getDatabase(java.lang.Class<DB> dbType)
Returns a Database instance. If parameter is null, than method returns the first database.


saveOrUpdate

public void saveOrUpdate(OrmUjo bo)
                  throws java.lang.IllegalStateException
INSERT or UPDATE object into table.

Throws:
java.lang.IllegalStateException

save

public void save(OrmUjo bo)
          throws java.lang.IllegalStateException
INSERT object into table.

Throws:
java.lang.IllegalStateException

update

public int update(OrmUjo bo)
           throws java.lang.IllegalStateException
UPDATE object into table.

Throws:
java.lang.IllegalStateException

delete

public <UJO extends OrmUjo> int delete(Criterion<UJO> criterion)
Delete all object object by the criterion from parameter.
Warning: method does not remove deleted object from internal cache, however you can call method clearCache() to release all objects from the cache.

Parameters:
criterion - filter for deleting tables.
Returns:
Returns a number of the realy deleted objects.

delete

public boolean delete(OrmUjo bo)
Delete one object from the parameters.
Warning: method does not remove deleted object from internal cache, however you can call method clearCache() to release all objects from the cache.

Parameters:
bo - Business object to delete
Returns:
Returns a number of the removing is OK.

delete

public <UJO extends OrmUjo> int delete(java.lang.Class<UJO> tableClass,
                                       Criterion<UJO> criterion)
Delete all object object by the criterion from parameter.
Warning: method does not remove deleted object from internal cache, however you can call method clearCache() to release all objects from the cache.

Parameters:
tableClass - Type of table to delete
criterion - filter for deleting tables.
Returns:
Returns a number of the realy deleted objects.

delete

protected <UJO extends OrmUjo> int delete(MetaTable tableModel,
                                          Criterion<UJO> criterion)
Delete all objects object form parameter
Warning: method does not remove deleted object from internal cache, however you can call method clearCache() to release all objects from the cache.

Parameters:
tableModel - Type of table to delete
criterion - filter for deleting tables.
Returns:
Returns a number of the realy deleted objects.

call

protected void call(DbProcedure procedure)
Call the stored procedure


getOrmColumns

protected java.util.List<MetaColumn> getOrmColumns(UjoProperty... properties)
Convert a property array to a column list.


createPkCriterion

protected Criterion createPkCriterion(OrmUjo bo)
Returns an criterion by a PrimaryKey


getRowCount

public <UJO extends OrmUjo> long getRowCount(Query<UJO> query)
Returns a count of rows


getStatement

public JdbcStatement getStatement(Query query)
Run SQL SELECT by query.


iterateInternal

public <UJO extends OrmUjo> UjoIterator<UJO> iterateInternal(RelationToMany property,
                                                             OrmUjo value)
Iterate property of values

Parameters:
property - Table property type of the RelationToMany.
value - A value type of OrmUjo

getConnection

public final java.sql.Connection getConnection(MetaDatabase database)
                                        throws java.lang.IllegalStateException
Get connection for a required database with an autocommit na false.

Throws:
java.lang.IllegalStateException

getStatement

public JdbcStatement getStatement(MetaDatabase database,
                                  java.lang.CharSequence sql)
                           throws java.sql.SQLException
Create new statement

Throws:
java.sql.SQLException

getStatementCallable

public JdbcStatement getStatementCallable(MetaDatabase database,
                                          java.lang.String sql)
                                   throws java.sql.SQLException
Create new statement

Throws:
java.sql.SQLException

load

public <UJO extends OrmUjo> UJO load(java.lang.Class<UJO> tableType,
                                     java.lang.Object id)
                        throws java.util.NoSuchElementException
Load UJO by a unique id. If a result is not found then a null value is passed.

Parameters:
tableType - Type of Ujo
id - Value ID
Throws:
java.util.NoSuchElementException

loadInternal

public <UJO extends OrmUjo> UJO loadInternal(UjoProperty relatedProperty,
                                             java.lang.Object id,
                                             boolean mandatory)
                                throws java.util.NoSuchElementException
Load UJO by a unique id. If the result is not unique, then an exception is throwed.

Parameters:
relatedProperty - Related property
id - Valud ID
mandatory - If result is mandatory then the method throws an exception if no object was found else returns null;
Throws:
java.util.NoSuchElementException

close

public void close()
           throws java.lang.IllegalStateException
Close and release all DB connections.

Throws:
java.lang.IllegalStateException - The exception contains a bug from Connection close;

isClosed

public boolean isClosed()
Is the session closed?


findCache

public OrmUjo findCache(java.lang.Class type,
                        java.lang.Object pkey)
Find object from internal cache


findCache

public OrmUjo findCache(java.lang.Class type,
                        java.lang.Object... pkeys)
Find object from internal cache


clearCache

public void clearCache()
Clear the cache.


clearCache

public void clearCache(CachePolicy policy)
Clear cache and change its policy.


getParameters

public final MetaParams getParameters()
Returns parameters


isRollbackOnly

public boolean isRollbackOnly()
The rollback is allowed only


markForRolback

public void markForRolback()

readFK

public ForeignKey readFK(OrmUjo ujo,
                         UjoProperty<?,? extends OrmUjo> property)
                  throws java.lang.IllegalStateException
Build new Forign key.

Parameters:
property - The property must be a relalation type of "many to one".
Throws:
java.lang.IllegalStateException - If a parameter property is not a foreign key.

reload

public boolean reload(OrmUjo ujo)
Reload values of the persistent object.
Note: If the object has implemented the interface ExtendedOrmUjo than foreign keys are reloaded else a lazy initialization is loaded - for the first property depth.

Parameters:
ujo - The persistent object to relading values.
Returns:
The FALSE value means that the object is missing in the database.

newClosedSession

public static Session newClosedSession(OrmHandler handler)
Create the closed session



Copyright © 2010. All Rights Reserved.