gpt4 book ai didi

java - 为什么我在尝试执行涉及 2 个表之间联接的 HQL 查询时会获取此 "ErrorCounter - Path expected for join!"?

转载 作者:行者123 更新时间:2023-12-01 09:10:31 27 4
gpt4 key购买 nike

我正在开发一个 Spring 项目,使用 Spring Data JPA 执行查询。

所以我有这两个实体类:

1) Room 代表住宿的房间:

@Entity
@Table(name = "room")
public class Room implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;

@ManyToOne
@JoinColumn(name = "id_accomodation_fk", nullable = false)
private Accomodation accomodation;

@ManyToOne
@JoinColumn(name = "id_room_tipology_fk", nullable = false)
private RoomTipology roomTipology;

@Column(name = "room_number")
private String number;

@Column(name = "room_name")
private String name;

@Column(name = "room_description")
@Type(type="text")
private String description;

@Column(name = "max_people")
private Integer maxPeople;

@Column(name = "is_enabled")
private Boolean isEnabled;

// CONSTRUCTOR, GETTER AND SETTER METHODS
}

正如你所看到的,这个类包含这个字段:

@ManyToOne
@JoinColumn(name = "id_room_tipology_fk", nullable = false)
private RoomTipology roomTipology;

将许多 Room 实例链接到 RoomTipology 实例。

2)然后我有 RoomTipology 实体类:

@Entity
@Table(name = "room_tipology")
public class RoomTipology implements Serializable{

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;

@Column(name = "tipology_name")
private String name;

@Column(name = "tipology_description")
private String description;

@Column(name = "time_stamp")
private Date timeStamp;

@OneToMany(mappedBy = "roomTipology")
private List<Room> rooms;

@OneToOne(mappedBy = "roomTipology")
private RoomRate roomRate;

// CONSTRUCTOR, GETTER AND SETTER METHODS
}

然后我有 RoomTipology 实体类的存储库类,如下所示:

我想在其中实现一个返回与特定房间 ID 关联的房间提示的方法。所以我想实现此查询的 HQL 版本:

SELECT * 
FROM `room_tipology` rt
INNER JOIN room r
ON rt.id = r.id_room_tipology_fk
WHERE r.id = 7

所以我是这样的:

@Repository
@Transactional(propagation = Propagation.MANDATORY)
public interface RoomTipologyDAO extends JpaRepository<RoomTipology, Long> {

@Query("from RoomTipology rt JOIN Room r ON rt.id = r.id_room_tipology_fk WHERE r.id = :roomId")
RoomTipology getInfoByRoomId(Long id);

}

问题是执行我的应用程序时我收到此错误消息:

[ERROR] 2016-12-03 11:47:41 [org.hibernate.hql.internal.ast.ErrorCounter.reportError(ErrorCounter.java:73)] [main] ErrorCounter -  Path expected for join!
antlr.SemanticException: Path expected for join!
at org.hibernate.hql.internal.ast.HqlSqlWalker.createFromJoinElement(HqlSqlWalker.java:385) [hibernate-core-4.3.11.Final.jar:4.3.11.Final]
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.joinElement(HqlSqlBaseWalker.java:3903) [hibernate-core-4.3.11.Final.jar:4.3.11.Final]
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3689) [hibernate-core-4.3.11.Final.jar:4.3.11.Final]
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3567) [hibernate-core-4.3.11.Final.jar:4.3.11.Final]

.....................................................................
.....................................................................
.....................................................................
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: Path expected for join! [from com.betrivius.domain.RoomTipology rt JOIN Room r ON rt.id = r.id_room_tipology_fk WHERE r.id = :roomId]
at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:91) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
.....................................................................
.....................................................................
.....................................................................
为什么?怎么了?我缺少什么?我该如何解决这个问题?

最佳答案

您不需要 JOIN ... ON,因为您已经在映射中定义了关系:

from RoomTipology rt JOIN rt.rooms r WHERE r.id = :roomId

https://docs.oracle.com/javaee/7/tutorial/persistence-querylanguage004.htm#BNBTL

关于java - 为什么我在尝试执行涉及 2 个表之间联接的 HQL 查询时会获取此 "ErrorCounter - Path expected for join!"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40947042/

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