How to write a custom sequence-based String Id generator with JPA and Hibernate

i am new to hibernate. i was trying to create custom id of type string using hibernate and mysql. can someone help me with how to override generator class?

here is my hbm file

<class name="com.usermanagement.model.Employee" table="EMPLOYEE">
    <id name="id" column= "ID">
	<generator class="com.customid.EmployeeIdGenerator"/>
    <property name="name" type="java.lang.String">
        <column name="NAME" />

The id i am trying to generate is like str001, str002, str003…

You can write a String identifier generator like this one:

public class StringSequenceIdentifier implements 
        IdentifierGenerator, Configurable {

    public static final String SEQUENCE_PREFIX = "sequence_prefix";

    private String sequencePrefix;

    private String sequenceCallSyntax;

    public void configure(
            Type type,
            Properties params,
            ServiceRegistry serviceRegistry)
        throws MappingException {

        final JdbcEnvironment jdbcEnvironment = serviceRegistry.getService(

        final Dialect dialect = jdbcEnvironment.getDialect();

        final ConfigurationService configurationService = serviceRegistry.getService(

        String globalEntityIdentifierPrefix = configurationService.getSetting(

        sequencePrefix = ConfigurationHelper.getString(

        final String sequencePerEntitySuffix = ConfigurationHelper.getString(

        boolean preferSequencePerEntity = ConfigurationHelper.getBoolean(

        final String defaultSequenceName = preferSequencePerEntity
                ? params.getProperty(JPA_ENTITY_NAME) + sequencePerEntitySuffix
                : SequenceStyleGenerator.DEF_SEQUENCE_NAME;

        sequenceCallSyntax = dialect.getSequenceNextValString(

    public Serializable generate(SharedSessionContractImplementor session, Object obj) {
        if (obj instanceof Identifiable) {
            Identifiable identifiable = (Identifiable) obj;
            Serializable id = identifiable.getId();

            if (id != null) {
                return id;

        long seqValue = ((Number)

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

And map it like this:

	name = "assigned-sequence",
	strategy = "",
	parameters = {
			name = "sequence_name", value = "hibernate_sequence"),
			name = "sequence_prefix", value = "CTC_"),
	generator = "assigned-sequence",
	strategy = GenerationType.SEQUENCE)
private String id;

Check out this article for more details.

Thanks but is it possible to do it with just xml instead of using annotations for mapping?

why is it asking me to implement unimplemented methods in class name

it implements the following sets of code
public Serializable generate(SharedSessionContractImplementor session, Object object) throws HibernateException {
// TODO Auto-generated method stub
return null;

also it asks me to remove @Override from generate method

Yes, you can use XML too, but why would you use a deprecated mapping model?

It seems you are using an old Hibernate version. Upgrade to 5.2 and it will work. Or adapt it to the old method signature. It’s very easy. You’ll figure it out.

i’m using the latest version of hibernate and JPA

These are my gradle dependencies
implementation ‘org.springframework.boot:spring-boot-starter-web’
implementation ‘mysql:mysql-connector-java:8.0.15’
implementation ‘org.hibernate:hibernate-core:5.4.1.Final’
implementation ‘org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.0.Final’

i m confused.

Check out my StringSequenceIdentifierTest in my high-performance-java-persistence GitHub repository.

Just run the test and you’ll see that it works like a charm.

Is that custom string id possible with MySQL db?

MySQL does not support sequences, so you could not do that for the PK. Only if thevPK is identity and you use that value to build a secondary column with the String prefix amd the ID value combined.