Im beginning with Castor and have a problem with loading instances of an
inherited class from the database via a generic Vector.
My classes are the following:

Product (generic class, table PRODUCT)

Computer extends Product (table COMPUTER)

ProductSuite (table PRODSUITE) - has a Vector of Products:     private
Vector vProduct;

When I create a ProductSuite with a Computer object on the Vector as
result I get 3 records - one on each table; thats why I think, that the
extends relationship is able to work properly.

The problem occurs when I try to load the ProductSuite from the database
and to remove it - now I get a ProductSuite object with a pure Product
object on the Vector vProduct and the Computer record will not be loaded
nor removed. What is my mistake? What do I have to change?

My source code (mapping file, Product.java, InheritanceTeste1.java):

*** mapping.xml ***

<!DOCTYPE databases PUBLIC "-//EXOLAB/Castor Mapping DTD Version
1.0//EN"
                           "http://castor.exolab.org/mapping.dtd";>
<mapping>

  <!--  Mapping for ProductSuite  -->
  <class name="inheritancetest1.ProductSuite"
         identity="id">
    <description>Product definition</description>
    <map-to table="prodsuite" xml="productsuite" />
    <field name="id" type="integer">
      <sql name="id" type="integer" />
      <xml name="id" node="attribute"/>
    </field>
    <field name="suitename" type="string">
      <sql name="suitename" type="char" />
      <xml name="suitename" node="attribute" />
    </field>

    <!-- Zugangsstelle has reference to Product
         many products per productsuite  -->
    <field name="vProduct" type="inheritancetest1.Product"
required="true"
           collection="vector">
      <sql many-key="prodsuite_id"/>
      <xml name="product" node="element" />
    </field>


  </class>


  <!--  Mapping for Product  -->
  <class name="inheritancetest1.Product"
         identity="id" depends="inheritancetest1.ProductSuite"
         subclass-field="subclass">
    <description>Product definition</description>
    <map-to table="prod" xml="product" />
    <field name="id" type="integer">
      <sql name="id" type="integer" />
      <xml name="id" node="attribute"/>
    </field>
    <field name="name" type="string">
      <sql name="name" type="char" />
      <xml name="name" node="attribute" />
    </field>
    <field name="price" type="float">
      <sql name="price" type="numeric" />
      <xml name="price" node="attribute" />
    </field>
    <field name="productsuite_id" type="integer">
      <sql name="prodsuite_id" type="integer" />
      <xml name="prodsuite_id" node="attribute"/>
    </field>
    <field name="subclass" type="string">
      <sql name="subclass" type="char" />
      <xml name="subclass" node="element" />
    </field>

  </class>


  <!--  Mapping for Computer, extending Product  -->
  <class name="inheritancetest1.Computer"
extends="inheritancetest1.Product" identity="id">
    <description>Computer definition, extends generic
product</description>
    <map-to table="computer" xml="computer" />
    <field name="id" type="integer">
      <sql name="id" type="integer" />
      <xml name="id" node="attribute"/>
    </field>
    <field name="cpu" type="string">
      <sql name="cpu" type="char"/>
      <xml name="cpu" node="attribute" />
    </field>
  </class>


</mapping>


*** Product.java ***

/* Generated by Together */

package inheritancetest1;

import java.util.Vector;
import java.util.Enumeration;
import org.exolab.castor.jdo.Database;
import org.exolab.castor.jdo.Persistent;

public class Product implements Persistent {

    private int          _id;


    private String       _name;


    private float        _price;


    private int          _productsuite_id;


    private String       _subclass;


    private Database     _db;


    public int getId()
    {
        return _id;
    }


    public void setId( int id )
    {
        _id = id;
    }


    public String getName()
    {
        return _name;
    }


    public void setName( String name )
    {
        _name = name;
    }


    public float getPrice()
    {
        return _price;
    }


    public void setPrice( float price )
    {
        _price = price;
    }

    public int getProductsuite_id()
    {
        return _productsuite_id;
    }

    public void setProductsuite_id( int productsuite_id )
    {
        _productsuite_id = productsuite_id;
    }

    public String getSubclass()
    {
        return _subclass;
    }


    public void setSubclass( String subclass )
    {
        _subclass = subclass;
    }


    public void jdoPersistent( Database db )
    {
        _db = db;
    }


    public void jdoTransient()
    {
        _db = null;
    }


    public Class jdoLoad(short accessMode)
    {
        if ( _subclass.indexOf("Computer") >= 0 ) {
            return Computer.class;
        }
        return null;
    }


