我有以下实体
@Entity
@Table(name = "person")
public class Person implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
// Other fields
}
@Entity
@Table(name = "agreement")
public class Agreement implements Serializable {
private static final long serialVersionUID = 15L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@ManyToOne
@JoinColumn(name = "person_id", nullable = false)
private Person person;
@ManyToOne
@JoinColumn(name = "company_id", nullable = false)
private Company company;
}
@Entity
@Table(name = "company")
public class Company implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(name = "name", length = 100)
private String name;
}
我需要让所有与该公司达成协议且公司ID为45的人员。以下是我为获取详细信息而编写的本机查询。
SELECT distinct p.* ROM person p INNER JOIN agreement a ON a.person_id = p.id where a.company_id = 45;
但是我被告知将其转换为JPA实体查询。我想出了以下内容,
SELECT distinct p FROM Person p INNER JOIN Agreement a WHERE a.person = p AND a.company.id = 45;
但这给了我以下例外,
由于:org.hibernate.hql.internal.ast.QuerySyntaxException:路径 期待加入! [从人p INNER JOIN中选择不同的p 在以下位置同意a.person = p AND a.company.id = 45] org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:74) 在 org.hibernate.hql.internal.ast.ErrorCounter.throwQueryException(ErrorCounter.java:91) 在 org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:268) 在 org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:190) 在 org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:142) 在 org.hibernate.engine.query.spi.HQLQueryPlan。(HQLQueryPlan.java:115) 在 org.hibernate.engine.query.spi.HQLQueryPlan。(HQLQueryPlan.java:76) 在 org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:150) 在 org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:302) 在 org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:240) 在 org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1894) 在 org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:305
有人可以帮我解决我在查询中做错了什么吗?
答案 0 :(得分:0)
JPA使用HQL语句中的路径将一个实体与另一个实体相关联,因为它没有找到它要抱怨的实体。您可以尝试以下查询。
SELECT distinct p FROM Agreement a JOIN a.person p WHERE a.company.id = 45;