Numbering for Composition Items (such as OrderLines in Order)

Taking sample-sales as starting point.

I would like to add a column order_line_nr (persistent) to OrderLine that automatically increments for the Order when creating a new line.

Would be great if it could also handle special cases like re-numbering when a row was deleted.

Is there any suitable Service for this?

It may also be a non-persisent value. Important for me would be to have it somehow generated for the report (without Jasper). Something like “select row_number()…” would be enough, if it’s easier to implement. Persistent would be better though as it would keep the ordering.

I checked these, but didnt find the soluition, it might be there but didn’t get it :man_facepalming::



Thanks Team!

Hi Alex,

I agree that the number attribute should better be persistent.

Add the attribute to OrderLine entity:

@Entity(name = "sales_OrderLine")
public class OrderLine extends StandardEntity {

    @NotNull
    @Column(name = "NUM", nullable = false)
    protected Integer num;

Add ordering by this attribute to the collection in Order:

@Entity(name = "sales_Order")
public class Order extends StandardEntity {

    @OrderBy("num")
    @Composition
    @OnDelete(DeletePolicy.CASCADE)
    @OneToMany(mappedBy = "order")
    protected List<OrderLine> lines;

In OrderEdit screen, subscribe to CollectionChangeEvent of the data container and recalculate numbers for all items:

@UiController("sales_Order.edit")
@UiDescriptor("order-edit.xml")
@EditedEntityContainer("orderDc")
@LoadDataBeforeShow
public class OrderEdit extends StandardEditor<Order> {

    @Inject
    private CollectionContainer<OrderLine> linesDc;

    @Subscribe(id = "linesDc", target = Target.DATA_CONTAINER)
    protected void onOrderLinesDcCollectionChange(CollectionContainer.CollectionChangeEvent<OrderLine> event) {
        if (event.getChangeType() != CollectionChangeType.REFRESH) {
            calculateAmount();
            recalculateLineNumbers();
        }
    }

    private void recalculateLineNumbers() {
        int num = 1;
        for (OrderLine orderLine : linesDc.getItems()) {
            orderLine.setNum(num++);
        }
    }

That’s all. On each adding/removing of an order line, all lines will get correct numbers.

2 Likes