gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 13:54:44 27 4
gpt4 key购买 nike

我有以下类(class):

@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)

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

最佳答案

我相信您在 HQL 查询中缺少 join

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

关于java - org.hibernate.QueryException : could not resolve property: company of: Party [from Party party where party. company.name = :companyName],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29940716/

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