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.
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
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
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();
}
}