gpt4 book ai didi

spring - 使用 Hibernate 使用外键 (where) 查询数据库表

转载 作者:行者123 更新时间:2023-12-02 04:51:47 24 4
gpt4 key购买 nike

我是 hibernate 新手,所以这个问题可能看起来很愚蠢。

我有两个表:

应用:

@Entity
@Table
public class Application extends BaseSimpleEntity {
@Column(nullable = false)
private String appID;
@OneToOne(cascade = CascadeType.ALL)
@Searcheable
private LocalizedString name;
...

编译:

@Table
@Entity
public class Compilation extends BaseSimpleEntity {

@Column(nullable = false)
private String uid;
@ManyToOne
private Application application;
@Column
private DateTime creationDate;
@Column
private DateTime finishDate;
@Column
private String path;
....

我想获取与给定应用程序匹配的编译列表。

我做了以下查询:

@Query("FROM Compilation c WHERE c.Application.id = :applicationId")
List<Compilation> findValidCompialiton(@Param("applicationId") Long applicationId);

但是它不起作用。

错误:

Caused by: org.hibernate.QueryException: could not resolve property: Application of: xx.xx.xx.xx.xx.Compilation [FROM xx.xx.xx.xx.xx.Compilation c WHERE c.Application.id = :applicationId] 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.(HQLQueryPlan.java:126) at org.hibernate.engine.query.spi.HQLQueryPlan.(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)

最佳答案

您的查询应该是:

@Query("FROM Compilation c WHERE c.application.appID = :applicationId")

您已经为实体创建了别名 c,Hibernate 尝试检查 Compilation,现在您可以访问 Compilation 实体的属性。现在,Compilation 类中的属性是 application,此 application 属性代表具有字段 Application应用程序ID

关于spring - 使用 Hibernate 使用外键 (where) 查询数据库表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26018301/

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