我想实现一个端点,用于搜索受类\u id限制的表:
表格:
@Entity
@Table(name = "class_items")
public class ClassItems implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, updatable = false, nullable = false)
private int id;
@Column(name = "class_id", length = 20)
private Integer classId;
@Column(name = "title", length = 75)
private String title;
}
@PostMapping("/{class_id}/find")
public Page<ClassCategoriesFullDTO> search(@PathVariable("class_id") Integer classId, @Valid ClassCategoriesSearchParams params, Pageable pageable) {
Page<ClassCategoriesFullDTO> list = classItemsRestService.findClassItemsByClassId(classId, params, pageable);
return list;
}
public Page<ClassCategoriesFullDTO> findClassItemsByClassId(Integer classId, ClassCategoriesSearchParams params, Pageable pageable) {
// Limit here queries by classId
Specification<Product> spec = (root, query, cb) -> {
List<Predicate> predicates = new ArrayList<>();
if (params.getTitle() != null) {
predicates.add(cb.equal(root.get("title"), params.getTitle()));
}
return cb.and(predicates.toArray(new Predicate[predicates.size()]));
};
return classItemsService.findAllByClassId(spec, pageable).map(classItemsMapper::toFullDTO);
}
@Service
@Transactional
public class ClassItemsServiceImpl implements ClassItemsService {
@PersistenceContext
private EntityManager entityManager;
private ClassItemsRepository dao;
@Autowired
public ClassItemsServiceImpl(ClassItemsRepository dao) {
this.dao = dao;
}
public Page<ClassItems> findAllByClassId(Specification spec, Pageable pageable) {
return this.dao.findAllByClassId(spec, pageable);
}
}
@Repository
public interface ClassItemsRepository extends JpaRepository<ClassItems, Long>, JpaSpecificationExecutor<ClassItems> {
Page<ClassItems> findAllByClassId(Specification spec, Pageable pageable);
}
我得到错误:
参数值 [org.service.ClassItemsRestServiceImpl$$Lambda$1987/0x0000000801e21440@3c4de5a9] did not match expected type [java.lang.Integer (n/a)]; nested exception is java.lang.IllegalArgumentException: Parameter value [org.service.ClassItemsRestServiceImpl$$Lambda$1987/0x0000000801e21440@3c4de5a9] did not match expected type [java.lang.Integer (n/a)]",
你知道我怎么解决这个问题吗?
1条答案
按热度按时间ibrsph3r1#
出现此错误是因为spring数据试图基于方法名生成查询
findAllByClassId
,因此它需要一个整数作为第一个参数。使用规范时,应该使用
JpaSpecificationExecutor
. 用规范作为参数添加自己的方法是行不通的。如果你想过滤classId
,将适当的筛选器附加到规范本身。编辑解决方案是在构建规范时添加额外条件:
然后,在
ClassItemsServiceImpl
,呼叫dao.findAll(spec)