org.hibernate.QueryException:无法解析属性:公司:派对[来自派对的party.company.name =:companyName]

时间:2015-04-29 10:14:10

标签: java hibernate

我有以下课程:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Party", propOrder = {
    "company"
})
@Entity(name = "Party")
@Table(name = "PARTY")
@Inheritance(strategy = InheritanceType.JOINED)
public class Party implements Equals, HashCode, ToString
{

    @XmlElement(name = "Company")
    protected CompanyType company;


    /**
     * Gets the value of the company property.
     * 
     * @return
     *     possible object is
     *     {@link CompanyType }
     *     
     */ 
    @ManyToOne(targetEntity = CompanyType.class, cascade = {CascadeType.ALL})
    @JoinColumn(name = "COMPANY_0")
    public CompanyType getCompany() {
        return company;
    }

    /**
     * Sets the value of the company property.
     * 
     * @param value
     *     allowed object is
     *     {@link CompanyType }
     *     
     */
    public void setCompany(CompanyType value) {
        this.company = value;
    }
   }

这是我的CompanyType类:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "CompanyType", propOrder = {"name"})

@Entity(name = "CompanyType")
@Table(name = "COMPANYTYPE")
@Inheritance(strategy = InheritanceType.JOINED)
public class CompanyType implements Equals, HashCode, ToString {

@XmlJavaTypeAdapter(StringTrimAdapter.class)
protected String name;


/**
 * Gets the value of the name property.
 * 
 * @return
 *     possible object is
 *     {@link String }
 *     
 */
@Basic
@Column(name = "NAME", columnDefinition = "text")
public String getName() {
    return name;
}

/**
 * Sets the value of the name property.
 * 
 * @param value
 *     allowed object is
 *     {@link String }
 *     
 */
public void setName(String value) {
    this.name = value;
}
}

我使用以下查询来获取数据:

Query q = null;

q = session.createQuery(
"from Party party where party.company.name = :companyName")
            .setParameter("companyName", companyName);

    return ClassUtils.cast(q.setResultTransformer(DistinctRootEntityResultTransformer.INSTANCE).list());

但是,我收到了以下错误:

org.hibernate.QueryException: could not resolve property: company of: Party [from Party party where party.company.name = :companyName] 

    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 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:356)
    at com.sun.proxy.$Proxy104.createQuery(Unknown Source)

任何帮助将不胜感激。 谢谢。

1 个答案:

答案 0 :(得分:0)

我相信你在HQL查询中缺少join;

q = session.createQuery(
"from Party party inner join party.company as company where company.name = :companyName")
            .setParameter("companyName", companyName);