此代码出了点问题。我尝试创建一个自定义查询,但总是收到相同的错误。
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;
}
这是我班的主题
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 + "]";
}
}
我的资料库
public interface IPhotoRepository extends JpaRepository<Photo, Long>,
JpaSpecificationExecutor<Photo>{
}
我的服务
@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()]));
}
});
}
}
这是错误:
java.lang.IllegalArgumentException:参数值[主题 [themeId = 1]]与预期的类型[java.util.Collection(n / a)]不匹配 在 org.hibernate.query.spi.QueryParameterBindingValidator.validate(QueryParameterBindingValidator.java:54) 〜[hibernate-core-5.4.1.Final.jar:5.4.1.Final] ...
答案 0 :(得分:0)
IN
运算符检查列是否在查询参数提供的值列表中。要检查值是否在集合中,请使用MEMBER OF
查看更多https://www.objectdb.com/java/jpa/query/jpql/collection#Criteria_Query_Collection_Expressions_