You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-user@db.apache.org by Brian Chaplin <bc...@chaplinsolutions.com> on 2003/06/20 20:38:00 UTC
tutorial 3 afterStore() instance callback updated for version 1.0
I updated the code in this tutorial to run with version 1.0:
public abstract class DBAutoIncremented
implements PersistenceBrokerAware {
private static final String ID_ATTRIBUTE_NAME = "m_id";
public void afterDelete(PersistenceBroker broker)
throws PersistenceBrokerException {
}
public void afterInsert(PersistenceBroker broker)
throws PersistenceBrokerException {
// remove object from cache to ensure we are retrieving a
// copy that is in sync with the database.
broker.removeFromCache(this);
Class clazz = getClass();
ClassDescriptor cld = broker.getClassDescriptor(clazz);
PersistentField idField =
cld
.getFieldDescriptorByName(ID_ATTRIBUTE_NAME)
.getPersistentField();
if (hasNotBeenSet(idField)) {
// retrieve the object again with a query
// on all non-id attributes.
Object object =
broker.getObjectByQuery(
buildQueryOnAllNonIdAttributes(clazz, cld));
if (object == null) {
throw new PersistenceBrokerException(
"cannot assign ID to "
+ this
+ " ("
+ clazz
+ ")"
+ " because lookup by attributes failed");
}
// set id attribute with the value
// assigned by the database.
idField.set(this, idField.get(object));
}
}
public void afterLookup(PersistenceBroker broker)
throws PersistenceBrokerException {
}
public void afterUpdate(PersistenceBroker broker)
throws PersistenceBrokerException {
}
public void beforeDelete(PersistenceBroker broker)
throws PersistenceBrokerException {
}
public void beforeInsert(PersistenceBroker broker)
throws PersistenceBrokerException {
}
public void beforeUpdate(PersistenceBroker broker)
throws PersistenceBrokerException {
}
/**
* after storing a new instance reflect the
* autoincremented PK value
* back into the PK attribute.
*/
public void afterStore() {
PersistenceBroker broker =
PersistenceBrokerFactory.defaultPersistenceBroker();
try {
// remove object from cache to ensure we are retrieving a
// copy that is in sync with the database.
broker.removeFromCache(this);
Class clazz = getClass();
ClassDescriptor cld = broker.getClassDescriptor(clazz);
PersistentField idField =
cld
.getFieldDescriptorByName(ID_ATTRIBUTE_NAME)
.getPersistentField();
if (hasNotBeenSet(idField)) {
// retrieve the object again with a query
// on all non-id attributes.
Object object =
broker.getObjectByQuery(
buildQueryOnAllNonIdAttributes(clazz, cld));
if (object == null) {
throw new PersistenceBrokerException(
"cannot assign ID to "
+ this
+ " ("
+ clazz
+ ")"
+ " because lookup by attributes failed");
}
// set id attribute with the value
// assigned by the database.
idField.set(this, idField.get(object));
}
} finally {
PersistenceBrokerFactory.releaseAllInstances();
}
}
/**
* returns a query that identifies an object by all its non-
* primary key attributes.
* this method is only safe, if these values are unique!
*/
private Query buildQueryOnAllNonIdAttributes(
Class clazz,
ClassDescriptor cld) {
// note: these are guaranteed to be in the same order
FieldDescriptor[] fields = cld.getFieldDescriptions();
Criteria crit = new Criteria();
for (int i = 0; i < fields.length; i++) {
if (!fields[i].getAttributeName().equals(ID_ATTRIBUTE_NAME)) {
Object value = fields[i].getPersistentField().get(this);
if (value == null) {
crit.addIsNull(fields[i].getAttributeName());
} else {
crit.addEqualTo(fields[i].getAttributeName(), value);
}
}
}
return QueryFactory.newQuery(clazz, crit);
}
/**
* returns true if attribute idField == 0,
* else false.
*/
private boolean hasNotBeenSet(PersistentField idField) {
return (((Integer) idField.get(this)).intValue() == 0);
}
}