Q Class Usage (Specification Support)
Overview
ElastiCORE automatically generates Q classes based on entity definitions. Q classes are type-safe builders that allow you to define JPA Specification conditions based on fields.
Basic Usage
Specification<Entity> spec = Q.Entity.name(Op.LIKE, "test")
.and(Q.Entity.createdAt(Op.GTE, startDate, true));
Supported Operators (Op)
| Operator | Description | Example |
|---|---|---|
Op.EQUAL | Exact match | Q.User.status(Op.EQUAL, "ACTIVE") |
Op.LIKE | Partial match | Q.User.name(Op.LIKE, "John") |
Op.IN | Multiple values | Q.User.role(Op.IN, roleList) |
Op.GT | Greater than | Q.User.age(Op.GT, 20) |
Op.GTE | Greater than or equal | Q.User.age(Op.GTE, 18) |
Op.LT | Less than | Q.User.age(Op.LT, 65) |
Op.LTE | Less than or equal | Q.User.age(Op.LTE, 60) |
Condition Combination
AND Combination
Specification<User> spec = Q.User.name(Op.LIKE, "John Doe")
.and(Q.User.status(Op.EQUAL, "ACTIVE"))
.and(Q.User.age(Op.GTE, 20));
OR Combination
Specification<User> spec = Q.User.name(Op.LIKE, "John")
.or(Q.User.email(Op.LIKE, "john"));
Practical Usage Example
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
public Page<User> searchUsers(UserSearchDTO searchDTO) {
Specification<User> spec = Specification.where(null);
if (searchDTO.getName() != null) {
spec = spec.and(Q.User.name(Op.LIKE, searchDTO.getName()));
}
if (searchDTO.getStatus() != null) {
spec = spec.and(Q.User.status(Op.EQUAL, searchDTO.getStatus()));
}
return userRepository.findAll(spec, searchDTO.getPageable());
}
}
Q Class Generation Location
Q classes are generated according to the namespace settings in env.yml:
q: io.domain.q
Generation command:
./gradlew elcore