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.

4

2 回答 2

5

更改pet.type_idpet.type。您必须在HQL.

于 2013-09-05T23:17:42.537 回答
4

由于这是一个 JPQL 查询,您应该在查询中使用实体属性(Pet 有一个 PetType,它有一个 id)。以下代码将执行您的预期:

@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();
}  
于 2013-09-05T23:27:32.563 回答