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

4

43

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
Homozygote answered 4/9, 2013 at 19:57 Comment(4)
how was defined the method getPrKey()? are you sure that it's returning a String object?Retrace
Yes, it returns String only.Homozygote
any progress @Homozygote ? its strange that is seems to be so hard to simply have a String Id. I have the same problem.Tsan
I think you are using a wrong json key for field. Remember the json key is always what is mentions in java class. Not the Column name.Herpetology
W
57

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;
Whiskey answered 4/9, 2013 at 20:4 Comment(1)
Value of this key is provided by some other entity in my program. I dont want it to be generated automatically. I think GeneratedValue annotation will make it generated value. Please correct meHomozygote
S
3

Check the PR_KEY data type in database table. This problem might occur, if the column is of type Number and you are trying to map the same to String in your entity.

Same applies to the coulmn with generated Ids.

Sanford answered 4/9, 2013 at 20:5 Comment(2)
It is VARCHAR2 in database.Homozygote
@Homozygote I have a similar issue. Did you find a solution? Thanks!Haddington
C
1

A very simple way to use string as primary key by using strategy = "uuid" in the annotation @GenericGenerator(name = "system-uuid", strategy = "uuid")

This will generates a unique 36-character id

@Entity
@Data
@NoArgsConstructor
public class ToDoClass {

    @NotNull
    @Id
    @GeneratedValue(generator = "system-uuid")
    @GenericGenerator(name = "system-uuid", strategy = "uuid")
    private String id;
     
    //...
      
}
Cavesson answered 2/10, 2021 at 18:54 Comment(0)
G
0

When String is used as a id, same type should be used also when finding entity via Session/EntityManager:

Instead of providing Long:

Long key = 1L;
MyEntity me = session.get(MyEntity.class, key); 
//or 
MyEntity me = entityManager.find(MyEntity.class, key); 

String should be given:

String key = "1";
MyEntity me = session.get(MyEntity.class, key); 
//or 
MyEntity me = entityManager.find(MyEntity.class, key); 
Granulite answered 4/9, 2013 at 20:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.