    public void jdoBeforeCreate( Database db )
    {
    }


    public void jdoAfterCreate()
    {
    }


    public void jdoStore(boolean modified)
    {
    }


    public void jdoBeforeRemove()
    {
    }


    public void jdoAfterRemove()
    {
    }


    public void jdoUpdate()
    {
    }


    public String toString()
    {
        return _id + " " + _name;
    }

}


*** InheritanceTest1.java ***

/* Generated by Together */

package inheritancetest1;


import java.util.Hashtable;
import java.util.Vector;
import java.util.Enumeration;
import java.io.PrintWriter;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.io.FileOutputStream;
import java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.DriverManager;
import javax.sql.*;
import java.sql.*;
import org.xml.sax.ContentHandler;
import org.exolab.castor.jdo.JDO;
import org.exolab.castor.jdo.Database;
import org.exolab.castor.jdo.OQLQuery;
import org.exolab.castor.jdo.QueryResults;
import org.exolab.castor.util.Logger;
import org.exolab.castor.xml.Marshaller;
import org.exolab.castor.mapping.Mapping;
import org.apache.xml.serialize.*;
import oracle.jdbc.*;


public class InheritanceTest1 {
    public static final String DatabaseFile = "database.xml";
    public static final String MappingFile = "mapping.xml";
    public static final String Usage = "Usage: example jdo";
    private Mapping  _mapping;
    private JDO      _jdo;

    public static void main( String[] args )
    {
        PrintWriter   writer;
        InheritanceTest1          test;

//        writer = new Logger( System.out ).setPrefix( "test" );
        writer = new Logger( System.out ).setPrefix( "test" );
        try {
            test = new InheritanceTest1 ( writer );
            test.run( writer );
        } catch ( Exception except ) {
            writer.println( except );
            except.printStackTrace( writer );
        }
    }


    public InheritanceTest1( PrintWriter writer )
        throws Exception
    {
        // Load the mapping file
        _mapping = new Mapping( getClass().getClassLoader() );
        _mapping.setLogWriter( writer );
        _mapping.loadMapping( /*getClass().getResource(*/ MappingFile
/*)*/ );

        _jdo = new JDO();
        _jdo.setLogWriter( writer );
        _jdo.setConfiguration( /*getClass().getResource(*/ DatabaseFile
/*).toString()*/ );
        _jdo.setDatabaseName( "test" );
    }


    public void run( PrintWriter writer )
        throws Exception
    {
        Database      db;
        ProductSuite  productsuite;
        Product       product;
        Computer      computer;
        OQLQuery      productsuiteOql;
        OQLQuery      productOql;
        OQLQuery      computerOql;
        QueryResults  results;

        db = _jdo.getDatabase();

        db.begin();
        writer.println( "Begin transaction" );

        // Look up the product and if found in the database,
        // delete this object from the database
        productsuiteOql = db.getOQLQuery( "SELECT p FROM
inheritancetest1.ProductSuite p WHERE id = $1" );
        productsuiteOql.bind( 2 );
        results = productsuiteOql.execute();
        while ( results.hasMore() ) {
            productsuite = (ProductSuite) results.next();
            writer.println( "Deleting existing productsuite: " +
productsuite );
            db.remove(  productsuite );
        }


        // Checkpoint commits all the updates to the database
        // but leaves the transaction (and locks) open
        writer.println( "Transaction checkpoint" );
        db.commit();

        db.begin();

        // If no such product exists in the database, create a new
        // object and persist it
        // Note: product uses group, so group object has to be
        //       created first, but can be persisted later
        productsuiteOql.bind( 2 );
        results = productsuiteOql.execute();
        if ( ! results.hasMore() ) {
   productsuite = new ProductSuite();
            productsuite.setId( 2 );
            productsuite.setSuitename( "product suite A" );

            computer = new Computer();
            computer.setId( 6 );
            computer.setCpu( "Pentium" );
            computer.setName( "MyPC" );
            computer.setPrice( 300 );
            computer.setSubclass("inheritancetest1.Computer");
   productsuite.addVProduct(computer);

            writer.println( "Creating new productsuite: " + productsuite
);
            db.create( productsuite );
        } else {
            writer.println( "Query result: " + results.next() );
        }

        // If no such computer exists in the database, create a new
        // object and persist it
        // Note: computer uses group, so group object has to be
        //       created first, but can be persisted later
        writer.println( "Commit transaction" );
        db.commit();


        db.close();
    }

}

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

Reply via email to