Search code examples
hibernatespring-bootjpaspring-data-jpajpql

JPQL in clause with enum


I have application based on spring boot 1.2.3. One of the model is very specific: contains list of enums. When I try to call by spring jpa query part of sql disappear [c.categories IN (:category) is replaced with (. in (?))]. I guess it can be some issue in library or maybe I made something wrong.

Do you have any idea how to solve this?

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 (?))

Error:

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

Original JPQL from interface:

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

Target model contains:

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

Enum model:

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

private final Integer id;

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

public Integer getId() { return id; }
}

Solution

  • Something like this would be valid JPQL, available for use in any compliant JPA 2 provider

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