Correction to the code below: if (child instanceof Collection) { Collection chunks = (Collection) child; Collection chunksWithIds = new ArrayList(chunks.size()); for (Iterator itr = chunks.iterator(); itr.hasNext(); ) { DocumentTextChunk chunk = (DocumentTextChunk) itr.next(); chunk.setVersionId(_documentVersion.getVersionId()); chunksWithIds.add(chunk); } if (! chunksWithIds.isEmpty()) { _documentVersion.setTextChunks(chunksWithIds); } } else { _documentVersion.setTextChunks(null); }
jeff >>> [EMAIL PROTECTED] 04/29/04 05:02PM >>> Figured out a work-around on my own. Its a hack, but it works. Basically, you have to have a mechanism whereby (1) the dependent collection of objects is removed from the master object just before a database session begins, (2) the master object is saved (w/o the dependent collection), (3) the master object's id is then copied into the temporarily cached collection of dependents, and (4) the dependents are set back into the master while still within the transaction so that Castor automatically updates. Something like this: Object dependent = record.getChild(); record.setChild(null); Database db = null; PersistenceService service = PersistenceServiceFactory.getPersistenceService(AUDIT_SERVICE_PROVIDER); db = service.getDatabase(); db.begin(); db.insert(record); record.setChild(dependent); db.commit(); then in setChild(Object child): if (child instanceof Collection) { Collection chunks = (Collection) child; for (Iterator itr = chunks.iterator(); itr.hasNext(); ) { DocumentTextChunk chunk = (DocumentTextChunk) itr.next(); chunk.setVersionId(_documentVersion.getVersionId()); } if (! chunks.isEmpty()) { _documentVersion.setTextChunks(chunks); } } else { _documentVersion.setTextChunks(null); } This works pretty well. jeff >>> [EMAIL PROTECTED] 04/28/04 05:59PM >>> I have an issue I am desperately trying to solve with Castor JDO involving a master object that provides its PK to its collection of dependent objects as part of their PK. First some details on the environment: Castor version: 0.9.4.3 (no, cannot upgrade to 0.9.5+ at this time - has to be solved with the given version) IBM JDK 1.3.1 Win2k Database: Oracle 9i, classes12.zip, and I have little control of the schema I have the following situation: class AccessAuditRecord... private int auditId; ... private DocumentVersionRecord documentVersion;... class DocumentVersionRecord ... private int versionId; ... private Collection textChunks; //elements: DocumentTextChunk ... class DocumentTextChunk ... private int versionId; private int sequenceNumber; ... [with all the appropriate getter/setters/etc.] Where an AccessAuditRecord may or may not have a dependent DocumentVerionReport, which will have zero or more DocumentTextChunk instances. DocumentTextChunk cannot exist w/o a DocumentVerionReport, which cannot exist w/o an AccessAuditRecord. I am furthermore constrained to only issue a single database.create() with the AccessAuditRecord instance and want the inserts to cascade to the dependent objects. The mapping looks like this: <class name="edu.umich.med.careweb.audit.AccessAuditRecord" access="shared" identity="auditId" key-generator="auditseq"> <cache-type type="none"/> <map-to table="ACCESS_AUDIT_LOG" /> <field name="auditId" type="integer" > <sql name="ACCESS_AUD_ID" type="numeric" dirty="ignore" /> </field> ... <field name="documentVersion" type="edu.umich.med.careweb.audit.DocumentReportVersion" required="false"> <sql many-key="CWEB_ACCESS_AUD_ID"/> </field> </class> <class name="edu.umich.med.careweb.audit.DocumentReportVersion" access="shared" identity="versionId" key-generator="versionseq" depends="edu.umich.med.careweb.audit.AccessAuditRecord"> <cache-type type="none"/> <map-to table="document_report_version" /> <field name="versionId" type="integer" > <sql name="DOCUMENT_VERSION_ID" type="numeric" dirty="ignore" /> </field> ... <field name="textChunks" type="edu.umich.med.careweb.audit.DocumentTextChunk" required="false" lazy="true" collection="collection"> <sql many-key="DOCUMENT_VERSION_ID"/> </field> </class> <class name="edu.umich.med.careweb.audit.DocumentTextChunk" access="shared" identity="versionId sequenceNumber" depends="edu.umich.med.careweb.audit.DocumentReportVersion"> <cache-type type="none"/> <map-to table="document_text_version" /> <field name="versionId" type="integer"> <sql name="DOCUMENT_VERSION_ID" type="numeric" dirty="ignore" /> </field> <field name="sequenceNumber" type="integer"> <sql name="SEQ_NBR" type="numeric" dirty="ignore" /> </field> ... Everything works beautifully up to the inserting of DocumentTextChunk. The auditrecord and versionreport are inserted ok; However, I cannot get the DocumentTextChunk instances to save. I get the following errors: (1) if I treat versionId on DocumentTextChunk as its own field, I get FK constraint violation: java.sql.SQLException: ORA-02291: integrity constraint (CWEBSRC.DOCUM_TEXT_VERSIONID_FKC) violated - parent key not found basically because the field is not getting set after the DocumentVersionReport is inserted. (2) if I treate the getVersionId() method on DocumentTextChunk as a call-through to the same method on its parent DocumentVersionReport instance, I get the following error: org.exolab.castor.jdo.PersistenceException: Nested error: org.exolab.castor.jdo.DataObjectAccessException: Field access error: FieldMolder of edu.umich.med.careweb.audit.DocumentTextChunk.setversionId(integer versionId) access resulted in exception: java.lang.reflect.InvocationTargetException at org.exolab.castor.persist.TransactionContext.markCreate(TransactionContext.java:817) at org.exolab.castor.persist.ClassMolder.markCreate(ClassMolder.java:986) at org.exolab.castor.persist.LockEngine.markCreate(LockEngine.java:400) at org.exolab.castor.persist.TransactionContext.markCreate(TransactionContext.java:806) at org.exolab.castor.persist.TransactionContext.create(TransactionContext.java:854) at org.exolab.castor.jdo.engine.DatabaseImpl.create(DatabaseImpl.java:366) probably because I am trying to play God while castor is using reflection and somehow has control of the parent versionId?? (3) if I add fields in the mapping from children back to parents (and employ the Bug 925 hack of creating a view that duplicates the DOCUMENT_VERSION_ID column on the DOCUMENT_TEXT_VERSION table) I get the following: java.sql.SQLException: ORA-01400: cannot insert NULL into () I have tried lots of other permutations and hacks to try to work around this, but cannot get it to work. Help! Anyone have suggestions or have dealt with a similar situation and got it to work? jeff Jeffrey Bonevich Applications Programmer MCIT/Technical Application Support University of Michigan Health Systems Phone: 734.615.8296och.med.careweb.audit.DocumentReportVer Mobile: 734.678.7242 Email: [EMAIL PROTECTED]