java 参数值 [2] 与预期类型 [com.cityBike.app.model.User 不匹配
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34837963/
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
Parameter value [2] did not match expected type [com.cityBike.app.model.User
提问by wero
I receive the error of
我收到错误
java.lang.IllegalArgumentException: Parameter value [2] did not match expected type [com.cityBike.app.model.User (n/a)] at org.hibernate.jpa.spi.BaseQueryImpl.validateBinding(BaseQueryImpl.java:885) at org.hibernate.jpa.internal.QueryImpl.access$000(QueryImpl.java:80) at org.hibernate.jpa.internal.QueryImpl$ParameterRegistrationImpl.bindValue(QueryImpl.java:248) at org.hibernate.jpa.spi.BaseQueryImpl.setParameter(BaseQueryImpl.java:631) at org.hibernate.jpa.spi.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:180) at org.hibernate.jpa.spi.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:49) at com.cityBike.app.service.RentService.getAllByUser(RentService.java:22)
java.lang.IllegalArgumentException:参数值 [2] 与 org.hibernate.jpa.spi.BaseQueryImpl.validateBinding(BaseQueryImpl.java:885) 的预期类型 [com.cityBike.app.model.User (n/a)] 不匹配) 在 org.hibernate.jpa.internal.QueryImpl.access$000(QueryImpl.java:80) 在 org.hibernate.jpa.internal.QueryImpl$ParameterRegistrationImpl.bindValue(QueryImpl.java:248) 在 org.hibernate.jpa.spi .BaseQueryImpl.setParameter(BaseQueryImpl.java:631) at org.hibernate.jpa.spi.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:180) at org.hibernate.jpa.spi.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:49) at com.cityBike.app.service.RentService.getAllByUser(RentService.java:22)
Below is my code snippet, how can I fix this issue?
下面是我的代码片段,我该如何解决这个问题?
File Rent.java
文件租用.java
@Entity
@Table(name="Rent")
public class Rent implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "id")
private Integer id;
@ManyToOne
@JoinColumn(name = "start_id")
private Station start_id;
@ManyToOne
@JoinColumn(name = "meta_id")
private Station meta_id;
@ManyToOne
@JoinColumn(name = "user_id")
private User user_id;
...
File User.java
文件用户.java
@Entity
@Table(name="Users")
public class User implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "id")
private Integer id;
@Column(name = "login")
private String login;
...
File RentService.java
文件租用服务.java
@Service
public class RentService {
@PersistenceContext
private EntityManager em;
@Transactional
public List<Rent> getAllByUser(int user_id){
System.out.println(user_id);
List<Rent> result = em.createQuery("from Rent a where a.user_id = :user_id", Rent.class).setParameter("user_id", user_id).getResultList();
System.out.println(result);
return result;
}
}
I should add that "user_id" when displayed on the console is correct as it has such a numerical value ex. 2 or 3. Please guidance and assistance.
我应该添加在控制台上显示的“user_id”是正确的,因为它具有这样的数值 ex。2 或 3。请指导和帮助。
回答by wero
The Type of Rent.user_id
is User therefore when you pass a int
to the query
Rent.user_id
因此,当您将 a 传递int
给查询时,类型是用户
from Rent a where a.user_id = :user_id
you are comparing a User
with an int
.
您正在将 aUser
与a 进行比较int
。
Instead you need to write
相反,你需要写
from Rent a where a.user_id.id = :user_id
I would recommend to rename Rent.user_id
to Rent.user
to avoid this kind of error.
我建议重命名Rent.user_id
以Rent.user
避免这种错误。
回答by karim mohsen
You can use this query :-
您可以使用此查询:-
The user_id
in Rent
class isn't int it's an object of type User
so you have to get the User
object by using it's id
该user_id
在Rent
类不是INT它的类型的对象User
,所以你必须让User
使用它的ID对象
List<Rent> result = em.createQuery("from Rent a where a.user_id = :user_id", Rent.class).setParameter("user_id", em.getReference(User.class, user_id)).getResultList();
Or you can write :-
或者你可以写:-
List<Rent> result = em.createQuery("from Rent a where a.user_id.id = :user_id",Rent.class).setParameter("user_id", user_id).getResultList();