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 BI...@HanseCom.com on 2004/02/13 14:20:03 UTC

Casting an Object to a subclass





Hello,

I am using the ODMG API.
i don't know whether this behaviour is to be expected:

I am mapping a child class to it's parent class via multiple joined tables.
When I retrive objects of the parent class from the Database, some of the
returned objects should be members of a child class.
But if i ask  for the objects of the parent class, I only get them as
instances of the parent class.
In the spy.log - File I can see that only parent objects are retrieved.

What can I do to get the "real" objects?




The repository:
<class-descriptor class="test.Point" table="point">
      <field-descriptor id="1"      name="id"         column="id"
jdbc-type="INTEGER"
                        primarykey="true"       autoincrement="true"
access="anonymous"/>
      <field-descriptor id="2"      name="region"           column="region"
      jdbc-type="VARCHAR"/>
</class-descriptor>
<class-descriptor class="test.NamedPoint" table="namedpoint">
      <field-descriptor id="1"      name="id"         column="id"
jdbc-type="INTEGER"
                        primarykey="true"       autoincrement="true"
access="anonymous"/>
      <field-descriptor id="2"      name="name"             column="name"
jdbc-type="VARCHAR"/>
      <reference-descriptor name="super" class-ref="test.Point">
<foreignkey field-ref="id" /> </reference-descriptor>
</class-descriptor>

the table definitions:
CREATE TABLE point (id INT PRIMARY KEY, region VARCHAR (20)) ;
CREATE TABLE namedpoint (id INT PRIMARY KEY,name VARCHAR (20)) ;

the main - Method (Class definitions are as expected from the repository):
      public static void main(String[] args) {
            // Open Database
            Implementation theOdmg = OJB.getInstance();
            Database theDb = theOdmg.newDatabase();
            try {
                  theDb.open("default", Database.OPEN_READ_WRITE);
            } catch (ODMGException ex) {
                  ex.printStackTrace();
            }

            //Create a NamedPoint
            Transaction tx = theOdmg.newTransaction();
            tx.begin();
            NamedPoint np = new NamedPoint();
            np.setRegion("region");
            np.setName("test");
            tx.lock(np, Transaction.WRITE);
            tx.commit();

            //get Point
            tx = theOdmg.newTransaction();
            tx.begin();
            OQLQuery query = theOdmg.newOQLQuery();
            DList pointList = null;
            try {
                  String queryString =
                        "select all from " + Point.class.getName() + "
where region =\"region\"";
                  query.create(queryString);
                  pointList = (DList) query.execute();

                  java.util.Iterator iter = pointList.iterator();
                  while (iter.hasNext()) {
                        Point point = (Point) iter.next();
                        if (point instanceof NamedPoint) {
                              System.out.println("Named Point!");
                        } else {
                              System.out.println("No Named Point!");
                        }
                  }
            } catch (QueryInvalidException ex) {
                  ex.printStackTrace();
            } catch (QueryException ex) {
                  ex.printStackTrace();
            }
      }

the text on the Screen:
No Named Point!

thanks,
Birgitta

random crashes in OJB RC5 ClassCastException for addThisListenerTo

Posted by Reda Benzair <re...@smartjog.com>.
I am experimenting random crashes in OJB RC5 : 

