0

https://vladmihalcea.com/how-to-implement-a-custom-string-based-sequence-identifier-generator-with-hibernate/

i tried to this for a field that is not primary key.

Also same solution for here: How to implement IdentifierGenerator with PREFIX and separate Sequence for each entity

But even it does not go to Java method when i run the program. It saves as null.

And i cant see the log that i put inside my class. There is no log for my class.

I copied from that blog but my code is:

public class StringSequenceIdentifier
        implements IdentifierGenerator, Configurable {

    public static final String SEQUENCE_PREFIX = "sequence_prefix";

    private String sequencePrefix;

    private String sequenceCallSyntax;

    @Override
    public void configure(
            Type type, Properties params, ServiceRegistry serviceRegistry)
            throws MappingException {
        System.out.println("xxx");
        final JdbcEnvironment jdbcEnvironment =
                serviceRegistry.getService(JdbcEnvironment.class);
        final Dialect dialect = jdbcEnvironment.getDialect();

        final ConfigurationService configurationService =
                serviceRegistry.getService(ConfigurationService.class);
        String globalEntityIdentifierPrefix =
                configurationService.getSetting( "entity.identifier.prefix", String.class, "SEQ_" );

        sequencePrefix = ConfigurationHelper.getString(
                SEQUENCE_PREFIX,
                params,
                globalEntityIdentifierPrefix);

        final String sequencePerEntitySuffix = ConfigurationHelper.getString(
                SequenceStyleGenerator.CONFIG_SEQUENCE_PER_ENTITY_SUFFIX,
                params,
                SequenceStyleGenerator.DEF_SEQUENCE_SUFFIX);

        final String defaultSequenceName = ConfigurationHelper.getBoolean(
                SequenceStyleGenerator.CONFIG_PREFER_SEQUENCE_PER_ENTITY,
                params,
                false)
                ? params.getProperty(JPA_ENTITY_NAME) + sequencePerEntitySuffix
                : SequenceStyleGenerator.DEF_SEQUENCE_NAME;

        sequenceCallSyntax = dialect.getSequenceNextValString(
                ConfigurationHelper.getString(
                        SequenceStyleGenerator.SEQUENCE_PARAM,
                        params,
                        defaultSequenceName));
    }

    @Override
    public Serializable generate(SharedSessionContractImplementor session, Object obj) {
        System.out.println("xxx");
        if (obj instanceof Identifiable) {
            Identifiable identifiable = (Identifiable) obj;
            Serializable id = identifiable.getId();
            if (id != null) {
                return id;
            }
        }
        long seqValue = ((Number) Session.class.cast(session)
                .createSQLQuery(sequenceCallSyntax)
                .uniqueResult()).longValue();

        return sequencePrefix + String.format("%011d%s", 0 ,seqValue);
    }
}

That is in my domain:

@GenericGenerator(
        name = "assigned-sequence",
        strategy = "xxxxxx.StringSequenceIdentifier",
        parameters = {
                @org.hibernate.annotations.Parameter(
                        name = "sequence_name", value = "hibernate_sequence"),
                @org.hibernate.annotations.Parameter(
                        name = "sequence_prefix", value = "CTC_"),
        }
)
@GeneratedValue(generator = "assigned-sequence", strategy = GenerationType.SEQUENCE)
private String referenceCode;

WHAT I WANT IS I need a unique field, which is not primary. So, i decided that incrementing is best solution because otherwise, i have to check for each created random if it exists in database (i also open suggestions for this).

It will be around 5-6 characters and alphanumeric.

I want to make JPA increment this but it seems i cant do it.

vegan
  • 117
  • 1
  • 11

1 Answers1

1

This is very similar to Hibernate JPA Sequence (non-Id) but I don't think it's an exact duplicate. Yet the answers seem to apply and they seem to suggest the following strategies:

  1. Make the field to be generated a reference to an entity with the only purpose that the field now becomes an ID and can get generated by the usual strategies. https://stackoverflow.com/a/536102/66686

  2. Use @PrePersist to fill the field before it gets persisted. https://stackoverflow.com/a/35888326/66686

  3. Make it @Generated and generate the value in the database using a trigger or similar. https://stackoverflow.com/a/283603/66686

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348