child entity is not getting saved in DB using Audit trail using DecriptorEventAdapter eclipse link

StackOverflow https://stackoverflow.com/questions/20851924

Question

When we save the parent Entity Object which contains child entities as a one to many Mapping only Parent is getting inserted in DB.

Below is the postInsert(DescriptorEvent event)

public void postInsert(DescriptorEvent event) {

              AbstractEntity ae = (AbstractEntity) event.getObject();

              // inserting all the fields as seperate entry to audit table
              InsertObjectQuery query = (InsertObjectQuery) event.getQuery();
              AuditSession as = new AuditSession();

              as.setOperationType("I");
              as.setReason("test reason");


              long count=1;
              for (int i = 0; i < query.getModifyRow().getFields().size(); i++)
        {     
                     AuditTrail at = new AuditTrail();


                     DatabaseField dbField =(DatabaseField)query.getModifyRow().getFields().elementAt(i);
            if (dbField == null)
                continue;
            String fieldName = dbField.getName();

              if(query.getModifyRow().getValues(fieldName) != null){
                      at.setNewValue(query.getModifyRow().getValues(fieldName));
                }else{
                     at.setNewValue(null);
                }

            at.setFieldId(fieldName);
            at.setOldValue(null);

            if (ae.getClass().getAnnotation(Table.class) != null) {
                     at.setTableId(ae.getClass().getAnnotation(Table.class)
                                  .name());
              } else {
                     at.setTableId(ae.getClass().getSimpleName());
              }

            at.setOperationType("I");
            at.setSeqNo(count);
            at.setAppRecordId("1");
            as.addAuditTrail(at);
            count++;
         } 


         event.getSession().insertObject(as);

       }

In the above method AuditSession is the parent entity which has OneToMany mapping to AuditTrail entity

Était-ce utile?

La solution

All insert, update and delete queries default to cascade private parts, so unless your 1:M mapping is privately owned, the referenced child objects will not be inserted. You can change this on the query using the cascadeAllParts() method as needed: http://www.eclipse.org/eclipselink/api/2.0/org/eclipse/persistence/queries/DatabaseQuery.html#cascadeAllParts()

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top