我有以下JPA方法:
@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();
}
它抛出以下错误信息:
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]
这是来自Spring petclinic示例应用程序的内容,因此所有相关代码都在此链接中(https://github.com/SpringSource/spring-petclinic/),包括数据库定义。我正在使用hsqldb和jpa,上面的findByPetType()方法是我编写的,不在示例应用程序中。
有人能否向我展示如何修复代码,以使其不生成此错误消息?
编辑:
我遵循了Alex的建议,并将pet.type_id更改为pet.type。现在它给我以下错误消息(typeID的值设置为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]
第二次编辑:
我采纳了Sergi Almar的建议,但现在出现以下错误:
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]
我检查了一下,调用代码将typeID初始化为"Integer typeID = 1;"。所以我不确定为什么这里没有看到一个整数。