14

i want generate Custom Id in JPA it must be primary key of table. there are many examples to create Custom Id using hibernate like this i want same implementation but in JPA.The id must be alphanumeric like STAND0001

Thanks.

Amol Raje
  • 928
  • 3
  • 9
  • 16
  • There is no such thing as spring JPA. There is JPA, the specification, and some implementations of JPA, like Hibernate or EclipseLink. Spring doesn't have, and is not a JPA implementation. So check the documentation of your **actual** JPA implementation. – JB Nizet Nov 13 '17 at 07:34
  • JB Nizet thanks for your reply ..i will edit question.. can you please tell me how to generate custom id in JPA – Amol Raje Nov 13 '17 at 07:40
  • the id must be combination of string and numbers – Amol Raje Nov 13 '17 at 07:42
  • i am using dependency spring-boot-starter-data-jpa version 1.4.7.RELEASE and implementation in hibernate – Amol Raje Nov 13 '17 at 07:50
  • Which itself depends on hibernate. So you're using hibernate. So check the documentation of hibernate. – JB Nizet Nov 13 '17 at 07:53
  • Can you post some code, i.e your entity/bo/model and how you want to generate your primary key? i.e number or string? – Sreenath Reddy Nov 13 '17 at 07:58
  • Sreenath Reddy ..edit same in question – Amol Raje Nov 13 '17 at 08:02
  • I think the question was how to create generator not depending on specific JPA implementation. Looks like JPA doesn't have such functionality which is a pity, because I see no problem to define it in JPA. – Marx Sep 21 '18 at 07:23

1 Answers1

21

You can do it using GenericGenerator like this :

 @Entity
public class Client {

    @Id
    @GenericGenerator(name = "client_id", strategy = "com.eframe.model.generator.ClientIdGenerator")
    @GeneratedValue(generator = "client_id")  
    @Column(name="client_id")
    private String clientId;
}

and the custom generator class (will add prefix to the ID, you can make it do what you like):

public class ClientIdGenerator implements IdentifierGenerator {

@Override
public Serializable generate(SessionImplementor session, Object object)
        throws HibernateException {

    String prefix = "cli";
    Connection connection = session.connection();

    try {
        Statement statement=connection.createStatement();

        ResultSet rs=statement.executeQuery("select count(client_id) as Id from Client");

        if(rs.next())
        {
            int id=rs.getInt(1)+101;
            String generatedId = prefix + new Integer(id).toString();
            return generatedId;
        }
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return null;
}
}
Bara' ayyash
  • 1,913
  • 1
  • 15
  • 25
  • 7
    GenericGenerator is from hibernate package, and the question was how to do that independently of JPA provicder – Marx Sep 21 '18 at 07:24
  • 1
    @Marx I think you'll have to use the SessionCustomizer in EclipseLink. There is no mention of the said behavior in the JSR 338. – LalakaJ Nov 05 '18 at 08:51
  • 2
    In Spring Boot 2.7 with Hibernate 5.6.9 you have to use `SharedSessionContractImplementor` instead of `SessionImplementor` – swissbuechi Jun 22 '22 at 07:49