the Equivalent in Spring Jpa Data is Specification
, and you can use the repository SpecificationExecutor<T>
and Jpa MetaModel to create your Jpa Criteria.
You can find an introduction about Jpa JpaSpecificationExecutor
A quick example :
- Entity
@Entity
public class ClassRoom {
// id and other properties
@ManyToOne
private School school;
private Date creationDate;
private String reference;
// Getters and setters
}
2.Repository:
@Repository
public interface ClassRoomRepository extends JpaSpecificationExecutor<ClassRoom>{
}
2.Service interface:
public interface ClassRoomService {
List<ClassRoom> list(String reference, String schoolName,
Date creationDate)
}
3.Service Implementaion:
import static yourpackage.ClassRoomSpecifications.*;
import static org.springframework.data.jpa.domain.Specifications.*;
@Service
public class ClassRoomServiceImpl implements ClassRoomService {
@Resource
private ClassRoomRepository repository;
@Override
@Transactional(propagation = Propagation.SUPPORTS)
public List<ClassRoom> list(String reference, String schoolName,
Date creationDate) {
Specifications<ClassRoom> spec = null;
Specifications<ClassRoom> tempo = null;
spec = where(findPerSchool(schoolName));
if (reference != null) {
tempo = where(findPerReference(reference));
}
if (creationDate!=null) {
tempo = tempo == null ? where(findPerCreationDate(creationDate):tempo.and(findPerCreationDate(creationDate));
}
spec = tempo == null ? spec : spec.and(tempo);
return repository.findAll(spec);
}
}