SRJRCFrames
v0.1.296

de.schwarzrot.data.access.jdbc
Class JDBCEntityManager

java.lang.Object
  extended by de.schwarzrot.data.access.support.AbstractEntityManager
      extended by de.schwarzrot.data.access.jdbc.JDBCEntityManager
All Implemented Interfaces:
EntityManager

public class JDBCEntityManager
extends AbstractEntityManager

implementation of an EntityManager that uses JDBC-database access for persistence. It uses DbUtil-implementations to hide the database implementation dependant stuff, so this class should work with other databases too.

Author:
Reinhard Mantey

Field Summary
 
Fields inherited from class de.schwarzrot.data.access.support.AbstractEntityManager
appDefaults
 
Constructor Summary
JDBCEntityManager()
           
JDBCEntityManager(DataSource ds)
           
 
Method Summary
protected
<E extends Entity>
E
checkExistance(E instance, Connection conn, DbUtil dbUtil)
          checks whether the given instance already exists in persistence.
 void commit(TransactionResource<?> resource)
          used to persist all changes made during transaction.
<E extends Entity>
long
countInstances(Class<E> entityType, Connection conn)
           
protected  int createInstance(Entity instance, Connection conn, DbUtil dbUtil)
           
 TransactionResource<?> createTransactionResource(Transaction t)
          create an abstract handle to storage.
 void execute(TransactionalOperation<?> tao)
          execute a single step of ApplicationTransaction
protected  int find(List<?> resultList, Class<?> entityType, List<ConditionElement> args, EntityDescriptor ed, Connection conn, DbUtil dbUtil, boolean readRelated, Collection<SortInfo> orderSequence)
          internal call to read a number of entities.
 DataSource getDataSource()
           
protected
<E extends Entity>
E
getEntity(E instance, Class<? extends Entity> entityType, long id, Connection conn)
          internal call to resolve a given instance.
 EntityDescriptor getEntityDescriptor(Class<?> entityType, Connection conn)
           
 EntityDescriptor getEntityDescriptor(Class<?> entityType, TransactionResource<?> res)
          accessor to description instances of entities.
protected  long instanceCount(Class<? extends Entity> entityType, List<ConditionElement> args, Connection conn)
           
protected
<E extends Entity>
E
instanceFromResultSet(ResultSet rs, Connection conn, EntityDescriptor ed, DbUtil dbUtil, boolean readRelated)
           
protected  long queryId(EntityDescriptor ed, Date now, String userName, Connection conn, DbUtil dbUtil)
           
protected  int readProperties(List resultList, Class entityType, Collection<String> properties, List<ConditionElement> args, EntityDescriptor ed, Connection conn, DbUtil dbUtil, int maxRows, boolean readRelated, boolean uniq, Collection<SortInfo> orderSequence)
           
 void releaseTransactionResource(TransactionResource<?> resource)
          free given storage handle
protected
<E extends Entity>
int
remove(Class<E> entityType, List<ConditionElement> args, EntityDescriptor ed, Connection conn, DbUtil dbUtil)
           
protected
<E extends Entity>
int
removeChildren(E instance, EntityDescriptor ed, Connection conn, DbUtil dbUtil)
           
protected
<E extends Entity>
int
removeRelated(Class<?> childEntityType, E instance, Connection conn, DbUtil dbUtil)
           
protected
<E extends Entity>
void
resolveGroupedUnnamedChildren(E instance, EntityDescriptor ed, PropertyInfo pi, Connection conn, DbUtil dbUtil)
           
protected
<E extends Entity>
void
resolveNamedChildren(E instance, EntityDescriptor ed, PropertyInfo pi, Connection conn, DbUtil dbUtil)
           
protected
<E extends Entity>
void
resolveReferences(E instance, EntityDescriptor ed, PropertyInfo pi, Connection conn, DbUtil dbUtil)
           
protected
<E extends Entity>
void
resolveRelated(EntityDescriptor ed, E instance, Connection conn, DbUtil dbUtil)
          resolve all relations, that could be defined for an Entity.
protected
<E extends Entity>
void
resolveSingleForeign(E instance, EntityDescriptor ed, PropertyInfo pi, Connection conn, DbUtil dbUtil)
           
protected
<E extends Entity>
void
resolveUnnamedChildren(E instance, EntityDescriptor ed, PropertyInfo pi, Connection conn, DbUtil dbUtil)
           
