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 Galvin Hsiu <gk...@yahoo.com> on 2003/04/25 00:23:14 UTC
Bug in RC1/RC2 with fix
Interesting small bug in RC1 / RC2 when attempting to
persist m:n collections.
This occurs when one attempts to store an object in
which its m:n child relationships is null, notably
when arrays[] are mapped to a m:n table. If the
object already exists in the DB and already has m:n
relationships, an attempt on storing the same item
with null m:n references gets successfully put in the
cache as such BUT DOES NOT GET DELETED IN THE DB.
For example:
public class TestObject
{
private Long id;
private ChildObject[] children;
public void setId(Long id)
{...}
public Long getId()
{...}
public void setChildren(ChildObject[] objects)
{...}
public ChildObject[] getChildren()
{...}
}
When attempting to call the following:
// store the item initially
TestObject a = new TestObject();
a.setId(new Long(9));
a.setChildren( new ChildObject(...));
// restore the same item, this time disassociate
the m:n relationship
a = new TestObject();
a.setId(new Long(9));
a.setChildren(null);
Assuming that the OJB cache is set to anything other
than EmptyCacheImpl, broker retrievals to the same
object will yield that the children is null. However,
a careful query to the DB outside of OJB will reveal
that the M:N relationship table still exists!
The solutions is to remove all m:n implementors if the
m:n references is null.
Possible Fix (taken from cvs):
private void storeCollections(Object obj, Vector
vecCds) throws PersistenceBrokerException
{
// get all members of obj that are collections
and store all their elements
Iterator i = vecCds.iterator();
while (i.hasNext())
{
CollectionDescriptor cds =
(CollectionDescriptor) i.next();
Object col =
cds.getPersistentField().get(obj);
Collection currentMtoNKeys = null;
// BEGIN FIX
if (col == null)
{
if (cds.isMtoNRelation())
{
// delete all rows from m:n table
belonging to obj
deleteMtoNImplementor(cds, obj);
}
}
// END FIX
else if (col != null)
{
// MBAIRD
// if the collection is a
collectionproxy and it's not already loaded
// no need to store it.
if (col instanceof CollectionProxy &&
!((CollectionProxy) col).isLoaded())
{
continue;
}
if (cds.isMtoNRelation())
{
currentMtoNKeys =
getMtoNImplementor(cds, obj);
// delete unused m:n implementors
deleteMtoNImplementor(cds, obj,
(Collection)col, currentMtoNKeys);
}
Iterator colIterator;
if (col instanceof
ManageableCollection)
{
colIterator =
((ManageableCollection) col).ojbIterator();
}
else if (col instanceof Collection)
{
colIterator = ((Collection)
col).iterator();
}
else if (col.getClass().isArray())
{
colIterator = new
ArrayIterator(col);
}
else
{
throw new OJBRuntimeException(
col.getClass()
+ " can not be managed by
OJB, use Array, Collection or ManageableCollection
instead !");
}
while (colIterator.hasNext())
{
Object otherObj =
colIterator.next();
// for m:n mapped collections
store association implementing entries
if (cds.isMtoNRelation())
{
// 1. Store depended upon
object first to avoid FK violation
storeCollectionObject(cds,
otherObj);
// 2. Store indirection record
storeMtoNImplementor(cds, obj,
otherObj, currentMtoNKeys);
}
// for 1:n mapped collection
assert proper fk assignment
else
{
assertFkAssignment(otherObj,
obj, cds);
storeCollectionObject(cds,
otherObj);
}
}
// invoke callback on collection
if (col instanceof
ManageableCollection)
{
((ManageableCollection)
col).afterStore(this);
}
}
}
}
__________________________________________________
Do you Yahoo!?
The New Yahoo! Search - Faster. Easier. Bingo
http://search.yahoo.com
Re: Bug in RC1/RC2 with fix
Posted by Jakob Braeuchi <jb...@gmx.ch>.
hi galvin,
thanks for your fix. i commited it to the repository.
jakob
Galvin Hsiu wrote:
>Interesting small bug in RC1 / RC2 when attempting to
>persist m:n collections.
>
>This occurs when one attempts to store an object in
>which its m:n child relationships is null, notably
>when arrays[] are mapped to a m:n table. If the
>object already exists in the DB and already has m:n
>relationships, an attempt on storing the same item
>with null m:n references gets successfully put in the
>cache as such BUT DOES NOT GET DELETED IN THE DB.
>
>For example:
>
>public class TestObject
>{
> private Long id;
> private ChildObject[] children;
>
> public void setId(Long id)
> {...}
> public Long getId()
> {...}
> public void setChildren(ChildObject[] objects)
> {...}
> public ChildObject[] getChildren()
> {...}
>}
>
>When attempting to call the following:
>
> // store the item initially
> TestObject a = new TestObject();
> a.setId(new Long(9));
> a.setChildren( new ChildObject(...));
>
> // restore the same item, this time disassociate
>the m:n relationship
>
> a = new TestObject();
> a.setId(new Long(9));
> a.setChildren(null);
>
>Assuming that the OJB cache is set to anything other
>than EmptyCacheImpl, broker retrievals to the same
>object will yield that the children is null. However,
>a careful query to the DB outside of OJB will reveal
>that the M:N relationship table still exists!
>
>The solutions is to remove all m:n implementors if the
>m:n references is null.
>
>Possible Fix (taken from cvs):
>
>private void storeCollections(Object obj, Vector
>vecCds) throws PersistenceBrokerException
> {
> // get all members of obj that are collections
>and store all their elements
> Iterator i = vecCds.iterator();
> while (i.hasNext())
> {
> CollectionDescriptor cds =
>(CollectionDescriptor) i.next();
> Object col =
>cds.getPersistentField().get(obj);
> Collection currentMtoNKeys = null;
>
> // BEGIN FIX
> if (col == null)
> {
> if (cds.isMtoNRelation())
> {
> // delete all rows from m:n table
>belonging to obj
> deleteMtoNImplementor(cds, obj);
> }
> }
> // END FIX
> else if (col != null)
> {
> // MBAIRD
> // if the collection is a
>collectionproxy and it's not already loaded
> // no need to store it.
> if (col instanceof CollectionProxy &&
>!((CollectionProxy) col).isLoaded())
> {
> continue;
> }
> if (cds.isMtoNRelation())
> {
> currentMtoNKeys =
>getMtoNImplementor(cds, obj);
> // delete unused m:n implementors
> deleteMtoNImplementor(cds, obj,
>(Collection)col, currentMtoNKeys);
> }
> Iterator colIterator;
> if (col instanceof
>ManageableCollection)
> {
> colIterator =
>((ManageableCollection) col).ojbIterator();
> }
> else if (col instanceof Collection)
> {
> colIterator = ((Collection)
>col).iterator();
> }
> else if (col.getClass().isArray())
> {
> colIterator = new
>ArrayIterator(col);
> }
> else
> {
> throw new OJBRuntimeException(
> col.getClass()
> + " can not be managed by
>OJB, use Array, Collection or ManageableCollection
>instead !");
> }
> while (colIterator.hasNext())
> {
> Object otherObj =
>colIterator.next();
> // for m:n mapped collections
>store association implementing entries
> if (cds.isMtoNRelation())
> {
> // 1. Store depended upon
>object first to avoid FK violation
> storeCollectionObject(cds,
>otherObj);
> // 2. Store indirection record
> storeMtoNImplementor(cds, obj,
>otherObj, currentMtoNKeys);
> }
> // for 1:n mapped collection
>assert proper fk assignment
> else
> {
> assertFkAssignment(otherObj,
>obj, cds);
> storeCollectionObject(cds,
>otherObj);
> }
> }
> // invoke callback on collection
> if (col instanceof
>ManageableCollection)
> {
> ((ManageableCollection)
>col).afterStore(this);
> }
> }
> }
> }
>
>
>
>__________________________________________________
>Do you Yahoo!?
>The New Yahoo! Search - Faster. Easier. Bingo
>http://search.yahoo.com
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
>For additional commands, e-mail: ojb-user-help@db.apache.org
>
>
>
>