1

I am persisting entity to mysql database. my requirement is to store id as BR01 , BR02 ,... etc. instead of storing as 1,2,.. etc.

how can i store id as BR01,BR02,.. etc?

I know sequence is not supported in mysql database.

my entity class is as follows :

package com.kabira.hrm.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.TableGenerator;
@Entity
@Table(name="branch")
public class Branch
{
    private Long id;
    private String country;
    private String state;
    private String city;
    private String address;
    private String phoneNumber;

    @Id
    @GeneratedValue 
    public Long getId()
    {
        return id;
    }
    public void setId(Long id)
    {
        this.id = id;
    }
    public String getCountry()
    {
        return country;
    }
    public void setCountry(String country)
    {
        this.country = country;
    }
    public String getState()
    {
        return state;
    }
    public void setState(String state)
    {
        this.state = state;
    }
    public String getCity()
    {
        return city;
    }
    public void setCity(String city)
    {
        this.city = city;
    }
    public String getAddress()
    {
        return address;
    }
    public void setAddress(String address)
    {
        this.address = address;
    }
    public String getPhoneNumber()
    {
        return phoneNumber;
    }
    public void setPhoneNumber(String phoneNumber)
    {
        this.phoneNumber = phoneNumber;
    }
    @Override
    public String toString()
    {
        return "Branch [id=" + id + "]";
    }

}
Devloper
  • 120
  • 3
  • 14
  • Are you sure you want a maximum of 99 rows in the table? Or does your requirement scale beyond two digits? – Deltharis Oct 28 '15 at 10:46
  • `@GeneratedValue` remove this annotation and set id each time adding the Branch. – Sindhoo Oad Oct 28 '15 at 10:48
  • My requirement scale beyond two digits – Devloper Oct 28 '15 at 10:52
  • I wouldn't bet on it - all comparisons will work on Strings instead of numbers, so max(id) when beyond 2 digits will not work how you hope it will which will require additional work that might not be worth it. – Deltharis Oct 28 '15 at 15:15
  • The best way to implement custom id generator using Annotation and XML Visit this link https://stackoverflow.com/a/50564556/9495226 – Sumit Anand May 28 '18 at 11:09

3 Answers3

1

You can extend org.hibernate.id.enhanced.SequenceStyleGenerator and use it with annotations @GenericGenerator.

0

You may want to check out a solution like this. Hibernate: How specify custom sequence generator class name using annotations? This way you make your own key generator.

Community
  • 1
  • 1
Dale
  • 1,613
  • 4
  • 22
  • 42
0

Write custom generator class and put the logic there to fetch max id from db and you would parse number from that id and convert that number into an integer and make increment to that number and again concat with your required prefix and return concatenated value.

write this logic here

import org.hibernate.id.IdentifierGenerator;

public class MyGenerator implements IdentifierGenerator {@
 Override
    public Serializable generate(SessionImplementor session, Object object){
    // your logic comes here.
    }

 }
Pankaj Saboo
  • 1,125
  • 1
  • 8
  • 27