简体   繁体   中英

Why I am obtaining this “ErrorCounter - Path expected for join!” trying to perform an HQL query that involves join between 2 tables?

I am working on a Spring project using Spring Data JPA to perform query.

So I have these 2 entity classes:

1) Room representing a room of an accomodation:

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

As you can see this class contains this field:

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

that link many Room instance to a RoomTipology instance.

2) Then I have RoomTipology entity class:

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

Then I have this repository clas for the RoomTipology entity class, something like:

in which I want implement a method that return the room tipology associated to a specific room id. So I want implement an HQL version of this query:

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

So I have donw in this way:

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

}

The problem is that performing my appliction I obtain this error message:

[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]
.....................................................................
.....................................................................
.....................................................................

Why? What is wrong? What am I missing? How can I fix this issue?

You don't need the JOIN ... ON because you have defined the relationship already in the mapping:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM