db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Armin Waibel <arm...@apache.org>
Subject auto-XXX setting confusion
Date Thu, 11 Mar 2004 13:56:46 GMT
Hi all,

recently several posts on the user-list discuss the behaviour of the
auto-XXX setting (especially auto-update 'true/false' for m:n relations) 
- what should be the expected behaviour?
The most confusing attribute is auto-update. Here my thoughts about 
auto-update false setting (on PB-level).

-------------------
auto-update 'false'
-------------------
Documentation says: "On updating or inserting an object with
PersistenceBroker.store(...) referenced objects are NOT updated by default."
If *auto-update false* is set in metadata I would expect that the
referenced objects are not being touched (modified) and not inserted 
when I perform an insert/update operation on the main object.

First I describe how OJB currently handle auto-update false setting and 
then I try to show an alternative handling.

main object with 1:1 relation
-----------------------------
OJB does not store the reference but assign the FK on the main object
(by reading the PK values of the reference) before storing.

Seems ok, but assume we have set auto-retrieve false and auto-update
false for class A with 1:1 reference to B. We lookup an A object (B 
reference will not be loaded) made change on A and call PB.store(A). 
What will happen? OJB assign the FK again but the reference is null and 
OJB assume the reference is nullified and set FK in A to 'null'. Thus we 
have to load all 1:1 references for A before we can update A.

On the other hand if we do not touch the references on insert/update
operation (auto-update false). Update of objects will not cause
problems, but we have to take care on insert operation, because we have
to set FK by hand.
Assume class A has a reference to class B and we use anonymous key for
reference A-->B, we want to store A and B. Then we can't set the FK in A 
(anonymous key field), rather we need a method like 'assignFK(Object 
obj)' which extract the PK values from B and set FK in A:

A.setB(B)
broker.beginTransaction
broker.store(B)
broker....assignOneToOneFK(A)
broker.store(A)
broker.commitTransaction


main object with 1:n relation
-----------------------------
OJB does not store the references but set the FK on all n-side reference
objects. If the n-side collection is 'null' nothing happens. On
insert/update for all materialzed n-side objects (proxies are skipped)
the FK to main object was set on each update operation of the main object.

On the other hand if we do not touch the references on insert/update
operation (auto-update false). Update of objects will not cause
problems, but we have to take care on insert operation, because we have
to set FK on all n-side objects by hand after storing the main object
and before storing the n-side objects.
Assume class A has a 1:n reference to class B:

broker.beginTransaction
broker.store(A)
A.setAllB(bList)
broker....assignOneToManyFK(A)
while(bListIterator.hasNext())
{
    broker.store(bListIterator.next())
}
broker.commitTransaction


main object with m:n relation
-----------------------------
OJB does not insert/update the references when main object was stored, but
- if reference collection is 'null' on update all indirection entires
will be removed (if collection is an unmaterialized proxy nothing happens)
Currently you will get an key constraint violation when try to insert
object A with m:n relation to B and A contains a list of B objects with
auto-update false, because OJB try to insert keys in the indirection 
table, but the B objects aren't in DB. With an empty B list insert pass.
If you use auto-retrieve false with auto-update true you will run into 
problems, because indirection table entries will be deleted on each 
store operation of main object, thus you will lost all m:n catenations.

On the other hand if we do not touch the references on insert/update
operation (auto-update false). Update of the main object will not cause 
problems, but we have to take care on insert operation

broker.beginTransaction
broker.store(A)
A.setAllB(bList)
while(bListIterator.hasNext())
{
    broker.store(bListIterator.next())
}
broker....assignManyToManyIndirectionTable(A)
broker.commitTransaction


Conclusion
----------
I agree with users posted that the current auto-XXX handling is in some 
cases not transparent and not clearly documented. Don't know if the 
proposed alternative behaviour will fit all users, but I think it will 
be more what you expect when auto-update is set to false.

What do you think?
Corrections, Comments, Clarifications and Proposals are very welcome.

regards,
Armin

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


Mime
View raw message