3

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)

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

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

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

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;
    }
}

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.

2 Answers2

12

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

you are comparing a User with an 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.

wero
  • 32,544
  • 3
  • 59
  • 84
  • Thanks very much helped. I have an additional problem so after displaying on display at the "user_id" I have a number of such 2. How now easily turn it on full name, eg. John? –  Jan 17 '16 at 12:16
  • 1
    @phoenix37 given a user id you can query the User object by `User user = em.find(User.class,user_Id)` and then access its name. – wero Jan 17 '16 at 12:21
  • I had same problem which I got solve by your answer. Addtiionally If you could add some more clearity on last line " I would recommend to rename Rent.user_id to Rent.user to avoid this kind of error " I am not able to figure out how and what exactly to change. – Nadim Nov 18 '21 at 09:34
0

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

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();
karim mohsen
  • 2,164
  • 1
  • 15
  • 19