JPQL in 从句与枚举

JPQL in clause with enum

我有基于 spring boot 1.2.3 的应用程序。其中一个模型非常具体:包含枚举列表。当我尝试通过 spring 调用时,sql 的 jpa 查询部分消失 [c.categories IN (:category) 被替换为 (. 在 (?))]。我想这可能是图书馆的一些问题,或者我做错了什么。

你知道如何解决这个问题吗?

记录捕获 SQL:

select count(myobject0_.id) as col_0_0_ from myobject myobject0_ cross join myobject0_categories categories1_ where myobject0_.id=categories1_.myobject_id and (. in (?))

错误:

o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 0, SQLState: 07001
java.sql.SQLException: No value specified for parameter 1

来自接口的原始 JPQL:

@Query("from Myobject c where and c.categories IN (:category)")
Page<Myobject> findAll(@Param("category") Set<Category> categories, Pageable pageable);

目标模型包含:

@Column(name = "category")
@Enumerated(EnumType.STRING)
@ElementCollection(fetch = FetchType.LAZY)
@CollectionTable(name = "myobject_categories", joinColumns = @JoinColumn(name = "myobject_id"))
    private Set<Category> categories;

枚举模型:

public enum Category {
CATEGORY(1), CATEGORY2(2);

private final Integer id;

Category(Integer id) { this.id=id; }

public Integer getId() { return id; }
}

正确的解决方案是反转 IN 子句,然后 jpql 将创建子查询。非常丑陋,但它有效。

@Query("from Myobject c where :category in elements(c.categories)")
Page<Myobject> findAll(@Param("category") Set<Category> categories, Pageable pageable);

已记录 SQL:

select count(myobject0_.id) as col_0_0_ from myobject myobject0_ where (myobject0_.updated is not null) and (? in (select categories1_.category from comic_categories categories1_ where myobject0_.id=categories1_.comic_id))

类似这样的内容将是有效的 JPQL,可用于任何兼容的 JPA 2 提供程序

SELECT c FROM Myobject c WHERE :category MEMBER OF c.categories