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 Da...@equifax.com on 2003/06/25 17:28:13 UTC
Getting the correct class instantiated
We would like to have a table that represents 3 or 4 different classes that
share a common base class. When objects are created out of the table, one
column in the table indicates which concrete class should be instantiated.
These objects are refered to by a handful of different classes. I thought
this was a perfect job for a RowReader, but the RowReader example seems to
be a case of instantiating two objects out of one row, and our attempts at
variations have not met with success.
What is the correct approach to this?
David
This message contains information from Equifax Inc. which may be
confidential and privileged. If you are not an intended recipient, please
refrain from any disclosure, copying, distribution or use of this
information and note that such actions are prohibited. If you have
received this transmission in error, please notify by e-mail
postmaster@equifax.com.
RE: Getting the correct class instantiated
Posted by Jim Shingler <sh...@iwaynet.net>.
I do the same thing.
Below you will find a segment of my row reader below.
Table
ContactPoint
ID
TypeID
EmailAddress
PhoneNumber
...
ID TypeID EmailAddress PhoneNumber
1 1 MY@Email.com
2 2 NA 123-345.6789
Type Table
ID
classOf
ID ClassOF
1 sd.loc.Email
2 sd.loc.PhoneNumber
Objects
ContactPoint
|- sd.loc.PhoneNumber
|- sd.loc.Email
Obvious disclaimers, I haven't had this reviewed by the experts yet.
Maybe some experts can comment.
ClassDescriptor cld = getClassDescriptor();
// check if there is an attribute which tells us which concrete
class is to be instantiated
//FieldDescriptor concreteClassFD =
cld.getFieldDescriptorByName(ClassDescriptor.OJB_CONCRETE_CLASS);
FieldDescriptor concreteClassFD =
cld.getFieldDescriptorByName("classOf");
FieldDescriptor concreteClassFD2 =
cld.getFieldDescriptorByName("typeID");
ClassDescriptor result = null;
// JLS IDEA:
// IF ClassOf isnt present check for TypeID and Load the underlying
TypeClass to get the "ClassOF"
// TypeID should be the preferred method
if (concreteClassFD == null && concreteClassFD2 == null)
return cld;
if (concreteClassFD2 != null)
{
// Load the Type and pull classof off it
Type typeQuery = new Type();
typeQuery.setObjID((String)
row.get(concreteClassFD2.getColumnName()));
Query query = new QueryByCriteria(typeQuery);
//uery.
Type type = null;
String concreteClass = null;
PersistenceBroker broker = null;
try
{
broker =
PersistenceBrokerFactory.defaultPersistenceBroker();
//broker.beginTransaction();
Object o =
broker.getObjectByQuery(query);
type =
(Type)ProxyHelper.getRealObject(o);
//broker.commitTransaction();
concreteClass = type.getClassOf();
if (concreteClass == null ||
concreteClass.trim().length() == 0)
{
throw new
PersistenceBrokerException(
"type.classOf
field returned null or 0-length string");
}
else
{
concreteClass =
concreteClass.trim();
}
result =
broker.getClassDescriptor(Class.forName(concreteClass,
true,
Thread.currentThread().getContextClassLoader()));
//PersistenceBrokerFactory.releaseInstance(broker);
//Constructor con =
result.getZeroArgumentConstructor();
//Object oo =
ConstructorHelper.instantiate(con);
//ClassDescriptor result =
cld.getRepository().getDescriptorFor(concreteClass);
if (result == null)
{
result = cld;
}
return result;
}
catch (PBFactoryException e)
{
throw new PersistenceBrokerException(e);
}
catch (Throwable t)
{
//broker.abortTransaction();
t.printStackTrace();
}
}
else if (concreteClassFD != null)
{
try
{
String concreteClass = (String)
row.get(concreteClassFD.getColumnName());
if (concreteClass == null ||
concreteClass.trim().length() == 0)
{
throw new PersistenceBrokerException(
"ojbConcreteClass field returned null or
0-length string");
}
else
{
concreteClass = concreteClass.trim();
}
result =
cld.getRepository().getDescriptorFor(concreteClass);
if (result == null)
{
result = cld;
}
return result;
}
catch (PBFactoryException e)
{
throw new PersistenceBrokerException(e);
}
}
return null;
}
-----Original Message-----
From: David.Corbin@equifax.com [mailto:David.Corbin@equifax.com]
Sent: Wednesday, June 25, 2003 11:28 AM
To: ojb-user@db.apache.org
Subject: Getting the correct class instantiated
We would like to have a table that represents 3 or 4 different classes
that share a common base class. When objects are created out of the
table, one column in the table indicates which concrete class should be
instantiated. These objects are refered to by a handful of different
classes. I thought this was a perfect job for a RowReader, but the
RowReader example seems to be a case of instantiating two objects out of
one row, and our attempts at variations have not met with success.
What is the correct approach to this?
David
This message contains information from Equifax Inc. which may be
confidential and privileged. If you are not an intended recipient,
please refrain from any disclosure, copying, distribution or use of this
information and note that such actions are prohibited. If you have
received this transmission in error, please notify by e-mail
postmaster@equifax.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org