After data migration, how to update the sequence generation?

Hi, forum followers.
In my Cuba project there’s an entity extending BaseLang IdEntity and I created a SQL procedure to perform the data migration, inserting records manually in the corresponding table and assigning the id value from a incremental numeric variable.

Here you are the entity class declaration:

@NamePattern("%s %s %s|tipMoviment,dtMoviment,partida")
@Table(name = "MAGATZEM_MOVIMENT")
@Entity(name = "magatzem_Moviment")
public class Moviment extends BaseLongIdEntity implements Creatable, Updatable, SoftDelete {
    private static final long serialVersionUID = 8073284349450287765L;

    @Column(name = "CREATE_TS")
    protected Date createTs;

    @Column(name = "CREATED_BY", length = 50)
    protected String createdBy;

And the portion of SQL procedure code for inserting the values:

DECLARE @newrefmag varchar(20),
	@idmovim bigint = 0,

				--Crear el registre a la taula MAGATZEM_MOVIMENT
	  			--********* TAULA MAGATZEM_MOVIMENT **********
	  			--Incrementar id moviment
	  			SET @idmovim = @idmovim + 1
	  			--Crear nou registre:
	  			VALUES (@idmovim, SYSDATETIME(), 'admin', null, null, null, null, 'E', CASE WHEN @dtentrada IS NULL THEN SYSDATETIME() ELSE @dtentrada END, @uuidpartida, @totunit, ('Migració dades ' + @swrepas), null)

When finishing the data migration and trying to insert a new record from the application next error appears:

Platform details:
CUBA Platform version: 7.2.11
CUBA Studio plugin version: 15.3-202
IntelliJ version: CUBA Studio 2020.2
Database: MS SQL-SERVER 2008 R2

To solve the problem, I was thinking in alter manually the value of the corresponding sequence on database but I didn’t find the SEC_UN_{domain} table as framework documentation indicates for Ms Sql Server before 2012:

The implementation of the sequence generation mechanism depends on the DBMS type. Sequence parameters can also be managed directly in the database, but in different ways.

    For HSQL, Microsoft SQL Server 2012+, PostgreSQL and Oracle each UniqueNumbersAPI sequence corresponds to a SEC_UN_{domain} sequence in the database.

    For Microsoft SQL Server before 2012 each sequence corresponds to a SEC_UN_{domain} table with a primary key of IDENTITY type.

    For MySQL sequences correspond to records in the SYS_SEQUENCE table.

Which is the right method to perform the data migration and update the sequence with the id of the last migrated record.

Thanks in advance.

To update the sequence of the ID you can execute the same SQL as CUBA itself calls when creating the “sequence”.

see code in the following classes:

Thanks, Alex.
I’ll try to adapt my Sql procedure code to use the same SQL as CUBA, but I didn’t find next class com.haulmont.cuba.core.sys.persistence.MssqlSequenceSupport

On the other hand, do you consider necessary to change cuba-platform documentation where it indicates that for Ms Sql Server before 2012 each sequences correspond to a SEC_UN_{domain} table? … because in my case this table doesn’t exist.


Use the Ctrl + N IDEA shortcut to open any class in the project or library.

The table is created by the application on demand when the sequence value is requested for the first time, therefore it isn’t created right after DB creation.
I’m afraid that CUBA team doesn’t have resources to cover all internal details in the documentation. As CUBA is open source, anyone is free to investigate the source code for the things they are interested in.

Thanks, Alex, for the tips explained and the suggestion for exploring the code.

It’s understandable that documentation can’t cover all possible exceptions.