Problem with Sequences with the method getCurrentValue() and setStartValue(0)

Hello,

When you use for the first time you get the number 1, but it should return 0.
When you use for the second time you get the number 1, andit should return 1.

So when i try to show beforehand the next number for the first document i use getCurrentValue() + 1 and it returns 2 (error).
But for the second document, the same formula returns 2 (ok)

I tried to use setStartValue(0) but again a get a number 2 as answer.

I think the logic should be changed. Thanks in advance for your help.

Could you provide the code that you use to test this behavior?
And what is your database?

Hello,
I use a postgres database. The code is in an edit window and is llike this:

@Subscribe
public void onInitEntity(InitEntityEvent<PresupuestoRevision> event) {
    //visibles
    LocalDate hoy = LocalDate.now();
    
    //CODIGO letra + añoMes + 4corr
    String idCorrel = "D"+hoy.format(DateTimeFormatter.ofPattern("YYYYMM"));
    DecimalFormat df = new DecimalFormat("0000");
    String codigo = idCorrel + df.format( (sequences.getCurrentValue( Sequence.withName(idCorrel) )+1) );
    event.getEntity().setCodigo(codigo);
}

Did you try using createNextValue() instead of getCurrentValue() + 1?

I can’t use createNextValue() because it will create a new value and i need to show beforehand the next value, but if the document is canceled the code should not increment. If i use createNextValue(), the value increments even if the document is canceled.

Unfortunately, we cannot guarantee the correct behavior of getCurrentValue() for your case, it’s even stated in the Javadocs:

/**
 * Returns the current value of the sequence. For some implementations
 * {@link #createNextValue(Sequence)} must be called at least once beforehand.
 *
 * @param sequence object {@link Sequence}
 * @return          current value
 */
long getCurrentValue(Sequence sequence);

So the getCurrentValue method should be used only for information purpose, for example to show the last returned value in an administrative UI.

Perhaps you need to develop your own counting mechanism based on a regular table.

Dear Konstantin, thank you anyway, i’ll follow your advice for that part.