protected
<E extends Entity>
void
resolveWeightedReferences(E instance, EntityDescriptor ed, PropertyInfo pi, Connection conn, DbUtil dbUtil)
           
protected
<E extends Entity>
int
resultSet2EList(ca.odell.glazedlists.EventList<E> resultList, ResultSet rs, Connection conn, EntityDescriptor ed, DbUtil dbUtil, int maxRows, boolean readRelated)
           
protected
<E extends Entity>
int
resultSet2List(List<E> resultList, ResultSet rs, Connection conn, EntityDescriptor ed, DbUtil dbUtil, int maxRows, boolean readRelated)
           
protected
<E extends Entity>
int
resultSet2TrList(ca.odell.glazedlists.TransactionList<E> resultList, ResultSet rs, Connection conn, EntityDescriptor ed, DbUtil dbUtil, int maxRows, boolean readRelated)
           
 void rollback(TransactionResource<?> resource)
          discards changes of given storage handle and marks Transaction as failed.
protected  int save(Entity instance, Connection conn, DbUtil dbUtil)
          internal call to bring an Entity to persistence.
protected  void saveForeigns(EntityDescriptor ed, Entity instance, Connection conn, DbUtil dbUtil)
           
protected  int saveInstance(Entity instance, Connection conn, DbUtil dbUtil)
           
protected  void saveNamedChildren(EntityDescriptor ed, Entity instance, Connection conn, DbUtil dbUtil)
           
protected
<E extends Entity>
int
saveProperties(E instance, Collection<String> propertyNames, List<ConditionElement> args, Connection conn, DbUtil dbUtil, EntityDescriptor ed)
           
protected  void saveRelated(EntityDescriptor ed, Entity instance, Connection conn, DbUtil dbUtil)
           
protected  void saveSingleForeigns(EntityDescriptor ed, Entity instance, Connection conn, DbUtil dbUtil)
           
protected  void saveUnnamedChildren(EntityDescriptor ed, Entity instance, Connection conn, DbUtil dbUtil)
           
 void setDataSource(DataSource dataSource)
           
protected
<E extends Entity>
void
truncate(Connection conn, EntityDescriptor ed, DbUtil dbUtil)
           
 
Methods inherited from class de.schwarzrot.data.access.support.AbstractEntityManager
dumpTypeCache, getApplicationDefaults, getApplicationName, getEntityCache, getEntityIntrospector, getEntityUtils, getName, getSchemaName, getSchemaName, getUserNameProvider, loadClass, needsTransaction, setEntityCache, setEntityIntrospector, setEntityUtils, setSchemaName, setSchemaName, setUserNameProvider
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JDBCEntityManager

public JDBCEntityManager()

JDBCEntityManager

public JDBCEntityManager(DataSource ds)
Method Detail

commit

public void commit(TransactionResource<?> resource)
            throws Exception
Description copied from interface: EntityManager
used to persist all changes made during transaction.

Parameters:
resource - - an abstract handle to storage
Throws:
Exception - - depends on implementation

countInstances

public <E extends Entity> long countInstances(Class<E> entityType,
                                              Connection conn)

createTransactionResource

public TransactionResource<?> createTransactionResource(Transaction t)
                                                 throws Exception
Description copied from interface: EntityManager
create an abstract handle to storage. TransactionResource is a wrapper to handle jdbc and preferences the same way.

Parameters:
t - - the transaction
Returns:
- a wrapped storage handle
Throws:
Exception - - depends on implementation

execute

public void execute(TransactionalOperation<?> tao)
             throws Exception
Description copied from interface: EntityManager
execute a single step of ApplicationTransaction

Parameters:
tao - - the transactional operation to execute
Throws:
Exception - - depends on implementation

getDataSource

public final DataSource getDataSource()

getEntityDescriptor

public final EntityDescriptor getEntityDescriptor(Class<?> entityType,
                                                  Connection conn)

getEntityDescriptor

public final EntityDescriptor getEntityDescriptor(Class<?> entityType,
                                                  TransactionResource<?> res)
Description copied from interface: EntityManager
accessor to description instances of entities. An EntityDescriptor holds all information needed by an EntityManager to handle instances of that entity. The EntityDescriptor may vary between different EntityManager s.

Parameters:
entityType - - the class of that entity
res - - the wrapped storage handle
Returns:
- the descriptor for that entity

