Expressions & objects state

From: Razumovsky Andrey (hmai..otmail.ru)
Date: Sat Mar 01 2008 - 04:55:15 EST

  • Next message: Andrus Adamchik: "Re: State of DataObjects (uncommitted but nonexistent on DB)"

    Hi all,

    these are actually two separate questions.

    First is about expressions. Suppose i have two entities, A and B. A has pk 'aid' and fk (non-mandatory) 'bid'. B has pk 'bid'.
    I want to build two separate expressions for A like those (1) 'aid=1' and (2) 'bid=2'. Since 'aid' and 'bid' are not properties of A class, statements like this:
    Expression.fromString("aid=1") results in exception. With 'bid' the sutiation is event harder, because while i write something like 'b.bid=2', b relation target might event not exist.
    I know i can do this using 'db:' prefix, but this is not a solution for me, because i'm using ROP and 'db:' do not work on client since DBEntities are not being sent through hessian.
    So how i can create such queries (note than i prefer using SelectQuery, because there might be other qualifiers in my expression)??

    Other thing i discovered about expresions is they are type-dependent, so that if 'count' is integer property, Expression.fromString("count='1'").match(...) (note quotes here!) will never work because it will search for string 'count' property. This is no good because all databases i know support such automatic type conversion. So SelectQuery will work ok, but simple match() will not.
    Reading comments in 'ASTEqual', 'ASTLess' and other, i saw i'm not the first to ask this. But i think this is quite easy to implement (at least converting from String to Number), so i'd ask for this feature.
    ==========
    Second, about state of objects.
    I create new transient object 'A' and then set some EXISTING relation target of B class:

    //b is persistent in db and fetched using SelectQuery or something else
    A a = new A();
    a.setB(b);

    The reason i do this is somewhat like the previuos question in the list. I need to persist objects only when user presses 'Save', not when my data object is created.

    So, when i do this, several things occur. First, 'b' state is changed to MODIFIED since relationship affects both sides. But note that 'b' now refers to unexisting and non-'NEW' object. Second, a's temp id is added to ObjectContextStateLog's dirtyIds!
    So right now these functions will definitely crash in NPE:
    -context.commitChanges()
    -context.newObjects,
    since temp id presents in dirtyIds but node is not registered in GraphManager.

    In my local version of cayenne i managed to do some workaround by changing ObjectContextStateLog and ObjectContextChangeLog so that they will not mark dirty anything while working with transient objects. However, probably context.registerObject(a) will not work correctly, but i don't need that, instead i create 'new' object and fill it with 'transient' one properties:
    A a2 = (A)context.newObject(A.class);
    a2.setB(a.getB())
    context.commitChanges().

    This works fine for me, but i think something must be done around collapsing 'transient' and 'in-scope' (registered) objects.

    Thanks,
    Andrey)



    This archive was generated by hypermail 2.0.0 : Sat Mar 01 2008 - 04:55:49 EST