休眠查询异常:在 JPA 查询期间无法解析实体属性

Posted

技术标签:

【中文标题】休眠查询异常:在 JPA 查询期间无法解析实体属性【英文标题】:Hibernate queryexception: could not resolve entity property during JPA query 【发布时间】:2014-07-18 13:01:59 【问题描述】:

我正在尝试在我的休眠表中查询 RunEntity。查询中的第一个 where 子句搜索 RunEntities,其中 testName = 传递的值 testname。在堆栈跟踪中,它提到它无法在 RunEntity 中找到类型 testname 的匹配项,但 RunEntity 对象明确具有一个名为 testName 的字符串,其中包含 setter 和 getter 以及 @Column 表示法。

表设置

CREATE TABLE RunEntity (ID INTEGER IDENTITY,TestNumber INTEGER NOT NULL, TestName varchar(50) NOT NULL, ENVIRONMENT VARCHAR(50) NOT NULL, Source VARCHAR(50), Date TIMESTAMP, RESULTFILES BLOB);

查询

@Query("SELECT r FROM RunEntity r WHERE r.testName = :testname AND r.testNumber = :testnumber AND r.environment = :environment AND r.source = :source")
public List<RunEntity> findByNameNumberEnvironmentSource(
        @Param("testname") String testname,
        @Param("testnumber") int testnumber,
        @Param("environment") String environment,
        @Param("source") String source);

实体

package com.web_application;

 import java.sql.Timestamp;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Lob;

@Entity
@Table(name = "TESTRUNS")
public class RunEntity 

private int ID;
private int testNumber;
private String testName;
private String environment;
private String source;
private String passOrFail;
private Timestamp date;
private byte[] resultFiles;


@Id
@Column(name = "ID")
@GeneratedValue
public int getID()

    return this.ID;

public void setID(int ID)this.ID = ID;

@Column(name="TestNumber")
public int getTestNumber()

    return this.testNumber;

public void setTestNumber(int testNum)this.testNumber = testNum;

@Column(name="TestName")
public String testName()

    return this.testName;

public void setTestName(String testName)this.testName = testName;

@Column(name="Environment")
public String getEnvironment()

    return this.environment;

public void setEnvironment(String enviro)this.environment = enviro;

@Column(name="Source")
public String getSource()

    return this.source;

public void setSource(String src)this.source = src;

@Column(name="PassOrFail")
public String getPassOrFail()

    return this.passOrFail;

public void setPassOrFail(String pOrF)this.passOrFail = pOrF;

@Column(name="Date")
public Timestamp getDate()

    return this.date;

public void setDate(Timestamp dates)this.date = dates;

@Lob
@Column(name="ResultFiles")
public byte[] getResultFiles()

    return this.resultFiles;

public void setResultFiles(byte[] file)this.resultFiles = file;


堆栈跟踪的一部分

Caused by: org.hibernate.QueryException: could not resolve property: testname of: com.web_application.RunEntity [SELECT r FROM com.web_application.RunEntity r WHERE r.testname = :testname AND r.testNumber = :testnumber AND r.environment = :environment AND r.source = :source]
at org.hibernate.QueryException.generateQueryException(QueryException.java:137)
at org.hibernate.QueryException.wrapWithQueryString(QueryException.java:120)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:234)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:158)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:126)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:88)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:190)
at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:301)
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236)
at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1800)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:328)
... 66 more

【问题讨论】:

【参考方案1】:

改变这个

 @Column(name="TestName")
public String testName()

    return this.testName;

@Column(name="TestName")
public String getTestName()

    return this.testName;

属性访问命名约定很重要。例如尝试使用 IDE(Eclipse Getter-Setter,而不是手动执行)

【讨论】:

感谢您的帮助,有时当您离项目太近时很难看到这些东西。【参考方案2】:

将您的 testName() getter 更正为 getTestName()。您正在使用属性访问并且必须遵守 JavaBeans 约定。

【讨论】:

以上是关于休眠查询异常:在 JPA 查询期间无法解析实体属性的主要内容,如果未能解决你的问题,请参考以下文章

休眠条件查询到 JPA 条件查询

使用对象属性的 JPA 命名查询

使用 JPA 原生查询时是不是必须选择所有实体属性?

spring data jpa 创建方法名进行简单查询

休眠查询(不是 SQL)日志记录

如何使用 JPA 和 Hibernate 通过自定义对象实体属性进行查询