cayenne-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Andrew Willerding <awillerd...@itsurcom.com>
Subject Question on how to properly create parent-child nested dependent objects?
Date Mon, 18 Sep 2017 15:30:46 GMT
Hi,

I am trying to create a DB record where a dependency record may not yet 
exist and I'm getting a "No rows for ..." error.  I don't understand how 
I should create the child record that will be used for the parent object 
so it can be referenced by the parent object without getting an error.  
The idea is to dynamically create a child record as needed.

Thanks,

Andrew

Example code:

ObjectContext oc = ClientBaseAdmin.getObjectContext();
CommunicationLog cl = oc.newObject(CommunicationLog.class);
cl.setCommunicationDT(LocalDateTime.now());
cl.setCommunicationType(CommunicationType.find("Email"));
oc.commitChanges();

and in CommunicationType I have

public static CommunicationType find(String value) {
         CommunicationType result = null;
         result = ObjectSelect.query(CommunicationType.class)
                 .where(CommunicationType.DESCRIPTION.eq(value))
                 .selectFirst(ClientBaseAdmin.getObjectContext());

         if (result == null) {
             CommunicationType ct = 
ClientBaseAdmin.getObjectContext().newObject(CommunicationType.class);
             ct.setDescription(value);
             ClientBaseAdmin.getObjectContext().commitChanges();
             result = ct;
         }
         return result;
}


Mime
View raw message