TypeMismatchException提供的ID类型错误 [英] TypeMismatchException the provided ID is of a wrong type
问题描述
在Hibernate中开发我的第一个应用程序时.尝试从数据库检索User对象时,出现以下异常:
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:提供了错误类型的ID,用于org.cw.form.User类.预期:类java.lang.Integer,得到了类java.lang.String在org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:109)在org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:906)在org.hibernate.impl.SessionImpl.load(SessionImpl.java:823)在org.hibernate.impl.SessionImpl.load(SessionImpl.java:816)
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)
我使用以下postgreSQL创建了USERS表:
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));
User实体是这样定义的:
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...
我想念什么吗?
推荐答案
如果显示如何检索用户,将更容易回答.根据消息:
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
我想您提供的是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);
这篇关于TypeMismatchException提供的ID类型错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!