为什么我得到这个“ErrorCounter - 期望加入的路径!”尝试执行涉及两个表之间的连接的HQL查询?

时间:2016-12-03 11:03:24

标签: java spring hibernate hql spring-data-jpa

我正在使用 Spring Data JPA 来执行Spring项目来执行查询。

所以我有这两个实体类:

1)代表住宿房间的房间:

@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实体类创建了这个存储库clas,类似于:

我想要实现一个方法,该方法返回与特定房间ID相关联的房间tipology 。所以我希望实现此查询的 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]
.....................................................................
.....................................................................
.....................................................................

为什么呢?怎么了?我错过了什么?我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

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

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

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