TypeMismatchException the provided ID is of a wrong type
-
13-11-2019 - |
Frage
While working on my first app in Hibernate. While trying to retrieve a User object from the DB i am getting the following exception:
org.hibernate.TypeMismatchException: Provided id of the wrong type for class org.cw.form.User. Expected: class java.lang.Integer, got class java.lang.String at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:109) at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:906) at org.hibernate.impl.SessionImpl.load(SessionImpl.java:823) at org.hibernate.impl.SessionImpl.load(SessionImpl.java:816)
I have created the USERS table with the following postgreSQL:
CREATE SEQUENCE user2_id_seq;
CREATE TABLE USERS(id integer NOT NULL DEFAULT nextval('user2_id_seq'), user_name varchar(45) NOT NULL UNIQUE , password varchar(45) NOT NULL, email varchar(45) NOT NULL, PRIMARY KEY (id));
And the User entity is defined as such:
@Entity @Table(name="USERS") public class User {
@Id
@Column(name="ID")
@GeneratedValue(strategy= GenerationType.IDENTITY)
private Integer id;
@Column(name="USER_NAME", unique = true)
private String userName;
@Column(name="PASSWORD")
private String password;
@Column(name="EMAIL")
private String email; .. all the getters and setters...
I am I missing something?
Lösung
It would be easier to answer if you would show how do you retrieve Users. Based to message:
Provided id of the wrong type for class org.cw.form.User.
Expected: class java.lang.Integer, got class java.lang.String
I guess you are providing String instead of correct type (Integer):
String userID = "1"; //Should be Integer userID = 1 instead
session.get(User.class, userID);
Andere Tipps
I had a different culprit creating this issue: I had copy-pasted another entity's repository which used a String as primary key type.
So I had
class MyEntity implements Serializable {
@Id
Integer id
in combination with
interface MyEntityRepository extends CrudRepository<MyEntity, String> {
which produced the error message.
Simply changing the interface type from String to Integer resolved the issue for me.
I don't really know if it'll solve your issue, but since you are using sequences to generate ids on the db side, I think you should use a Sequence generator :
@Id
@Column(name="ID")
@GeneratedValue(strategy= GenerationType.SEQUENCE, generator="user2_id_seq")
private Integer id;
Please see this post for details : Hibernate sequence on oracle, @GeneratedValue(strategy = GenerationType.AUTO)
public User findClientByUsername(String login) {
Criteria criteria = sessionFactory.getCurrentSession().createCriteria(User.class);
criteria.add(Restrictions.like("userName", login));
return (User) criteria.uniqueResult();
}
Solution of your problem.
I also had the same issue, If you have made id as autoincrement then it doesn't require any setters, only get is fine as mentioned below.
public Long getId() {
return id;
}
I got this problem, when I tried to use inheritance for the identifier class (@IdClass).