Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hibernate QueryException: could not resolve property in spring petclinic sample application

I have the following JPA method:

@SuppressWarnings("unchecked")
public Collection<Owner> findByPetType(Integer typeID) {
    Query query = this.em.createQuery("SELECT DISTINCT owner FROM Owner owner left join fetch owner.pets as pet WHERE pet.type_id LIKE :typeID");
    query.setParameter("typeID", typeID + "%");
    return query.getResultList();
}  

It is throwing the following error message:

org.hibernate.QueryException: could not resolve property: type_id of:  
org.springframework.samples.petclinic.model.Pet [SELECT DISTINCT owner FROM  
org.springframework.samples.petclinic.model.Owner owner left join fetch owner.pets  
as pet WHERE pet.type_id LIKE :typeID];  
nested exception is java.lang.IllegalArgumentException:  
org.hibernate.QueryException: could not resolve property: type_id of:  
org.springframework.samples.petclinic.model.Pet [SELECT DISTINCT owner FROM      
org.springframework.samples.petclinic.model.Owner owner left join fetch owner.pets  
as pet WHERE pet.type_id LIKE :typeID]  

This is from the Spring petclinic sample application, so all relevant code is at this link, including the database definition. I am using the hsqldb and jpa, the findByPetType() method above is something I wrote, which is not in the sample application.

Can anyone show me how to fix the code so that it does not produce this error message?

EDIT:

I followed Alex's advice and changed pet.type_id to pet.type. Now it is giving me the following error message (the value of typeID is set to 1):

Parameter value [1%] did not match expected type  
[org.springframework.samples.petclinic.model.PetType]; nested exception is  
java.lang.IllegalArgumentException: Parameter value [1%] did not match expected type  
[org.springframework.samples.petclinic.model.PetType]  

SECOND EDIT:

I made Sergi Almar's suggestion, and now it is throwing the following error:

Parameter value [1%] did not match expected type [java.lang.Integer]; nested exception   
is java.lang.IllegalArgumentException: Parameter value [1%] did not match expected type   
[java.lang.Integer]  

I checked, and the calling code initiates typeID as "Integer typeID = 1;" So I am not sure why it is not seeing an integer here.

like image 548
CodeMed Avatar asked Jan 12 '23 10:01

CodeMed


1 Answers

Change pet.type_id to pet.type. You have to use entity's mapping properties (but not column names) in HQL.

like image 128
Alex Avatar answered Jan 17 '23 03:01

Alex