gpt4 book ai didi

java - hibernate 查询异常 : could not resolve property in spring petclinic sample application

转载 作者:搜寻专家 更新时间:2023-11-01 01:35:13 25 4
gpt4 key购买 nike

我有以下 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 示例应用程序,因此所有相关代码 is at this link ,包括数据库定义。我使用的是 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;”所以我不确定为什么在这里看不到整数。

最佳答案

pet.type_id 更改为 pet.type。您必须在 HQL 中使用实体的映射属性(但不是列名)。

关于java - hibernate 查询异常 : could not resolve property in spring petclinic sample application,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18647300/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com