【问题标题】:spring jpa query SQLGrammarException on join @ManyToManyspring jpa 查询 SQLGrammarException on join @ManyToMany
【发布时间】:2020-06-25 05:13:27
【问题描述】:

我的 Spring Boot 应用程序中有 Way POJO,如下所示:

public class Way {
    @Id
    private Long wayID;
    @ToString.Exclude
    @JsonIgnore
    @ManyToMany(fetch = FetchType.LAZY,cascade=CascadeType.ALL)
    private List<Relation> relations=new ArrayList<>();

现在我想选择所有具有给定值的关联 ID 的方式。 relationIDRelation 的成员 POJO。

所以这是我的查询 @Query("select w from Way w join Relation relations where relations.relationID=?1") List<Way> selectAllWaysByRelationID(Long relationID);

但错误似乎在我的JOIN 部分,它崩溃并说: org.springframework.dao.InvalidDataAccessResourceUsageException:无法准备语句; SQL [select way0_.wayid as wayid1_5_ from way way0_内连接关系relation1_ on where relation1_.relationid=?];嵌套异常是 org.hibernate.exception.SQLGrammarException: could not prepare statement

我该如何解决这个问题?

我也试过这个:

    @Query("select way from Way.relations full join Way.relations rel where rel.relationID=?1")
    List<Way> selectAllWaysByRelationID(Long relationID);

这也是:

    @Query("select way from Way way join Way.relations rel where rel.relationID=?1")
    List<Way> selectAllWaysByRelationID(Long relationID);

【问题讨论】:

    标签: spring spring-boot jpa spring-data-jpa


    【解决方案1】:

    使用JOIN 时,您需要使用Way 的别名和relations 这样的字段,w.relations r 而不是Way.relations

    完整的查询如下:

    @Query("select w from Way w join w.relations r where r.relationID=?1")
    List<Way> selectAllWaysByRelationID(Long relationID);
    

    【讨论】:

      猜你喜欢
      • 2012-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-19
      • 2018-12-12
      • 1970-01-01
      • 2016-07-29
      • 1970-01-01
      相关资源
      最近更新 更多