spring TypeMismatchException 提供的 ID 类型错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9467705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeMismatchException the provided ID is of a wrong type
提问by special0ne
While working on my first app in Hibernate. While trying to retrieve a User object from the DB i am getting the following exception:
在 Hibernate 中开发我的第一个应用程序时。在尝试从数据库检索用户对象时,我收到以下异常:
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)
org.hibernate.TypeMismatchException:为类 org.cw.form.User 提供了错误类型的 id。预期:类 java.lang.Integer,在 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)
I have created the USERS table with the following postgreSQL:
我使用以下 postgreSQL 创建了 USERS 表:
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?
我错过了什么吗?
采纳答案by Mikko Maunu
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);
回答by Jan Papenbrock
I had a different culprit creating this issue: I had copy-pasted another entity's repository which used a String as primary key type.
我有另一个造成此问题的罪魁祸首:我复制粘贴了另一个实体的存储库,该存储库使用 String 作为主键类型。
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.
只需将接口类型从 String 更改为 Integer 即可解决我的问题。
回答by HelterShelter
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.
解决您的问题。
回答by ndeverge
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,我认为您应该使用序列生成器:
@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)
有关详细信息,请参阅此帖子:oracle 上的休眠序列,@GeneratedValue(strategy = GenerationType.AUTO)
回答by Brahmadev
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.
我也遇到了同样的问题,如果您将 id 设为自动增量,那么它不需要任何设置器,只有如下所述的 get 就可以了。
public Long getId() {
return id;
}
回答by Spille
I got this problem, when I tried to use inheritance for the identifier class (@IdClass).
当我尝试对标识符类 (@IdClass) 使用继承时,我遇到了这个问题。

