The PersistenceBrokerAware is a special case of listener, so you probably would get the same behaviour. But why is it not working for you ? Depending on what you're interested in, you override afterUpdate and afterLookup. The latter is called when you traverse the collection via the iterator's next() method.
Tom

I really don't know, why it is not working for me and it's also a reason, why I'm asking you for help..


Possibly, there is something smelling in my code?

On the first run, it behaves OK:

not in db
test.Thing
 => stored

On the second run, the exception is thrown to demonstrate,
that Sale is not materialized completely (~ with Thing):

test.Sale
afterLookup
java.lang.UnsupportedOperationException: not completely initialized!
        at test.Sale.afterLookup(Unknown Source)
        ...

Before "afterLookup" there should be "test.Thing", informing about thing instantiation.

If instanitated via getObjectByQuery instead of getCollectionByQuery,
it works OK.

There are the demonstration classes:

package test;
import java.util.*;
import org.apache.ojb.broker.query.*;
import org.apache.ojb.broker.*;

public class Main
{
private static PersistenceBroker broker = PersistenceBrokerFactory.defaultPersistenceBroker();


    public Main()
    {
        // on the second run should materialize 1 instance
        Query query = new QueryByCriteria(Sale.class, null);
        Collection c = broker.getCollectionByQuery(query);
        // on first run stores the Sale with Thing
        if (c.size() == 0)
        {
            System.out.println("not in db");
            Thing t = new Thing();
            Sale s = new Sale(t);
            broker.beginTransaction();
            broker.store(s);
            System.out.println(" => stored");
            broker.commitTransaction();
        }
        else
        {
            System.out.println(c.iterator().next());
        }
        System.exit(0);
    }

    public static void main(String[] args)
    {
        new Main();
    }
}

package test;
import java.util.*;

/**
 * @ojb.class include-inherited="true"
 */
public class Sale implements org.apache.ojb.broker.PersistenceBrokerAware
{
    /**
     * Artificial ID key holder
     *
     * @ojb.field nullable="false"
     *            autoincrement="ojb"
     *            primarykey="true"
     */
    private Integer a_id;

    /**
     * Referential ID key holder
     *
     * @ojb.field
     */
    private Integer thing_id;

    /**
     *
     * @ojb.reference class-ref="test.Thing"
     *                foreignkey="thing_id"
     *                auto-retrieve="true"
     *                auto-update="true"
     *                auto-delete="true"
     */
    protected Thing thing;

    public Sale()
    {
        System.out.println(this.getClass().getName());
    }

    public Sale(Thing t)
    {
        this.thing = t;
    }

    public String toString()
    {
        return "s:"+thing.toString();
    }

public void beforeDelete(org.apache.ojb.broker.PersistenceBroker broker) throws org.apache.ojb.broker.PersistenceBrokerException { }
public void beforeInsert(org.apache.ojb.broker.PersistenceBroker broker) throws org.apache.ojb.broker.PersistenceBrokerException { }
public void beforeUpdate(org.apache.ojb.broker.PersistenceBroker broker) throws org.apache.ojb.broker.PersistenceBrokerException { }
public void afterUpdate(org.apache.ojb.broker.PersistenceBroker broker) throws org.apache.ojb.broker.PersistenceBrokerException { }
public void afterDelete(org.apache.ojb.broker.PersistenceBroker broker) throws org.apache.ojb.broker.PersistenceBrokerException { }
public void afterInsert(org.apache.ojb.broker.PersistenceBroker broker) throws org.apache.ojb.broker.PersistenceBrokerException { }
public void afterLookup(org.apache.ojb.broker.PersistenceBroker broker) throws org.apache.ojb.broker.PersistenceBrokerException
{
System.out.println("afterLookup");
if (thing == null)
throw new UnsupportedOperationException(
"not completely initialized!");
}
}


package test;
import java.util.*;

/**
 * @ojb.class include-inherited="true"
 */
public class Thing
{
    /**
     * Artificial ID key holder
     *
     * @ojb.field nullable="false"
     *            autoincrement="ojb"
     *            primarykey="true"
     */
    private Integer a_id;

    public Thing()
    {
        System.out.println(this.getClass().getName());
    }
}



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to