本文介绍了参数值[2]与预期的类型[com.cityBike.app.model.User不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到错误消息

下面是我的代码段,如何解决此问题?

Below is my code snippet, how can I fix this issue?

Rent.java文件

File Rent.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;
    ...

文件User.java

File User.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;
...

RentService.java文件

File RentService.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;
    }
}

我应该添加在控制台上显示的"user_id"是正确的,因为它具有这样的数值ex. 2或3.请指导和协助.

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.

推荐答案

因此,当您将int传递给查询时,Rent.user_id的类型是User

The Type of Rent.user_id is User therefore when you pass a int to the query

from Rent a where a.user_id = :user_id

您正在将Userint进行比较.

相反,您需要编写

from Rent a where a.user_id.id = :user_id

我建议将Rent.user_id重命名为Rent.user,以避免此类错误.

I would recommend to rename Rent.user_id to Rent.user to avoid this kind of error.

这篇关于参数值[2]与预期的类型[com.cityBike.app.model.User不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 11:48