[OJB-developers] removing associations
Brought to you by:
thma
From: Matthew B. <ma...@so...> - 2002-06-10 04:58:59
|
I changed assertFkAssignment to properly null out the foreign key attributes when an object reference is "detached". As I mentioned in my comment, I can't believe this wasn't tested: didn't anyone run into this problem when using OJB? To be clear, once a relationship was established in the DB, it could never be removed. See BidirectionalAssociationTest for broker: BidirectionalAssociationObjectA a = temp.getRelatedA(); if (a != null) { a.setRelatedB(null); // calling store here would not update a's foreign key to b. // the object would no longer refer to b, but in the db, the // foreign key would still exist. broker.store(a); } broker.delete(temp); private void assertFkAssignment(Object obj, Object ref, ObjectReferenceDescriptor rds) { /** * MBAIRD * we have 'disassociated' this object from the referenced object, the object representing the ord is now null, * so set the fk to null. * * Note: I can't believe this wasn't tested (as of June9/2002), attaching and removing objects seems to be a * pretty important piece of functionality. * */ if (ref == null) { ClassDescriptor cld = descriptorRepository.getDescriptorFor(obj.getClass()); Vector v = rds.getForeignKeyFields(); FieldDescriptor f[] = rds.getForeignKeyFieldDescriptors(cld); if (f != null) { for (int i = 0; i < f.length; i++) { f[i].getPersistentField().set(obj, null); } } } if ((!(obj instanceof VirtualProxy)) && (!(obj instanceof Proxy)) && (ref != null) && (!(ref instanceof VirtualProxy)) && (!(ref instanceof Proxy))) { ClassDescriptor refCld = descriptorRepository.getDescriptorFor(ref.getClass()); Object[] refPkValues = refCld.getKeyValues(ref); ClassDescriptor objCld = descriptorRepository.getDescriptorFor(obj.getClass()); FieldDescriptor[] objFkFields = rds.getForeignKeyFieldDescriptors(objCld); if (objFkFields != null) { FieldDescriptor fld = null; for (int i = 0; i < objFkFields.length; i++) { fld = objFkFields[i]; fld.getPersistentField().set(obj, refPkValues[i]); } } } } |