Issue
I have one entity which contains primary key of type string. This entity model is as follows:
@Entity
public class MyEntity {
@Id
@Column(name="PR_KEY", unique=true)
private String prKey;
....
....
}
But I am facing issue saying TypeMismatch.
org.hibernate.TypeMismatchException: Provided id of the wrong type. Expected: class java.lang.String, got class java.lang.Long
Solution
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;
Answered By - Sotirios Delimanolis