Hi,

I'm designing a java class which is mostly persisted in the database but
that also has a field that need to be 'persisted' using java code.  I was
wondering if 'transient="true"' in mapping.xml is the right thing for this
type of field... According to docs "transient" fields neither persist nor
marshal; I want it to marshal / unmarshal, but not persist in DB.  I *did*
make it work using 'transient="true"' the way I want it to, but I had to
modify SQLEngine.java and was wondering if it was the right thing to do...

Here is the portion of mapping.xml for my class:

  <class name="com.acme.File" identity="id" key-generator="oracle">
    <map-to table="docfile" xml="file" />
    <field name="id" type="integer">
      <sql type="numeric" name="docfile_id" />
      <bind-xml node="attribute" name="id" />
    </field>
    <field name="name" type="string">
      <sql type="char" name="docfile_name" />
      <bind-xml node="element" name="name" />
    </field>
    ...
    <field name="content" type="bytes" transient="true" required="false">
      <bind-xml type="xsd:base64Binary" node="element" name="content" />
    </field>
  </class>

The File class was originally generated using SourceGenerator, using XSD
that looks like this:

  <xsd:element name="file">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="name" type="xsd:string" maxOccurs="1" />
        <xsd:element name="location" type="xsd:string" maxOccurs="1" />
        <xsd:element name="content" type="xsd:base64Binary" minOccurs="0"
maxOccurs="1" />
      </xsd:sequence>
      <xsd:attribute name="id" type="xsd:integer" minOccurs="1"
maxOccurs="1" />
    </xsd:complexType>
  </xsd:element>

Now I also wrote event / listner classes that implement CallbackInterceptor
to intercept 'loading' event, so when com.acme.File object is loaded w/
SELECT statement, I can fill the "content" field with the java code (in this
case, I read files from local filesystem).  After that castor seems to take
care of marshaling the object to XML very nicely.  However in order for that
to work, I first had to fix SQLEngine.java so it doesn't cause
ClassCastException.  Apparently, around line1444 it tries and fails to cast
XMLFieldDescriptor to JDOFieldDescriptor, and I had to modify it so that it
doesn't add SQL columns for the "transient" field:

[org.exolab.castor.jdo.engine: 1440]
                // primitive field
                boolean isJDO = fieldDesc instanceof JDOFieldDescriptor;

                this.tableName = classTable;
                this.jdoName = fieldDesc.getFieldName();
                this.load = true;
                this.multi = false;
                this.joined = false;
                this.joinFields = null;

                this.store = !ext && isJDO &&
!((JDOFieldDescriptor)fieldDesc).isReadonly();
                this.dirtyCheck = isJDO &&
((JDOFieldDescriptor)fieldDesc).isDirtyCheck();

                FieldHandlerImpl fh = (FieldHandlerImpl)
fieldDesc.getHandler();

                if(!isJDO)
                  this.columns = new ColumnInfo[0];
                else {
                  this.columns = new ColumnInfo[1];
                  String[] sqlNameArray =
((JDOFieldDescriptor)fieldDesc).getSQLName();
                  String sqlName;
                  if ( sqlNameArray == null ) {
                      sqlName = fieldDesc.getFieldName();
                  } else {
                      sqlName = sqlNameArray[0];
                  }
                  this.columns[0] = new ColumnInfo( sqlName,
                          ((JDOFieldDescriptor)fieldDesc).getSQLType()[0],
fh.getConvertTo(),
                          fh.getConvertFrom(), fh.getConvertParam() );
                }

With this change apparently my code works fine. My question is, is this a
valid fix? Would this cause problems for other things (since I really don't
know what's going on with other parts of castor :-p)? Or is there some other
parameter I should've specified in mapping.xml, etc. for it to work without
modifying SQLEnginge.java?

Thanks,
Tomoki

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

Reply via email to