Java Application Development
Java Application Development
AbstractEntityList
+add()
+remove()
PersonNames_ImpI
interface
SimpIeEntityList
interface
PersonNames
56
Creating an Entity
To create a new Business Entity, you create the DTO first
. or you create an empty DTO from an "Id" cIass .
. and then create the Business Entity from the DTO
Person_DTO perDTO = (Person_DTO) createDTO(Person.class);
Person_Id perId = new Person_Id("1185477091");
Person_DTO perDTO = perId.newDTO();
Note: createDTO() is inherited from the parent GenericBusinessObject cIass
for aII Business Entity or Business Component cIasses (e.g. Change HandIers)
perDTO.setId(new Person_Id(1185477091));
perDTO.set...
Person person = perDTO.newEntity();
57
DeIeting an Entity
To deIete a Business Entity, you use the delete method on the entity .
. or you can deIete the resuIts of a query using the delete method on
Query
person.delete();
Query query = createQuery("from Person");
long rowsDeleted = query.delete();
58
Hibernate for SPL Framework - HQL
HQL queries must aIso go through the SPL Framework
The framework Query cIass impIements a subset of pure
HQL
The framework HQL queries return Business Entities
It is mostIy the same as the native HQL Ianguage, except
for a few differences .
The SELECT cIause is not aIIowed in the framework's HQL text
The ORDER BY cIause is not aIIowed in the framework's HQL text
UNIONs are catered for in the framework's HQL (native HQL does
not aIIow it)
The framework caters for raw SQL statements
The foIIowing sIides describe the framework's
impIementation of HQL
Querying Entities
60
SeIecting Entities
The SELECT cIause is not used in a framework Query caII
SimiIar to Hibernate, the foIIowing query is vaIid
In this exampIe, the List wiII contain the aIgorithm objects
seIected for the aIgorithm type (i.e. Business Entities are
returned)
AlgorithmType algorithmType = ... ;
Query query = createQuery("from Algorithm algorithm +
where algorithm.algorithmType = :algorithmType");
query.bindEntity("algorithmType", algorithmType);
List algorithms = query.list();
61
Specifying ResuIts
To seIect individuaI coIumns, you specify the "resuIt"
properties programmaticaIIy, giving each a unique name
The addResult method on the Query object specifies a
resuIt property (coIumn) to seIect
Now the List wiII contain QueryResultRow objects
Each QueryResultRow wiII have two vaIues, keyed by
"aIgorithm" and "aIgorithmId"
Query query = createQuery("from Algorithm algorithm+
where algorithm.algorithmType = :algorithmType");
query.bindEntity("algorithmType", algorithmType);
query.addResult("algorithm", "algorithm");
query.addResult("algorithmId", "algorithm.id");
List queryResults = query.list();
62
Ordering ResuIts
The ORDER BY cIause in HQL is not aIIowed by the
framework
Instead, the order must be programmaticaIIy specified .
The List wiII contain QueryResultRow objects, ordered by
"aIgorithmId"
Query query = createQuery("from Algorithm algorithm+
where algorithm.algorithmType = :algorithmType");
query.bindEntity("algorithmType", algorithmType);
query.addResult("algorithm", "algorithm");
query.addResult("algorithmId", "algorithm.id");
query.orderBy(algorithmId, Query.A$ENDING);
List queryResults = query.list();
63
Using Query Iterators
For a Iarge voIume query, instead of using a List, better
performance can be gained using the iterate method on
Query
This is more efficient because the iterator Ioads objects on
demand
It aIso performs better if the objects have aIready been
Ioaded and cached by the session
Query query = createQuery("from Person");
Person perEntity = null;
QueryIterator qIter = query.iterate();
while (qIter.hasNext()) {
perEntity = (Person)qIter.next();
...
}
64
Union Queries
Hibernate does not provide for unions (it does not fit in with the object-oriented
principIes of HQL)
However, unions can be usefuI so the framework aIIows you to programmaticaIIy
code a union
Unioned queries must have identicaI .
ResuIt coIumns
Order by cIauses
Max resuIts settings
The unionWith() method of the Query cIass performs the initiaI union
To add a third (or Iater) query, you add it directIy to the union
&nionQuery union = query.unionWith(query2);
union.addQueryTo&nion(query3);
65
PIease note that this is not a recommended or preferred means of accessing the
database
Raw SQL
It may be necessary to code SQL instead of HQL
PotentiaI reasons are .
To specify performance hints
A tabIe is not mapped to a Java entity
Instead of createQuery(), you use the JDBC-Iike
createPreparedStatement() method on the session object
The text is simiIar to the HQL Query text, but you use the
tabIe and coIumn names instead of the Java entity and
property names
This feature is onIy mentioned here for information - we do
not cover it in the exercises
66
Extending Business Entities
Entities can be extended by appIications "higher" on the
appIication stack. For exampIe, the "cm" appIication can
extend "ccb" entities.
The BusinessEntity annotation has a speciaI parameter
to define this: extendedInterfaceName
OnIy new business methods can be added. Existing ones
may not be overridden or removed.
WaIk-through: User_ImpI in CCB
67
COBOL-Backed Entities
There are compIex COBOL row programs in the CCB
appIication that maintain tabIe data
So not to dupIicate this Iogic between COBOL and Java,
these row programs are "wrapped" by BusinessEntity
cIasses.
These cIasses can be interacted with just Iike any other
entity: queried, changed, etc
The persistence mechanism has been changed though
so that rather than Hibernate persisting changes, the
COBOL programs are caIIed instead
WaIk-through: ServiceAgreement in CCB
End of Day 1
69
Day 2 Agenda
Change HandIers-Standard VaIidations
Change HandIers
Standard VaIidations
71
Change HandIers
The way to change an entity's response to changes in it's
state is by creating Change HandIers
VaIidations disaIIow inappropriate modification
Cascading changes aIIow one change to cause another
One or more Change HandIers may exist for a Business
Entity - the framework wiII caII aII of them
The Business Entity is specified in the annotations - it
teIIs the framework which entity to attach the Change
HandIer to at runtime
A Change HandIer extends the AbstractChangeHandIer
framework cIass
A Change HandIer cIass name MUST end in "_CHandIer"
for the artifact generator to recognize it as such
72
ExampIe Change HandIer