4

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.

2 Answers 2

5

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. +1 for trying to help. I put an edit to my original posting above with the result of trying your suggestion. typeID is an Integer, being passed in as the value 1. Do you have a way to fix the new error message which resulted from trying your suggestion?
4

As this is a JPQL query, you should use the entity properties in the query (Pet has a PetType which has an id). The following code will do what you expect:

@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 = :typeID");
    query.setParameter("typeID", typeID);
    return query.getResultList();
}  

3 Comments

Thank you. +1 for trying to help. I posted the results of trying your suggestion in the second edit to my original posting above. Can you tell me how to get past the new error message?
Copy the code I posted, I've tested it myself and it works. Notice that you need to defined the id in the query (pet.type.id) not the type object. Also remove the % when setting the parameter
Yes, removing the % was the issue. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.