Auto Number field in Entity

Is it possible to select auto-number field in an Entity while designing it in the CUBA-Studio? If not, any plan to add this feature in future? What could be the suggested solution to get this?

1 Like

You can use the UniqieNumbers mechanism to generate number sequences: https://doc.cuba-platform.com/manual-6.0/uniqueNumbers.html

Thanks. Can you please suggest how can I include this sequence while persisting he Entity as i am using default saving option by the screen generated by CUBA?

The simplest option is to initialize the field in the initNewItem() method of your editor screen controller, see https://doc.cuba-platform.com/manual-6.0/init_values_in_initNewItem.html.
Something like this:

@Inject
private UniqueNumbersService unService;
@Override
protected void initNewItem(MyEntity item) {
    item.setNumber(unService.getNextNumber("MyEntity_numbers"));
}

Hi
Thanks. I tried it but I am getting message for @Override as follows:
'Method does not override method from it’s superclass ’

When I remove it. the program runs but no auto number generated.
I have attached the project file, thanks for your help.

demo-mortoza-1.7z (164.0K)

initNewItem() is a method of AbstractEditor, so it can be used only in editor screens.

Excellent. It worked

Hi
This is working perfectly but noticed one thing. If i do not save the record, the sequence number is lost. I want to keep the continuity of the sequence in the database as reference. Therefore, instead of creating the sequence at init time, can we do it just before the record is saved in database?

If I want to have diffrent types of uniqueNumbers.
Ex:
Seq 1 = starts at 1000 shall increment with one or as set by user
Seq 2 = starts at 5000 shall increment with one or as set by user
Seq 3 = starts at 10000 shall increment with one or as set by user
Seq 4 = starts at 1 shall increment with one or as set by user and has a formatting as N001

Each of the sequence numbers above may be created by user an presentet in a table.
Different entites uses each of the sequence numbers as report number or document Id.

How can we solve this?

Then use the UniqueNumbersAPI interface in a BeforeInsertEntityListener / BeforeUpdateEntityListener: https://doc.cuba-platform.com/manual-6.9/entity_listeners.html
But keep in mind that even when you use the sequence generation in an entity listener, there is still a small chance that the generated number will be lost: if the transaction is rolled back, the sequence is not.

I am trying to use onBeforeInsert, but this didn’t create number.



    @Inject
    private UniqueNumbersService unService;

    @Override
    @Transactional
    public void onBeforeInsert(SalesOrder entity) {
    
        entity.setSoNumber(unService.getNextNumber("SalesOrder_numbers"));
    }

  1. Remove @Transactional annotation - all Insert/Update/Delete entity listeners are executed in the context of the transaction which saves data.
  2. Set a breakpoint to the listener and watch what is happening at run time.

Hi
getting message as below!


Frame is not available

Mortiza,

Where do you get it? When? How can we reproduce this?

Regards.

It worked

I have another question about unique number.

How can I generate a sub-sequence of numbers?

For example, I have orderNumber sequence generated which is unique in my application. Now I want to generate orderLines which will be unique within each order and orderLines will always start from 1 in every order. How can I do this?

I would just iterate over the collection of orderLines, find the max value and increment it for a new line.