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 Emil Alexandrov <Em...@rila.com> on 2003/06/25 17:06:57 UTC

Delete problem with QueryByCriteria

Hi I'm new in using OJB and so far I just try to follow the examples in
tutorials.
I'm using OJB 1.0.rc3 and MySQL DB.
In my code which is copied and modified a little from tutorial1.html
section - Deleting Object I noticed the following problem.
When I use 
Query query = new QueryByCriteria(example);
 to create query which will return the object that I want to delete
nothing happens.
But if I use 
Query query = new QueryByIdentity(example);
The record is found and successfully deleted.
In the example in tutorial1.html QueryByCriteria is used.
Can someone explain what I do wrong? And is there a way to see what SQL
statement is generated in both cases? 
Here is the complete code I'm using:
    public void deleteUser(int userID) {
        PersistenceBroker broker = null;
        UserData example = new UserData();
        example.setUserID(userID);
        // 2. build a QueryByCriteria from this sample instance:
        Query query = new QueryByCriteria(example);
        try
        {
            broker =
PersistenceBrokerFactory.defaultPersistenceBroker();

            // start broker transaction
            broker.beginTransaction();
            // lookup the product specified by the QBE
            UserData toBeDeleted = (UserData)
broker.getObjectByQuery(query);
            // now ask broker to delete the object
            broker.delete(toBeDeleted);
            // commit transaction
            broker.commitTransaction();
        }
        catch (Throwable t)
        {
            // rollback in case of errors
            broker.abortTransaction();
            t.printStackTrace();
        }
    }

10x
Emil

Re: Delete problem with QueryByCriteria

Posted by Jakob Braeuchi <jb...@gmx.ch>.
hi emil,

QueryByCriteria(example) build a query based on _all_ non-null instvars of the example object whereas QueryByIdentity(example) uses the pk-values only. 

hth
jakob



Emil Alexandrov wrote:

>Hi I'm new in using OJB and so far I just try to follow the examples in
>tutorials.
>I'm using OJB 1.0.rc3 and MySQL DB.
>In my code which is copied and modified a little from tutorial1.html
>section - Deleting Object I noticed the following problem.
>When I use 
>Query query = new QueryByCriteria(example);
> to create query which will return the object that I want to delete
>nothing happens.
>But if I use 
>Query query = new QueryByIdentity(example);
>The record is found and successfully deleted.
>In the example in tutorial1.html QueryByCriteria is used.
>Can someone explain what I do wrong? And is there a way to see what SQL
>statement is generated in both cases? 
>Here is the complete code I'm using:
>    public void deleteUser(int userID) {
>        PersistenceBroker broker = null;
>        UserData example = new UserData();
>        example.setUserID(userID);
>        // 2. build a QueryByCriteria from this sample instance:
>        Query query = new QueryByCriteria(example);
>        try
>        {
>            broker =
>PersistenceBrokerFactory.defaultPersistenceBroker();
>
>            // start broker transaction
>            broker.beginTransaction();
>            // lookup the product specified by the QBE
>            UserData toBeDeleted = (UserData)
>broker.getObjectByQuery(query);
>            // now ask broker to delete the object
>            broker.delete(toBeDeleted);
>            // commit transaction
>            broker.commitTransaction();
>        }
>        catch (Throwable t)
>        {
>            // rollback in case of errors
>            broker.abortTransaction();
>            t.printStackTrace();
>        }
>    }
>
>10x
>Emil
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
>For additional commands, e-mail: ojb-user-help@db.apache.org
>
>
>  
>