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 Aldo Luis Aguirre <ag...@y2knetworks.com.ar> on 2004/08/03 01:34:21 UTC
Proplems Using a Single Proxy for a Whole Collection
Hi,
I have a problem, I have a class named Product (just like tutorial with
an relation M:N):
-------------------------------------
package org.apache.ojb.tutorials;
import java.util.*;
import org.apache.ojb.broker.util.collections.ManageableArrayList;
import org.apache.ojb.broker.core.proxy.ListProxyDefaultImpl;
import org.apache.ojb.broker.PersistenceBroker;
import org.apache.ojb.broker.PersistenceBrokerException;
import org.apache.ojb.broker.PersistenceBrokerFactory;
/**
* Represents product objects in the tutorial system
*
* @ojb.class
*/
public class Product implements java.io.Serializable
{
/**
* Artificial primary key atribute
*
* @ojb.field primarykey="true"
* autoincrement="ojb"
*/
private Integer id;
/**
* Product name
*
* @ojb.field length="100"
*/
protected String name;
/**
* Price per item
*
* @ojb.field
*/
protected double price;
/**
* Stock of currently available items
*
* @ojb.field
*/
protected int stock;
/**
* Returns the id
*
* @return The id
*/
/**
* @ojb.collection element-class-ref="org.apache.ojb.tutorials.Pieza"
* auto-retrieve="true"
* auto-update="true"
* auto-delete="none"
* indirection-table="PRODUCT_PIEZA"
* foreignkey="PRODUCT_ID"
* remote-foreignkey="PIEZA_ID"
* proxy="true"
*/
protected ListProxyDefaultImpl piezas=new ListProxyDefaultImpl(
PersistenceBrokerFactory.defaultPersistenceBroker().getPBKey(),null);
//protected ManageableArrayList piezas=new ManageableArrayList();
public int getId()
{
return id.intValue();
}
/**
* Returns the name of the product.
*
* @return The name
*/
public String getName()
{
return name;
}
/**
* Returns the price of the product.
*
* @return The price
*/
public double getPrice()
{
return price;
}
/**
* Returns the number of available items of this product.
*
* @return The number of items in stock
*/
public int getStock()
{
return stock;
}
/**
* Sets the id of the product.
*
* @param newId The new id
*/
public void setId(int newId)
{
id = new Integer(newId);
}
/**
* Sets the name of the product.
*
* @param newName The new name
*/
public void setName(String newName)
{
name = newName;
}
/**
* Sets the price of the product
*
* @param newPrice The new price
*/
public void setPrice(double newPrice)
{
price = newPrice;
}
/**
* Sets the number of available items of this product.
*
* @param newStock The number of available items
*/
public void setStock(int newStock)
{
stock = newStock;
}
/**
* Returns a string representation of the product.
*
* @return The string representation
*/
public String toString()
{
return "[" + id + "] " + name + "\t\t\t price: " + price + "\t\t
stock: " + stock;
}
public void addPieza(Pieza pieza) {
piezas.ojbAdd((Object)pieza);
}
public Collection getPiezas() {
return piezas;
}
}
-------------------------------
and I modified PBExample.java like this:
-----------------------------------
.
.
.
System.out.println("2a. Find product by template, used template:
" + template);
System.out.println("ReCargando Piezas");
product.setId(41);
Product aProduct = findByTemplate(product);
System.out.println("Fin de ReCarga de Piezas");
System.out.println("2b. Found product: " + aProduct);
System.out.println();
System.out.println("Comienzo a ver las pieza de adentro");
System.out.println("Tengo: " + aProduct.getPiezas().size());
Iterator it = aProduct.getPiezas().iterator();
while(it.hasNext()) {
Pieza pieza = (Pieza)it.next();
System.out.println("Pieza: " + pieza);
}
System.out.println("Fin de impresion de piezas");
.
.
.
-----------------------
the repository_user.xml is:
----------------------
<class-descriptor
class="org.apache.ojb.tutorials.Pieza"
table="Pieza"
>
<field-descriptor
name="id"
column="id"
jdbc-type="INTEGER"
primarykey="true"
autoincrement="true"
>
</field-descriptor>
<field-descriptor
name="name"
column="name"
jdbc-type="VARCHAR"
length="100"
>
</field-descriptor>
<field-descriptor
name="price"
column="price"
jdbc-type="FLOAT"
>
</field-descriptor>
<field-descriptor
name="stock"
column="stock"
jdbc-type="INTEGER"
>
</field-descriptor>
<collection-descriptor
name="productos"
collection-class="org.apache.ojb.broker.util.collections.ManageableArrayList"
element-class-ref="org.apache.ojb.tutorials.Product"
indirection-table="PRODUCT_PIEZA"
auto-retrieve="true"
auto-update="true"
auto-delete="none"
>
<fk-pointing-to-this-class column="PIEZA_ID"/>
<fk-pointing-to-element-class column="PRODUCT_ID"/>
</collection-descriptor>
</class-descriptor>
<class-descriptor
class="org.apache.ojb.tutorials.Product"
table="Product"
>
<field-descriptor
name="id"
column="id"
jdbc-type="INTEGER"
primarykey="true"
autoincrement="true"
>
</field-descriptor>
<field-descriptor
name="name"
column="name"
jdbc-type="VARCHAR"
length="100"
>
</field-descriptor>
<field-descriptor
name="price"
column="price"
jdbc-type="FLOAT"
>
</field-descriptor>
<field-descriptor
name="stock"
column="stock"
jdbc-type="INTEGER"
>
</field-descriptor>
<collection-descriptor
name="piezas"
collection-class="org.apache.ojb.broker.core.proxy.ListProxyDefaultImpl"
element-class-ref="org.apache.ojb.tutorials.Pieza"
indirection-table="PRODUCT_PIEZA"
proxy="true"
auto-retrieve="true"
auto-update="true"
auto-delete="none"
>
<fk-pointing-to-this-class column="PRODUCT_ID"/>
<fk-pointing-to-element-class column="PIEZA_ID"/>
</collection-descriptor>
</class-descriptor>
---------------------------
I can get the number of pieces, but when I want to show them, It gives
me the follow error:
--------------------------
2a. Find product by template, used template: [null]
Sprocket price: 0.0 stock: 0
ReCargando Piezas
Fin de ReCarga de Piezas
2b. Found product: [41] Sprocket price:
1.99 stock: 7
Comienzo a ver las pieza de adentro
Number of pieces: 3000 // that's OK
[org.apache.ojb.broker.core.QueryReferenceBroker] ERROR:
org.apache.ojb.broker.core.proxy.ListProxyDefaultImpl
java.lang.InstantiationException:
org.apache.ojb.broker.core.proxy.ListProxyDefaultImpl
at java.lang.Class.newInstance0(Class.java:293)
at java.lang.Class.newInstance(Class.java:261)
at
org.apache.ojb.broker.core.QueryReferenceBroker.getCollectionByQuery(Unknown
Source)
at
org.apache.ojb.broker.core.QueryReferenceBroker.getCollectionByQuery(Unknown
Source)
at
org.apache.ojb.broker.core.PersistenceBrokerImpl.getCollectionByQuery(Unknown
Source)
at
org.apache.ojb.broker.core.DelegatingPersistenceBroker.getCollectionByQuery(Unknown
Source)
at
org.apache.ojb.broker.core.DelegatingPersistenceBroker.getCollectionByQuery(Unknown
Source)
at
org.apache.ojb.broker.core.proxy.CollectionProxyDefaultImpl.loadData(Unknown
Source)
at
org.apache.ojb.broker.core.proxy.ListProxyDefaultImpl.loadData(Unknown
Source)
at
org.apache.ojb.broker.core.proxy.CollectionProxyDefaultImpl.getData(Unknown
Source)
at
org.apache.ojb.broker.core.proxy.CollectionProxyDefaultImpl.iterator(Unknown
Source)
at org.apache.ojb.tutorials.PBExample.main(Unknown Source)
java.lang.InstantiationException:
org.apache.ojb.broker.core.proxy.ListProxyDefaultImpl
at java.lang.Class.newInstance0(Class.java:293)
at java.lang.Class.newInstance(Class.java:261)
at
org.apache.ojb.broker.core.QueryReferenceBroker.getCollectionByQuery(Unknown
Source)
at
org.apache.ojb.broker.core.QueryReferenceBroker.getCollectionByQuery(Unknown
Source)
at
org.apache.ojb.broker.core.PersistenceBrokerImpl.getCollectionByQuery(Unknown
Source)
at
org.apache.ojb.broker.core.DelegatingPersistenceBroker.getCollectionByQuery(Unknown
Source)
at
org.apache.ojb.broker.core.DelegatingPersistenceBroker.getCollectionByQuery(Unknown
Source)
at
org.apache.ojb.broker.core.proxy.CollectionProxyDefaultImpl.loadData(Unknown
Source)
at
org.apache.ojb.broker.core.proxy.ListProxyDefaultImpl.loadData(Unknown
Source)
at
org.apache.ojb.broker.core.proxy.CollectionProxyDefaultImpl.getData(Unknown
Source)
at
org.apache.ojb.broker.core.proxy.CollectionProxyDefaultImpl.iterator(Unknown
Source)
at org.apache.ojb.tutorials.PBExample.main(Unknown Source)
rethrown as org.apache.ojb.broker.PersistenceBrokerException:
org.apache.ojb.broker.core.proxy.ListProxyDefaultImpl
at
org.apache.ojb.broker.core.QueryReferenceBroker.getCollectionByQuery(Unknown
Source)
at
org.apache.ojb.broker.core.QueryReferenceBroker.getCollectionByQuery(Unknown
Source)
at
org.apache.ojb.broker.core.PersistenceBrokerImpl.getCollectionByQuery(Unknown
Source)
at
org.apache.ojb.broker.core.DelegatingPersistenceBroker.getCollectionByQuery(Unknown
Source)
at
org.apache.ojb.broker.core.DelegatingPersistenceBroker.getCollectionByQuery(Unknown
Source)
at
org.apache.ojb.broker.core.proxy.CollectionProxyDefaultImpl.loadData(Unknown
Source)
at
org.apache.ojb.broker.core.proxy.ListProxyDefaultImpl.loadData(Unknown
Source)
at
org.apache.ojb.broker.core.proxy.CollectionProxyDefaultImpl.getData(Unknown
Source)
at
org.apache.ojb.broker.core.proxy.CollectionProxyDefaultImpl.iterator(Unknown
Source)
at org.apache.ojb.tutorials.PBExample.main(Unknown Source)
Caused by: java.lang.InstantiationException:
org.apache.ojb.broker.core.proxy.ListProxyDefaultImpl
at java.lang.Class.newInstance0(Class.java:293)
at java.lang.Class.newInstance(Class.java:261)
... 10 more
Exception: org.apache.ojb.broker.core.proxy.ListProxyDefaultImpl
--------------------
I'm really sorry to bored you, but in the page are not an clear example
about how to use the proxy, thank for help a newbe :)
Regards,
Luis
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org
Re: Proplems Using a Single Proxy for a Whole Collection
Posted by Armin Waibel <ar...@apache.org>.
Aldo Luis Aguirre wrote:
> Hi,
> I have a problem, I have a class named Product (just like tutorial with
> an relation M:N):
> -------------------------------------
> package org.apache.ojb.tutorials;
> import java.util.*;
> import org.apache.ojb.broker.util.collections.ManageableArrayList;
> import org.apache.ojb.broker.core.proxy.ListProxyDefaultImpl;
>
> import org.apache.ojb.broker.PersistenceBroker;
> import org.apache.ojb.broker.PersistenceBrokerException;
> import org.apache.ojb.broker.PersistenceBrokerFactory;
>
> /**
> * Represents product objects in the tutorial system
> *
> * @ojb.class
> */
> public class Product implements java.io.Serializable
> {
> /**
> * Artificial primary key atribute
> *
> * @ojb.field primarykey="true"
> * autoincrement="ojb"
> */
> private Integer id;
>
> /**
> * Product name
> *
> * @ojb.field length="100"
> */
> protected String name;
>
> /**
> * Price per item
> *
> * @ojb.field
> */
> protected double price;
>
> /**
> * Stock of currently available items
> *
> * @ojb.field
> */
> protected int stock;
>
> /**
> * Returns the id
> *
> * @return The id
> */
> /**
> * @ojb.collection element-class-ref="org.apache.ojb.tutorials.Pieza"
> * auto-retrieve="true"
> * auto-update="true"
> * auto-delete="none"
> * indirection-table="PRODUCT_PIEZA"
> * foreignkey="PRODUCT_ID"
> * remote-foreignkey="PIEZA_ID"
> * proxy="true"
> */
> protected ListProxyDefaultImpl piezas=new ListProxyDefaultImpl(
>
> PersistenceBrokerFactory.defaultPersistenceBroker().getPBKey(),null);
??? why do you declare a proxy class for field 'piezas'. Your persistent
object don't need to know anything about proxy classes.
http://db.apache.org/ojb/docu/guides/basic-technique.html#Using+a+Single+Proxy+for+a+Whole+Collection
http://db.apache.org/ojb/docu/guides/xdoclet-module.html#Collections
try
protected List piezas;
or
protected List piezas = new ArrayList();
Then OJB should internal use ManageableArrayList or ListProxyDefaultImpl
when proxy is true.
If you use a collection proxy there is no difference in mapping except
the 'proxy="true"' instead 'false' flag.
regards,
Armin
> //protected ManageableArrayList piezas=new
> ManageableArrayList();
> public int getId()
> {
> return id.intValue();
> }
>
> /**
> * Returns the name of the product.
> *
> * @return The name
> */
> public String getName()
> {
> return name;
> }
>
> /**
> * Returns the price of the product.
> *
> * @return The price
> */
> public double getPrice()
> {
> return price;
> }
>
> /**
> * Returns the number of available items of this product.
> *
> * @return The number of items in stock
> */
> public int getStock()
> {
> return stock;
> }
>
> /**
> * Sets the id of the product.
> *
> * @param newId The new id
> */
> public void setId(int newId)
> {
> id = new Integer(newId);
> }
>
> /**
> * Sets the name of the product.
> *
> * @param newName The new name
> */
> public void setName(String newName)
> {
> name = newName;
> }
>
> /**
> * Sets the price of the product
> *
> * @param newPrice The new price
> */
> public void setPrice(double newPrice)
> {
> price = newPrice;
> }
>
> /**
> * Sets the number of available items of this product.
> *
> * @param newStock The number of available items
> */
> public void setStock(int newStock)
> {
> stock = newStock;
> }
>
> /**
> * Returns a string representation of the product.
> *
> * @return The string representation
> */
> public String toString()
> {
> return "[" + id + "] " + name + "\t\t\t price: " + price + "\t\t
> stock: " + stock;
> }
> public void addPieza(Pieza pieza) {
> piezas.ojbAdd((Object)pieza);
> }
> public Collection getPiezas() {
> return piezas;
> }
> }
> -------------------------------
>
> and I modified PBExample.java like this:
> -----------------------------------
> .
> .
> .
> System.out.println("2a. Find product by template, used template:
> " + template);
> System.out.println("ReCargando Piezas");
> product.setId(41);
> Product aProduct = findByTemplate(product);
> System.out.println("Fin de ReCarga de Piezas");
> System.out.println("2b. Found product: " + aProduct);
> System.out.println();
> System.out.println("Comienzo a ver las pieza de adentro");
> System.out.println("Tengo: " + aProduct.getPiezas().size());
> Iterator it = aProduct.getPiezas().iterator();
> while(it.hasNext()) {
> Pieza pieza = (Pieza)it.next();
> System.out.println("Pieza: " + pieza);
> }
> System.out.println("Fin de impresion de piezas");
> .
> .
> .
> -----------------------
> the repository_user.xml is:
> ----------------------
> <class-descriptor
> class="org.apache.ojb.tutorials.Pieza"
> table="Pieza"
> >
> <field-descriptor
> name="id"
> column="id"
> jdbc-type="INTEGER"
> primarykey="true"
> autoincrement="true"
> >
> </field-descriptor>
> <field-descriptor
> name="name"
> column="name"
> jdbc-type="VARCHAR"
> length="100"
> >
> </field-descriptor>
> <field-descriptor
> name="price"
> column="price"
> jdbc-type="FLOAT"
> >
> </field-descriptor>
> <field-descriptor
> name="stock"
> column="stock"
> jdbc-type="INTEGER"
> >
> </field-descriptor>
> <collection-descriptor
> name="productos"
>
> collection-class="org.apache.ojb.broker.util.collections.ManageableArrayList"
>
> element-class-ref="org.apache.ojb.tutorials.Product"
> indirection-table="PRODUCT_PIEZA"
> auto-retrieve="true"
> auto-update="true"
> auto-delete="none"
> >
> <fk-pointing-to-this-class column="PIEZA_ID"/>
> <fk-pointing-to-element-class column="PRODUCT_ID"/>
> </collection-descriptor>
> </class-descriptor>
> <class-descriptor
> class="org.apache.ojb.tutorials.Product"
> table="Product"
> >
> <field-descriptor
> name="id"
> column="id"
> jdbc-type="INTEGER"
> primarykey="true"
> autoincrement="true"
> >
> </field-descriptor>
> <field-descriptor
> name="name"
> column="name"
> jdbc-type="VARCHAR"
> length="100"
> >
> </field-descriptor>
> <field-descriptor
> name="price"
> column="price"
> jdbc-type="FLOAT"
> >
> </field-descriptor>
> <field-descriptor
> name="stock"
> column="stock"
> jdbc-type="INTEGER"
> >
> </field-descriptor>
> <collection-descriptor
> name="piezas"
>
> collection-class="org.apache.ojb.broker.core.proxy.ListProxyDefaultImpl"
> element-class-ref="org.apache.ojb.tutorials.Pieza"
> indirection-table="PRODUCT_PIEZA"
> proxy="true"
> auto-retrieve="true"
> auto-update="true"
> auto-delete="none"
> >
> <fk-pointing-to-this-class column="PRODUCT_ID"/>
> <fk-pointing-to-element-class column="PIEZA_ID"/>
> </collection-descriptor>
> </class-descriptor>
> ---------------------------
> I can get the number of pieces, but when I want to show them, It gives
> me the follow error:
> --------------------------
> 2a. Find product by template, used template: [null]
> Sprocket price: 0.0 stock: 0
> ReCargando Piezas
> Fin de ReCarga de Piezas
> 2b. Found product: [41] Sprocket price:
> 1.99 stock: 7
>
> Comienzo a ver las pieza de adentro
> Number of pieces: 3000 // that's OK
> [org.apache.ojb.broker.core.QueryReferenceBroker] ERROR:
> org.apache.ojb.broker.core.proxy.ListProxyDefaultImpl
> java.lang.InstantiationException:
> org.apache.ojb.broker.core.proxy.ListProxyDefaultImpl
> at java.lang.Class.newInstance0(Class.java:293)
> at java.lang.Class.newInstance(Class.java:261)
> at
> org.apache.ojb.broker.core.QueryReferenceBroker.getCollectionByQuery(Unknown
> Source)
> at
> org.apache.ojb.broker.core.QueryReferenceBroker.getCollectionByQuery(Unknown
> Source)
> at
> org.apache.ojb.broker.core.PersistenceBrokerImpl.getCollectionByQuery(Unknown
> Source)
> at
> org.apache.ojb.broker.core.DelegatingPersistenceBroker.getCollectionByQuery(Unknown
> Source)
> at
> org.apache.ojb.broker.core.DelegatingPersistenceBroker.getCollectionByQuery(Unknown
> Source)
> at
> org.apache.ojb.broker.core.proxy.CollectionProxyDefaultImpl.loadData(Unknown
> Source)
> at
> org.apache.ojb.broker.core.proxy.ListProxyDefaultImpl.loadData(Unknown
> Source)
> at
> org.apache.ojb.broker.core.proxy.CollectionProxyDefaultImpl.getData(Unknown
> Source)
> at
> org.apache.ojb.broker.core.proxy.CollectionProxyDefaultImpl.iterator(Unknown
> Source)
> at org.apache.ojb.tutorials.PBExample.main(Unknown Source)
> java.lang.InstantiationException:
> org.apache.ojb.broker.core.proxy.ListProxyDefaultImpl
> at java.lang.Class.newInstance0(Class.java:293)
> at java.lang.Class.newInstance(Class.java:261)
> at
> org.apache.ojb.broker.core.QueryReferenceBroker.getCollectionByQuery(Unknown
> Source)
> at
> org.apache.ojb.broker.core.QueryReferenceBroker.getCollectionByQuery(Unknown
> Source)
> at
> org.apache.ojb.broker.core.PersistenceBrokerImpl.getCollectionByQuery(Unknown
> Source)
> at
> org.apache.ojb.broker.core.DelegatingPersistenceBroker.getCollectionByQuery(Unknown
> Source)
> at
> org.apache.ojb.broker.core.DelegatingPersistenceBroker.getCollectionByQuery(Unknown
> Source)
> at
> org.apache.ojb.broker.core.proxy.CollectionProxyDefaultImpl.loadData(Unknown
> Source)
> at
> org.apache.ojb.broker.core.proxy.ListProxyDefaultImpl.loadData(Unknown
> Source)
> at
> org.apache.ojb.broker.core.proxy.CollectionProxyDefaultImpl.getData(Unknown
> Source)
> at
> org.apache.ojb.broker.core.proxy.CollectionProxyDefaultImpl.iterator(Unknown
> Source)
> at org.apache.ojb.tutorials.PBExample.main(Unknown Source)
> rethrown as org.apache.ojb.broker.PersistenceBrokerException:
> org.apache.ojb.broker.core.proxy.ListProxyDefaultImpl
> at
> org.apache.ojb.broker.core.QueryReferenceBroker.getCollectionByQuery(Unknown
> Source)
> at
> org.apache.ojb.broker.core.QueryReferenceBroker.getCollectionByQuery(Unknown
> Source)
> at
> org.apache.ojb.broker.core.PersistenceBrokerImpl.getCollectionByQuery(Unknown
> Source)
> at
> org.apache.ojb.broker.core.DelegatingPersistenceBroker.getCollectionByQuery(Unknown
> Source)
> at
> org.apache.ojb.broker.core.DelegatingPersistenceBroker.getCollectionByQuery(Unknown
> Source)
> at
> org.apache.ojb.broker.core.proxy.CollectionProxyDefaultImpl.loadData(Unknown
> Source)
> at
> org.apache.ojb.broker.core.proxy.ListProxyDefaultImpl.loadData(Unknown
> Source)
> at
> org.apache.ojb.broker.core.proxy.CollectionProxyDefaultImpl.getData(Unknown
> Source)
> at
> org.apache.ojb.broker.core.proxy.CollectionProxyDefaultImpl.iterator(Unknown
> Source)
> at org.apache.ojb.tutorials.PBExample.main(Unknown Source)
> Caused by: java.lang.InstantiationException:
> org.apache.ojb.broker.core.proxy.ListProxyDefaultImpl
> at java.lang.Class.newInstance0(Class.java:293)
> at java.lang.Class.newInstance(Class.java:261)
> ... 10 more
> Exception: org.apache.ojb.broker.core.proxy.ListProxyDefaultImpl
> --------------------
> I'm really sorry to bored you, but in the page are not an clear example
> about how to use the proxy, thank for help a newbe :)
>
> Regards,
>
> Luis
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
> For additional commands, e-mail: ojb-user-help@db.apache.org
>
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org