How to use @Id with String Type in JPA / Hibernate?

If you don’t specify an id generation strategy, Hibernate will use GenerationType.AUTO. This will result in any of

AUTO – either identity column, sequence or table depending on the
underlying DB.

If you look here, you’ll notice all of those generate ids of type long, short or int, not of type String.

Say you wanted a String UUID as an id, you could use

@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
@Column(name = "PR_KEY")
private String prKey;

Leave a Comment