我有基于spring boot 1.2.3的应用程序。其中一个模型非常具体:包含枚举列表。当我尝试通过spring jpa调用时,查询部分sql消失[ c.categories IN(:category)被替换为(。in(?))]。我想这可能是库中的一些问题,也可能是我做错了。
你知道如何解决这个问题吗?
Log catch 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;
Enum模型:
public enum Category {
CATEGORY(1), CATEGORY2(2);
private final Integer id;
Category(Integer id) { this.id=id; }
public Integer getId() { return id; }
}
答案 0 :(得分:2)
这样的东西是有效的JPQL,可用于任何兼容的JPA 2提供程序
SELECT c FROM Myobject c WHERE :category MEMBER OF c.categories
答案 1 :(得分:0)
正确的解决方案是反转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))