releaseTransactionResource

public void releaseTransactionResource(TransactionResource<?> resource)
Description copied from interface: EntityManager
free given storage handle

Parameters:
resource - - a wrapped storage handle

rollback

public void rollback(TransactionResource<?> resource)
Description copied from interface: EntityManager
discards changes of given storage handle and marks Transaction as failed.

Parameters:
resource - - a wrapped storage handle

setDataSource

public final void setDataSource(DataSource dataSource)

checkExistance

protected <E extends Entity> E checkExistance(E instance,
                                              Connection conn,
                                              DbUtil dbUtil)
checks whether the given instance already exists in persistence. The instance may exists by same id or same attributes, specified by getUniqueColumnNames

Type Parameters:
E - the type of the involed entity
Parameters:
instance - the instance to check
conn - the connection to persistence
dbUtil - the DbUtil of the connected database
Returns:
the found entity instance or null

createInstance

protected int createInstance(Entity instance,
                             Connection conn,
                             DbUtil dbUtil)

find

protected int find(List<?> resultList,
                   Class<?> entityType,
                   List<ConditionElement> args,
                   EntityDescriptor ed,
                   Connection conn,
                   DbUtil dbUtil,
                   boolean readRelated,
                   Collection<SortInfo> orderSequence)
internal call to read a number of entities. It maps to readProperties, cause there's no difference between reading the entire instance or only parts of it.

Parameters:
resultList - the list, that holds the read entities
entityType - the type of entities to read
args - the list of condition elements, to assemble the where clause
ed - the descriptor of the entity
conn - the connection to database
dbUtil - the DbUtil instance for the connected database
readRelated - flag to signal, whether related entities should be resolved
Returns:
the number of instances found for the given list of conditions

getEntity

protected <E extends Entity> E getEntity(E instance,
                                         Class<? extends Entity> entityType,
                                         long id,
                                         Connection conn)
internal call to resolve a given instance. Instance may by null, so a new one will be created. All references will be resolved.

Type Parameters:
E - the type of the given instance
Parameters:
instance - the instance to read/fill
entityType - the type of the given entity
id - the id of the entity to read
conn - the connection to database
Returns:
the read entity. May be the same as the given instance.

instanceCount

protected long instanceCount(Class<? extends Entity> entityType,
                             List<ConditionElement> args,
                             Connection conn)
                      throws Exception
Throws:
Exception

instanceFromResultSet

protected <E extends Entity> E instanceFromResultSet(ResultSet rs,
                                                     Connection conn,
                                                     EntityDescriptor ed,
                                                     DbUtil dbUtil,
                                                     boolean readRelated)

queryId

protected long queryId(EntityDescriptor ed,
                       Date now,
                       String userName,
                       Connection conn,
                       DbUtil dbUtil)

readProperties

protected int readProperties(List resultList,
                             Class entityType,
                             Collection<String> properties,
                             List<ConditionElement> args,
                             EntityDescriptor ed,
                             Connection conn,
                             DbUtil dbUtil,
                             int maxRows,
                             boolean readRelated,
                             boolean uniq,
                             Collection<SortInfo> orderSequence)

remove

protected <E extends Entity> int remove(Class<E> entityType,
                                        List<ConditionElement> args,
                                        EntityDescriptor ed,
                                        Connection conn,
                                        DbUtil dbUtil)
              throws Exception
Throws:
Exception

removeChildren

protected <E extends Entity> int removeChildren(E instance,
                                                EntityDescriptor ed,
                                                Connection conn,
                                                DbUtil dbUtil)

removeRelated

protected <E extends Entity> int removeRelated(Class<?> childEntityType,
                                               E instance,
                                               Connection conn,
                                               DbUtil dbUtil)

resolveGroupedUnnamedChildren

protected <E extends Entity> void resolveGroupedUnnamedChildren(E instance,
                                                                EntityDescriptor ed,
                                                                PropertyInfo pi,
                                                                Connection conn,
                                                                DbUtil dbUtil)

resolveNamedChildren

protected <E extends Entity> void resolveNamedChildren(E instance,
                                                       EntityDescriptor ed,
                                                       PropertyInfo pi,
                                                       Connection conn,
                                                       DbUtil dbUtil)

resolveReferences

protected <E extends Entity> void resolveReferences(E instance,
                                                    EntityDescriptor ed,
                                                    PropertyInfo pi,
                                                    Connection conn,
                                                    DbUtil dbUtil)

