简体   繁体   中英

CriteriaBuilder Predicate IN

Something went wrong with this code. I try to create a custom query but I get always the same error.

public class Photo {
    @Id 
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long photoId;
    private String title;


    @ManyToMany
    @JoinTable(name="PHOTO_THEME",
            joinColumns=@JoinColumn(name="PHOTO_FK"),
            inverseJoinColumns=@JoinColumn(name="THEME_FK"),
            uniqueConstraints=@UniqueConstraint(columnNames= {"PHOTO_FK", "THEME_FK"}))
    @JsonManagedReference
    private List<Theme> themes;

}

Here my class Theme

 public class Theme {

        @Id
        @GeneratedValue(strategy=GenerationType.IDENTITY)
        private Long themeId;

        @Lob
        @Column(length=1000000)
        private String description;

        @JsonBackReference
        @ManyToMany(mappedBy="themes")
        private List<Photo> photos;


        @Override
        public String toString() {
            return "Theme [themeId=" + themeId + "]";
        }


    }

My repository

    public interface IPhotoRepository extends JpaRepository<Photo, Long>, 
                      JpaSpecificationExecutor<Photo>{
}

My service

@Service
public class PhotoServiceImpl implements IPhotoService {
    @Autowired
    private IPhotoRepository photoRepository;
    @Autowired
    private ThemeServiceImpl themeService;


    @Override
    public List<Photo> findByCriteria(PhotoFilters filter) {
        return this.photoRepository.findAll(new Specification<Photo>() {

            @Override
            public Predicate toPredicate(Root<Photo> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();


                if (filter.getTheme() != null && filter.getTheme() != "") {

                    Theme themex2 = themeService.findByType("Retro");
                    List<Theme> listThemes = new ArrayList<Theme>();
                    listThemes.add(themex2);

                    In<Theme> predicate = criteriaBuilder.in(root.get("themes"));

                    listThemes.forEach(t -> predicate.value(t));

                    predicates.add(predicate);
                }
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        });
    }

}

And This is the ERROR:

java.lang.IllegalArgumentException: Parameter value [Theme [themeId=1]] did not match expected type [java.util.Collection (n/a)] at org.hibernate.query.spi.QueryParameterBindingValidator.validate(QueryParameterBindingValidator.java:54) ~[hibernate-core-5.4.1.Final.jar:5.4.1.Final] ...

IN operator checks if column is in list of values provided by query parameters. For checking if a value is in collection use MEMBER OF

See more https://www.objectdb.com/java/jpa/query/jpql/collection#Criteria_Query_Collection_Expressions_

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM