hello,

i would like to populate a database with objects from an xml file, e.g.:

<?xml version="1.0" encoding="UTF-8"?>
<root>
<PropertyType><name>VOLUME</name><description>Integer number of any kind of transactions.</description><id>3</id></PropertyType>
<PropertyType><name>PRICE</name><description>The price of a given equity in a given currency</description><id>2</id></PropertyType>
<PropertyType><name>DATETIME</name><description>The datetime at which an observation was made.</description><id>1</id></PropertyType>
</root>


i am therefore iterating over all child elements of <root> which may be of arbitrary type:
for(int i = 0; i < len; i++) {
Node node = nl.item(i);
if(node.getNodeType() != Node.ELEMENT_NODE)
continue;
System.out.println("node[" + i + "]: " + node.getNodeName());
xmlobject = unmarshaller.unmarshal(node);



at this point i would like to find out if the object already exists in the database or if i have to create it. my initial attempt was doing it via an OQL query:


String query = "SELECT obj FROM " + xmlobject.getClass().getName() + " obj WHERE obj = $1 ";
System.out.println("query: " + query);
try {
oql = db.getOQLQuery(query);
oql.bind(xmlobject);
results = oql.execute();
already_exists = results.hasMore();
} finally {
if(null != oql)
oql.close();
}


but with this query i get the following exception:
query: SELECT obj FROM net.sf.ojts.jdo.PropertyType obj WHERE obj = $1
org.exolab.castor.jdo.QueryException: The field obj was not found.
at org.exolab.castor.jdo.oql.ParseTreeWalker.checkField(ParseTreeWalker.java:645)
at org.exolab.castor.jdo.oql.ParseTreeWalker.checkWhereClause(ParseTreeWalker.java:578)
at org.exolab.castor.jdo.oql.ParseTreeWalker.checkWhereClause(ParseTreeWalker.java:590)
at org.exolab.castor.jdo.oql.ParseTreeWalker.checkWhereClause(ParseTreeWalker.java:590)
at org.exolab.castor.jdo.oql.ParseTreeWalker.checkErrors(ParseTreeWalker.java:236)
at org.exolab.castor.jdo.oql.ParseTreeWalker.<init>(ParseTreeWalker.java:137)
at org.exolab.castor.jdo.engine.OQLQueryImpl.create(OQLQueryImpl.java:261)
at org.exolab.castor.jdo.engine.DatabaseImpl.getOQLQuery(DatabaseImpl.java:439)
at CastorReadXMLTest.main(CastorReadXMLTest.java:115)


i tried to debug the exception and i think the root cause of this problem is that castor only allows for fields in the WHERE clause, but not for the _fromClassAlias itself. look at the following code snipet from the class ParseTreeWalker (my modifications are marked with >):


class ParseTreeWalker: private JDOFieldDescriptor checkField(ParseTreeNode fieldTree) throws QueryException {

//see if we've checked this field before.
JDOFieldDescriptor field = (JDOFieldDescriptor)_fieldInfo.get(fieldTree);
if ( field != null )
return field;


if ( fieldTree.getToken().getTokenType() == DOT ) {
field = checkProjection( fieldTree, false, false );
} else {
field = getFieldDesc( fieldTree.getToken().getTokenValue(), _clsDesc );
if (field != null)
_fieldInfo.put(fieldTree, field);
> if(_fromClassAlias.equals(fieldTree.getToken().getTokenValue()))
> return null;
}
if (field == null)
throw new QueryException( "The field " + fieldTree.getToken().getTokenValue() + " was not found." );
return field;


  }

i did not know what to return where i put a "return null;"?

my following attempt was to use the db.load() method:

        boolean already_exists = false;
        PropertyType pt = (PropertyType) xmlobject;
        Object loaded = null;
        try {
                loaded = db.load(xmlobject.getClass(), new Integer(pt.getId()));
        } catch (ObjectNotFoundException e) {
                // ignored
        }
        if(null != loaded)
                already_exists = true;

but this approach has the disadvantage that i first of all have to know which identifier is used to identify objects in the db, e.g. i have to know that the identifier is an Integer, and all of my objects would have to implement some kind of interface, e.g. HasIntegerId, in order to cast them to this type and then call the getId() method on them.

is there some other way to find out if a given object is already created in the database or not, without the need to know anything else about a given object, e.g. without the need to derive from a common interface and without the need to cast it?

thanks a lot for any infos!!
--
Christian Schuhegger
http://www.el-chef.de/



----------------------------------------------------------- If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
unsubscribe castor-dev

Reply via email to