resolveRelated

protected <E extends Entity> void resolveRelated(EntityDescriptor ed,
                                                 E instance,
                                                 Connection conn,
                                                 DbUtil dbUtil)
resolve all relations, that could be defined for an Entity. The different relations are determined by the EntityDescriptor and recognized by the type of the property and the mapping definition for that property.

Type Parameters:
E - the derived type of the entity
Parameters:
ed - the descriptor of the entity
instance - the instance to populate
conn - the active db connection
dbUtil - the util instance for the target database

resolveSingleForeign

protected <E extends Entity> void resolveSingleForeign(E instance,
                                                       EntityDescriptor ed,
                                                       PropertyInfo pi,
                                                       Connection conn,
                                                       DbUtil dbUtil)

resolveUnnamedChildren

protected <E extends Entity> void resolveUnnamedChildren(E instance,
                                                         EntityDescriptor ed,
                                                         PropertyInfo pi,
                                                         Connection conn,
                                                         DbUtil dbUtil)

resolveWeightedReferences

protected <E extends Entity> void resolveWeightedReferences(E instance,
                                                            EntityDescriptor ed,
                                                            PropertyInfo pi,
                                                            Connection conn,
                                                            DbUtil dbUtil)

resultSet2EList

protected <E extends Entity> int resultSet2EList(ca.odell.glazedlists.EventList<E> resultList,
                                                 ResultSet rs,
                                                 Connection conn,
                                                 EntityDescriptor ed,
                                                 DbUtil dbUtil,
                                                 int maxRows,
                                                 boolean readRelated)

resultSet2List

protected <E extends Entity> int resultSet2List(List<E> resultList,
                                                ResultSet rs,
                                                Connection conn,
                                                EntityDescriptor ed,
                                                DbUtil dbUtil,
                                                int maxRows,
                                                boolean readRelated)

resultSet2TrList

protected <E extends Entity> int resultSet2TrList(ca.odell.glazedlists.TransactionList<E> resultList,
                                                  ResultSet rs,
                                                  Connection conn,
                                                  EntityDescriptor ed,
                                                  DbUtil dbUtil,
                                                  int maxRows,
                                                  boolean readRelated)

save

protected int save(Entity instance,
                   Connection conn,
                   DbUtil dbUtil)
internal call to bring an Entity to persistence. Checks, whether the instance already exists in persistence, or whether it has to be created. Saves all related entities as well.

Parameters:
instance - the entity to save
conn - the connection to database
dbUtil - the DbUtil for the connected database
Returns:
the number of saved instances - may be 0 or 1

saveForeigns

protected void saveForeigns(EntityDescriptor ed,
                            Entity instance,
                            Connection conn,
                            DbUtil dbUtil)

saveInstance

protected int saveInstance(Entity instance,
                           Connection conn,
                           DbUtil dbUtil)

saveNamedChildren

protected void saveNamedChildren(EntityDescriptor ed,
                                 Entity instance,
                                 Connection conn,
                                 DbUtil dbUtil)

saveProperties

protected <E extends Entity> int saveProperties(E instance,
                                                Collection<String> propertyNames,
                                                List<ConditionElement> args,
                                                Connection conn,
                                                DbUtil dbUtil,
                                                EntityDescriptor ed)
                      throws Exception
Throws:
Exception

saveRelated

protected void saveRelated(EntityDescriptor ed,
                           Entity instance,
                           Connection conn,
                           DbUtil dbUtil)

saveSingleForeigns

protected void saveSingleForeigns(EntityDescriptor ed,
                                  Entity instance,
                                  Connection conn,
                                  DbUtil dbUtil)

saveUnnamedChildren

protected void saveUnnamedChildren(EntityDescriptor ed,
                                   Entity instance,
                                   Connection conn,
                                   DbUtil dbUtil)

truncate

protected <E extends Entity> void truncate(Connection conn,
                                           EntityDescriptor ed,
                                           DbUtil dbUtil)
                 throws Exception
Throws:
Exception

SRJRCFrames
v0.1.296

hosted at
Find SRJRCFrames at SourceForge.net. Fast, secure and free:
           Open Source Software download
Submit a bug or request a feature

SRJRCFrames is published according to the GNU General Public License
Copyright 2005-2012 Reinhard Mantey - some rights reserved.