Rows added to a composition do not maintain the insertion order

Hello,
I have a Microsoft SQL DB.
In this database there are two tables with a classic master-details example, a Orders table and a Rows table.
When I create a new Order and I add the Order Rows (always in the Order Edit Screen), I realized that the sequence in which the detail rows are saved is not retained.
The IDs assigned to each record are not in sequence.

img1

Test01.zip (78.7 KB)

Please help me
Thanks to everyone

Hello Michele,

Unfortunately you cannot sort the rows by BaseIntIdentityIdEntity.id, as the detail rows are commited at once with the master entity, and the sequence will not be maintained.
As a workaround, you can implement some other sorting logic: for example, add a generated column to the rows table and display the incrementing values in it, or even add a new attribute to the TabRows entity.

Thanks Olga, I figured the problem was that.
I can’t change the table structure, so I prefer to add a generated column.
Do you have an example to suggest?
Thank you

You can try the example from docs with Table.PlainTextCell as the cell component.

Hi Olga, I tried to add a generated column, but I need more help.
How can I set the sort order on this column and make sure that the order in which the rows were inserted is retained?
I read the documentation and searched in the forum, but I did not find anything that could help me.
Thank you very much

Hi Michele,

I experimented a little and decided that the best solution would be to create a persistent field for TabRow entity and set its unique value in the TabRow editor, for example:

@Column(name = "INSERT_NUM", unique = true)
protected Long insertNum;

Here I used the sequence generation API, it provides continuous numbering through the whole app:

public class TabRowEdit extends AbstractEditor<TabRow> {

    @Inject
    private UniqueNumbersService uniqueNumbersService;

    @Override
    protected boolean preCommit() {
        getItem().setInsertNum(uniqueNumbersService.getNextNumber("tabRow"));
        return super.preCommit();
    }
}

demo.zip (89.2 KB)