Adding Related Entites To A New Entity

Jmix 2.3.1 - Gradle - Postgresql

Pretty much every piece of data in my system has multiple related tables, things like clients have 5 - 10 related entities, reports have up to 2 dozed related entities, etc. I’m having the issue that adding these to an entitiy that hasn’t been saved and then reopened created key constraint violations because the related entity adds the primary entity when it saves the first time.

An example of this is:

  1. Create New Client and input their primary data, Name, DOB, Region
  2. Create a Phone Number for the client and on the related phone number table using a “list_create” action in a “DIALOG” and hit save
  3. New Client’s page can now not be saved because the Phone Number Dialog created the entity when save was selected.

I think I’m doing something wrong with how I’m managing this, as I would like to have none of these items commit to the DB until the master record is saved (in this example the client). I’m including some code example below of a simple report I do daily that only has two relationships matched. I’d appreciate any help on what I’m doing wrong.

Thank you,
Oran

CODE EXAMPLE:
Create a report for Service Agreements, there are three data tables, table parameters are:

CREATE TABLE public.record_daily_sal (
	"uuid" uuid DEFAULT gen_random_uuid() NOT NULL,
	date_received date NOT NULL,
	prepared_by varchar(100) NOT NULL,
	"type" public.records_daily_sal_type NOT NULL,
	name_first varchar(50) NOT NULL,
	name_last varchar(50) NOT NULL,
	pmi char(8) NULL,
	umpi varchar(20) NULL,
	sal_number varchar(20) NULL,
	CONSTRAINT record_daily_sal_pk PRIMARY KEY ("uuid")
);

CREATE TABLE public.record_daily_sal_lines (
	"uuid" uuid DEFAULT gen_random_uuid() NOT NULL,
	status public."status_daily_sal_lines_status" NOT NULL,
	line int NOT NULL,
	service varchar(8) NOT NULL,
	units int NULL,
	rate numeric NULL,
	start_date date NULL,
	end_date date NULL,
	ref__record_daily_sal uuid NOT NULL,
	CONSTRAINT record_daily_sal_lines_pk PRIMARY KEY ("uuid"),
        CONSTRAINT record_daily_sal_lines_record_daily_sal_fk FOREIGN KEY (ref__record_daily_sal) REFERENCES public.record_daily_sal("uuid") ON DELETE CASCADE;

);

CREATE TABLE public.record_daily_sal_tasks (
	"uuid" uuid DEFAULT gen_random_uuid() NOT NULL,
	ref__record_daily_sal uuid NOT NULL,
	task varchar(512) NOT NULL,
	CONSTRAINT record_daily_sal_tasks_pk PRIMARY KEY ("uuid"),
	CONSTRAINT record_daily_sal_tasks_record_daily_sal_fk FOREIGN KEY (ref__record_daily_sal) REFERENCES public.record_daily_sal("uuid") ON DELETE CASCADE;
);

Then I make a view for record-daily-sal-detail-view to create a Daily Service Agreement entry with the nested tasks and lines for the agreement. This view’s XML works correctly visually, but has the issues described above.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<view xmlns="http://jmix.io/schema/flowui/view"
      title="msg://recordDailySalDetailView.title"
      focusComponent="form">
    <data>
        <instance id="recordDailySalDc"
                  class="com.company.alirem.entity.RecordDailySal">
            <fetchPlan extends="_base">
                <property name="tasks" fetchPlan="_base" fetch="AUTO"/>
                <property name="lines" fetchPlan="_base" fetch="AUTO"/>
            </fetchPlan>
            <loader id="recordDailySalDl"/>
            <collection id="tasks" property="tasks"/>
            <collection id="lines" property="lines"/>
        </instance>
    </data>
    <facets>
        <dataLoadCoordinator auto="true"/>
    </facets>
    <actions>
        <action id="saveAction" type="detail_saveClose"/>
        <action id="closeAction" type="detail_close"/>
    </actions>
    <layout>
        <formLayout id="form" dataContainer="recordDailySalDc">
            <responsiveSteps>
                <responsiveStep minWidth="0" columns="1"/>
                <responsiveStep minWidth="30em" columns="2"/>
                <responsiveStep minWidth="40em" columns="3"/>
                <responsiveStep minWidth="50em" columns="4"/>
            </responsiveSteps>
            <select id="typeField" property="type" label="Type"
                    itemsEnum="com.company.alirem.entity.RecordsDailySalType" colspan="1"/>
            <datePicker id="dateReceivedField" property="dateReceived" label="Date Received" colspan="1"/>
            <textField id="preparedByField" property="preparedBy" label="Entry Prepared By" colspan="1"/>
        </formLayout>

        <formLayout id="form2" dataContainer="recordDailySalDc">
            <responsiveSteps>
                <responsiveStep minWidth="0" columns="1"/>
                <responsiveStep minWidth="30em" columns="2"/>
                <responsiveStep minWidth="40em" columns="3"/>
            </responsiveSteps>
            <textField id="nameFirstField" property="nameFirst" label="First Name"/>
            <textField id="nameLastField" property="nameLast" label="Last Name"/>
            <textField id="pmiField" property="pmi" label="PMI"/>
            <textField id="salNumberField" property="salNumber" label="SAL Number" visible="false"/>
            <textField id="umpiField" property="umpi" label="UMPI" visible="false"/>
            <dataGrid id="recordDailySalTasksDataGrid" dataContainer="tasks" width="100%" colspan="2">
                <actions>
                    <action id="create" type="list_create">
                        <properties>
                            <property name="openMode" value="DIALOG"/>
                        </properties>
                    </action>
                    <action id="edit" type="list_edit">
                        <properties>
                            <property name="openMode" value="DIALOG"/>
                        </properties>
                    </action>
                    <action id="remove" type="list_remove"/>
                </actions>
                <columns>
                    <column property="task"/>
                </columns>
            </dataGrid>
            <dataGrid id="recordDailySalLinesDataGrid" dataContainer="lines" width="100%" colspan="2">
                <actions>
                    <action id="create" type="list_create">
                        <properties>
                            <property name="openMode" value="DIALOG"/>
                        </properties>
                    </action>
                    <action id="edit" type="list_edit">
                        <properties>
                            <property name="openMode" value="DIALOG"/>
                        </properties>
                    </action>
                    <action id="remove" type="list_remove"/>
                </actions>
                <columns>
                    <column property="lineStatus" header="Status"/>
                    <column property="line" header="Line"/>
                    <column property="service" header="Service"/>
                    <column property="units" header="Units"/>
                    <column property="rate" header="Unit Rate"/>
                    <column property="startDate" header="Start Date"/>
                    <column property="endDate" header="End Date"/>
                </columns>
            </dataGrid>

        </formLayout>
        <hbox id="detailActions">
            <button id="saveAndCloseBtn" action="saveAction"/>
            <button id="closeBtn" action="closeAction"/>
        </hbox>
    </layout>
</view>

Hi, Oran

try to use @Composition relation type instead of Association that is by default Entities :: Jmix Documentation

wbr, Aleksey

1 Like