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.

    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(" = :project and e.projectSchemeCode = :sCode")
                    .parameter("sCode", event.getValue().getProjectSchemeCode())
                    .parameter("project", prjidLookUp.getValue().getId().intValue())


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.

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.