Reputation: 1
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] ...
Upvotes: 0
Views: 2131
Reputation: 2933
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_
Upvotes: 0