ClassCast exception with datasources

I think I’m pretty close…see below:

public class InvoicesEdit extends AbstractEditor<Invoices> {

    @Inject
    private UniqueNumbersService uniqueNumbersService;

    @java.lang.Override
    protected void initNewItem(Invoices item) {
        super.initNewItem(item);
        item.setRegistrationNo(uniqueNumbersService.getNextNumber("Domain1"));
    }
}

I created a new datasource - registrationNoDs but I’m not sure if I need to create a new view too.
However I have three datasources: datasource:invoicesDs, collectionDatasource:ordersDs and datasource:registrationNoDs.

Unfortunately when I press button Edit I have the following error message:

java.lang.ClassCastException: com.haulmont.cuba.gui.data.impl.DatasourceImpl cannot be cast to com.haulmont.cuba.gui.data.CollectionDatasource

The problem is not related to the code you attached. Please check your datasource injection points, there is an incorrect type of datasource in one of them.

Please check your Java code, fields with @inject are not correct.

Unfortunately I have compilation (2)errors: 

cannot find symbol import com.haulmont.cuba.core.app.UniqueNumbersAPI;
cannot find symbol private UniqueNumbersAPI uniqueNumbers;

import com.haulmont.cuba.core.app.UniqueNumbersAPI;
import javax.inject.Inject;

public class InvoicesEdit extends AbstractEditor<Invoices> {

  @Inject
  private UniqueNumbersAPI uniqueNumbers;

    @java.lang.Override
    protected void initNewItem(Invoices item) {
        super.initNewItem(item);
        item.setRegistrationNo(uniqueNumbers.getNextNumber("Domain1"));
    }
}

It is available only on middleware, use UniqueNumbersService in UI controllers.

It works. The RegistrationNo mandatory option should be unchecked.
The text field RegistrationNo is completed automatically after Save.
However there is a way to see the generated number before Save?