cayenne-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Nikita Timofeev <ntimof...@objectstyle.com>
Subject Re: Question on how to properly create parent-child nested dependent objects?
Date Tue, 19 Sep 2017 07:21:46 GMT
Ok, looked closer at your code, commitChanges() call inside find()
method doing not what you expect, it flushes ALL changes in a given
context to DB, and that includes partially created CommunicationLog
object.
Generally there is no need to commit every object, you can create and
link as many objects as you need, so you can just skip commitChanges()
call inside find() method or you should find CommunicationType first
and then create CommunicationLog.


On Mon, Sep 18, 2017 at 7:08 PM, Andrew Willerding
<awillerding@itsurcom.com> wrote:
> Hi Nikita,
>
> Based on what you said below, I rebuilt my Database Schema from scratch but
> now I'm getting the error below.  The validation is failing the line 34
>
>  ClientBaseAdmin.getObjectContext().commitChanges();
>
> because of the line 418
>
> cl.setCommunicationType(CommunicationType.find("Email"));
>
> To me it looks like the commit can't happen in the child record because the
> commit from the parent record is incomplete and no record is created at all.
>
> Andrew
>
>
> Caused by: org.apache.cayenne.validation.ValidationException: [v.4.0.B1 Jun
> 02 2017 15:11:18] Validation failures: Validation failure for
> com.callistacti.clientbase.Database.CommunicationLog.communicationType:
> "communicationType"  is required.
> Validation failure for
> com.callistacti.clientbase.Database.CommunicationLog.communicationType:
> "communicationType"  is required.
>     at
> org.apache.cayenne.access.ObjectStoreGraphDiff.validateAndCheckNoop(ObjectStoreGraphDiff.java:113)
>     at
> org.apache.cayenne.access.DataContext.flushToParent(DataContext.java:734)
>     at
> org.apache.cayenne.access.DataContext.commitChanges(DataContext.java:691)
>     at
> com.callistacti.clientbase.Database.CommunicationType.find(CommunicationType.java:34)
>     at
> com.callistacti.clientbase.Panel.PanelGroups.windowClose(PanelGroups.java:418)
>     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>     at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
>     at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
>     at java.lang.reflect.Method.invoke(Method.java:498)
>     at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:510)
>
>
>
> On 18/09/17 11:40 AM, Nikita Timofeev wrote:
>>
>> Hi Andrew,
>>
>> Can you provide stack trace for the exception you got (Cayenne related
>> part of it)?
>> It seems to me that you have problem with PK generation, not with your
>> data objects.
>> Your code looks fine to me and should work.
>>
>> On Mon, Sep 18, 2017 at 6:30 PM, Andrew Willerding
>> <awillerding@itsurcom.com> wrote:
>>>
>>> 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;
>>> }
>>>
>>
>>
>



-- 
Best regards,
Nikita Timofeev

Mime
View raw message