hql查询无法正常工作

pexxcrt2  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(283)

我对hql查询有一些问题:

@Query("Select s from Sport s JOIN s.categories as c where s.sportId = :sportId and c.categoryId = :categoryId")
Iterable<Sport> findByCategoryIdAndSportId(@Param("sportId")Long sportId, @Param("categoryId")Long categoryId);

此查询返回所有类别(似乎忽略了第二个and子句),但只返回与sportid匹配的运动。
以下是我代表运动和类别的课程:

@Entity
@Table(name = "Sports")
public class Sport  implements Serializable
{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long sportId;
    @OneToMany(mappedBy = "sportId")
    @ApiModelProperty(required = true)
    @JsonManagedReference
    private Set<Category> categories;

    @Column
    private String sportName;

    ....get and set

@Entity
@Table(name = "Categories")
public class Category implements Serializable
{
    @ManyToOne
    @JoinColumn(name = "sportId", nullable = false)
    @JsonBackReference
    private Sport sportId;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long categoryId;

    ..get and set

编辑
假设:

Sport:
  |sportId | SportName  
  |1       |Rugby      
  |2       |Basket       
  |3       |Tennis

和类别:

|categoryId | CategoryName | sportId|
  |1          |Italy         |1       |
  |2          |England       |2       | 
  |3          |USA           |1       |
  |1          |Spain         |2       |

我想通过查询获得(假设sportid=1,categoryid=1):

| sportId | SportName | categoryId | CategoryName |
   1         Rugby       1             Italy

有人能帮我吗?提前谢谢

a1o7rhls

a1o7rhls1#

那不行。看看这个。
您可以反转查询以获取具有某些特定运动的类别:

Select c from Categories c JOIN Sport s where c.categoryId = :categoryId and s.sportId = :sportId

相关问题