ClassCastException in 
QueryReferenceBroker$PBCollectionProxyListener.addThisListenerTo(CollectionProxy ) trying to cast into CollectionProxy:
        protected void addThisListenerTo(Object listenedObject)
        {
+		/*MODIFICATION AWAITING OJB'sTEAM REAL CORRECTION*/
+		if(listenedObject==null || !(listenedObject instanceof
CollectionProxy))
+		{
+			log.warn("The listenedObject is no
CollectionProxy. Cannot get registered! "  + listenedObject);
+		}
+		else
+		{
      	      _listenedCollection = (CollectionProxy) listenedObject;
	            _listenedCollection.addListener(this);
+		}
+		/*END MODIFICATION AWAITING OJB'sTEAM REAL CORRECTION*/
        }

It seems to work OK since it applied this modification. Do you have a
better solution? Does this change entail hidden drawbacks? Why is this
method called on a non CollectionProxy Object? Is another bug the source
of it?
We user OJB in our Production environment and are pretty satisfied with
it (except maybe when it comes to this bug or to M:N relation with
attributes...  )
thinks All for your response !!!


---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Re: Casting an Object to a subclass

Posted by Armin Waibel <ar...@apache.org>.
Hi,

I never used this feature and don't know much about it, but some things 
may problematic:

test.Point, field-descriptor for PK "id" is declared anonymous

test.NamedPoint, field-descriptor for PK "id" is anonymous
and no seperate reference field for reference (PK and reference
id field are the same)

regards,
Armin

BIRGITTA.MOEHRING@HanseCom.com wrote:
> 
> 
> 
> 
> Hello,
> 
> I am using the ODMG API.
> i don't know whether this behaviour is to be expected:
> 
> I am mapping a child class to it's parent class via multiple joined tables.
> When I retrive objects of the parent class from the Database, some of the
> returned objects should be members of a child class.
> But if i ask  for the objects of the parent class, I only get them as
> instances of the parent class.
> In the spy.log - File I can see that only parent objects are retrieved.
> 
> What can I do to get the "real" objects?
> 
> 
> 
> 
> The repository:

<class-descriptor class="test.Point" table="point">

<field-descriptor
id="1"
name="id"
column="id"
jdbc-type="INTEGER"
primarykey="true"
autoincrement="true"
access="anonymous"/>

<field-descriptor id="2"
name="region"
column="region"
jdbc-type="VARCHAR"/>
</class-descriptor>


<class-descriptor class="test.NamedPoint" table="namedpoint">
<field-descriptor id="1"
name="id"
column="id"
jdbc-type="INTEGER"
primarykey="true"
autoincrement="true"
access="anonymous"/>


<field-descriptor id="2"
name="name"
column="name"
jdbc-type="VARCHAR"/>

<reference-descriptor name="super" class-ref="test.Point">
> <foreignkey field-ref="id" /> 
</reference-descriptor>

> </class-descriptor>
> 
> the table definitions:
> CREATE TABLE point (id INT PRIMARY KEY, region VARCHAR (20)) ;
> CREATE TABLE namedpoint (id INT PRIMARY KEY,name VARCHAR (20)) ;
> 
> the main - Method (Class definitions are as expected from the repository):
>       public static void main(String[] args) {
>             // Open Database
>             Implementation theOdmg = OJB.getInstance();
>             Database theDb = theOdmg.newDatabase();
>             try {
>                   theDb.open("default", Database.OPEN_READ_WRITE);
>             } catch (ODMGException ex) {
>                   ex.printStackTrace();
>             }
> 
>             //Create a NamedPoint
>             Transaction tx = theOdmg.newTransaction();
>             tx.begin();
>             NamedPoint np = new NamedPoint();
>             np.setRegion("region");
>             np.setName("test");
>             tx.lock(np, Transaction.WRITE);
>             tx.commit();
> 
>             //get Point
>             tx = theOdmg.newTransaction();
>             tx.begin();
>             OQLQuery query = theOdmg.newOQLQuery();
>             DList pointList = null;
>             try {
>                   String queryString =
>                         "select all from " + Point.class.getName() + "
> where region =\"region\"";
>                   query.create(queryString);
>                   pointList = (DList) query.execute();
> 
>                   java.util.Iterator iter = pointList.iterator();
>                   while (iter.hasNext()) {
>                         Point point = (Point) iter.next();
>                         if (point instanceof NamedPoint) {
>                               System.out.println("Named Point!");
>                         } else {
>                               System.out.println("No Named Point!");
>                         }
>                   }
>             } catch (QueryInvalidException ex) {
>                   ex.printStackTrace();
>             } catch (QueryException ex) {
>                   ex.printStackTrace();
>             }
>       }
> 
> the text on the Screen:
> No Named Point!
> 
> thanks,
> Birgitta

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org