Generate unique number

Hi Konstantin,

I need to insert an unique number (let say ABC01, ABC02 etc - incremented by one) into my Invoice number attribute (invoiceNO).
For this a need a suggestion in: edit mode - (invoices-edit.xml) .
Do you have an example?



You can use UniqueNumbersAPI:

private UniqueNumbersAPI uniqueNumbers;

private long getNextValue() {
    return uniqueNumbers.getNextNumber("mySequence");

Take a look at Sequence Generation in the manual.

Hi Yuriy,

Thank you. However I’ve already seen this answer it in topics.
Can you give me more details, there is an example with such kind of implementation?
Basically I have to create a Service or (Entity listener) in Middleware block?
How can I link this (seq gen) code to <field property = “registartionNo”?>

In the simplest case you just set your unique number in entity editor right on new item initialization:

public class TicketEdit extends AbstractEditor<Ticket> {
    protected UniqueNumbersService uniqueNumbersService;

    protected void initNewItem(Ticket item) {


If you need to set number right before persisting an entity, you can use UniqueNumbersAPI from Entity listener.

3 posts were split to a new topic: ClassCast exception with datasources

This code is working fine for generating auto increase “RegistrationNo” field.
Now the question is how can I don’t make it increase the number next time, in case user don’t save the record.

I created an InvoiceEntityListener in Middleware but doesn’t work, the RegistrationNo field is empty (see below).
Do you have an example in order to set a specific start value (i.e. int i = 22).

public class InvoiceEntityListener implements BeforeInsertEntityListener<Invoices> {

    private UniqueNumbersAPI uniqueNumbers;

    public void onBeforeInsert(Invoices entity, EntityManager entityManager) {
       if (entity.getRegistrationNo()==null) {
           entity.setRegistrationNo((long)uniqueNumbers.getNextNumber( Integer.toString(22)));

Probably, you will find answers in the following topics:

More or less. It’s mandatory to create a new Service or a Listener is enough?

Entity listener should be enough. Please note, if your transaction is rolled back, number will not be returned into a sequence.

It.s not the case. Basically my question is simple:
When I use UniqueNumberService from client edit controller, RegistrationNo text field is completed from 1 to n.
The problem is that even I cancel the record the transaction sequence still increments.

I understand that a solution is to use an onBeforeInsert Entity Listener UniqueNumberAPI from Middleware.
The problem is that I cannot compile (debug) from IDEA (unable debugger port localhost:8787) and I need your help (maybe with a specific example).

Could you create a topic and describe your problem in details?

It is not that hard to debug a CUBA application, check our YouTube webinar:

    private UniqueNumbersAPI uniqueNumbersAPI;

    public void onBeforeInsert(Invoice entity, EntityManager entityManager) {
        entity.setRegistrationNumber("" + uniqueNumbersAPI.getNextNumber("invoiceRegNo"));