Cuba creating record with wrong FOREIGN KEY due to SQL server DB insert trigger

When creating a new record which contain a Composition record, a WRONG FOREIGN KEY was used.
The Foreign Key used to insert is another status history table one, which is created by the SQL server database insert trigger fire by the master table.
If I disable the insert trigger, the cuba application become resume normal.

The Composition record was added by the following code.

    @Subscribe("dummyList")
    public void onDummyListValueChange(HasValue.ValueChangeEvent<B011ProjectScheme> event) {
        if (event.isUserOriginated()) {
            D011IssueScheme aIssueScheme = metadata.create(D011IssueScheme.class);
            D011IssueScheme newIssueScheme = dataContext.merge(aIssueScheme);

            B011ProjectScheme oneProjectScheme =  dataManager.load(B011ProjectScheme.class)
                    .query("e.project.id = :project and e.projectSchemeCode = :sCode")
                    .parameter("sCode", event.getValue().getProjectSchemeCode())
                    .parameter("project", prjidLookUp.getValue().getId().intValue())
                    .one();

            newIssueScheme.setIssue(getEditedEntity());
            newIssueScheme.setProjectSchemeId(oneProjectScheme);
            newIssueScheme.setProjectSchemeCode(oneProjectScheme.getProjectSchemeCode());
            issueSchemeDc.getMutableItems().add(newIssueScheme);
            dummyList.setValue(null);
        }
    }

It seem to be a Cuba application bug. As our database trigger perform many checking to insert status history, we cannot disable it.

Any suggestion to prevent this problem, thanks.

Hi,
Do your entities have an Identity primary key?

There is a known problem in EclipseLink with Identity primary keys and on-insert triggers that insert another entity: Eclipse Community Forums: EclipseLink » Insert using identity column and trigger returns wrong primary key

There is no easy solution for it, unfortunately.

Yes, we use Identity primary key in our tables.
Thanks for your reply, luckily this insert trigger able to replace by after commit handler.