TypeMismatchException Provided invalid type identifier

While working on my first application in Hibernate. When I try to get a User object from the database, I get the following exception:

org.hibernate.TypeMismatchException: The wrong type identifier was provided for the org.cw.form.User class. Expected: class java.lang.Integer, got class java.lang.String in org.hibernate.event.def.DefaultLoadEventListener.onLoad (DefaultLoadEventListener.java:109) in org.hibernate.impl.SessionImpl.fireLoad (SessionImpj.mpj. 906) in org.hibernate.impl.SessionImpl.load (SessionImpl.java:823) in org.hibernate.impl.SessionImpl.load (SessionImpl.java:816)

I created a 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 object 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...

Am I missing something?

+6
6

, , . :

Provided id of the wrong type for class org.cw.form.User. 
Expected: class java.lang.Integer, got class java.lang.String

, (Integer):

String userID = "1"; //Should be Integer userID = 1 instead
session.get(User.class, userID); 
+5

, : , String .

,

class MyEntity implements Serializable {

    @Id
    Integer id

interface MyEntityRepository extends CrudRepository<MyEntity, String> {

.

String Integer .

+4

, , db, , :

@Id
@Column(name="ID")
@GeneratedValue(strategy= GenerationType.SEQUENCE, generator="user2_id_seq")
private Integer id;

See this post for more details: Sleep sequence on oracle, @GeneratedValue (strategy = GenerationType.AUTO)

+2
source
public User findClientByUsername(String login) {        
        Criteria criteria = sessionFactory.getCurrentSession().createCriteria(User.class);
        criteria.add(Restrictions.like("userName", login));
        return (User) criteria.uniqueResult();
    }

The solution to your problem.

+2
source

I also had the same problem: if you made id as an auto increment, then it does not require any setters, only get works fine, as indicated below.

  public Long getId() {
       return id;
   }
0
source

I got this problem when I tried to use inheritance for the identifier class (@IdClass).

0
source

All Articles