Challenge - 5 Problems
Specification Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
How does Specification combine multiple conditions?
Given two Specification objects combined with and(), what is the resulting behavior when querying the database?
Spring Boot
Specification<User> spec1 = (root, query, cb) -> cb.equal(root.get("status"), "ACTIVE"); Specification<User> spec2 = (root, query, cb) -> cb.greaterThan(root.get("age"), 18); Specification<User> combined = spec1.and(spec2); List<User> results = userRepository.findAll(combined);
Attempts:
2 left
💡 Hint
Think about how the
and() method combines conditions logically.✗ Incorrect
The and() method combines two specifications so that both conditions must be true for a record to be included. Here, only users who are ACTIVE and older than 18 are returned.
📝 Syntax
intermediate2:00remaining
Identify the syntax error in this Specification
Which option contains a syntax error in the Specification lambda expression?
Spring Boot
Specification<Product> spec = (root, query, cb) -> cb.like(root.get("name"), "%phone%");
Attempts:
2 left
💡 Hint
Check for missing or misplaced characters in the lambda expression.
✗ Incorrect
All options are identical and syntactically correct. This is a trick question to confirm understanding that the given code is valid.
🔧 Debug
advanced2:00remaining
Why does this Specification cause a runtime error?
Consider this Specification code snippet. What causes the runtime error?
Spring Boot
Specification<Order> spec = (root, query, cb) -> cb.equal(root.get("nonExistentField"), "value"); List<Order> orders = orderRepository.findAll(spec);
Attempts:
2 left
💡 Hint
Check if the field used in the query matches the entity's fields.
✗ Incorrect
Accessing a field that does not exist in the entity causes a runtime error because the query cannot be constructed properly.
❓ state_output
advanced2:00remaining
What is the result count of this dynamic Specification query?
Given this Specification that filters users by optional parameters, what is the count of users returned if both parameters are null?
Spring Boot
Specification<User> spec = Specification.where(null); if (name != null) { spec = spec.and((root, query, cb) -> cb.like(root.get("name"), "%" + name + "%")); } if (email != null) { spec = spec.and((root, query, cb) -> cb.equal(root.get("email"), email)); } List<User> users = userRepository.findAll(spec);
Attempts:
2 left
💡 Hint
Think about what happens when Specification.where(null) is used with no further conditions.
✗ Incorrect
Starting with Specification.where(null) means no filtering. If no conditions are added, the query returns all records.
🧠 Conceptual
expert2:00remaining
Why use Specification pattern for dynamic queries in Spring Data JPA?
Which is the best explanation for using the Specification pattern for dynamic queries?
Attempts:
2 left
💡 Hint
Consider how dynamic conditions can be combined in a flexible way.
✗ Incorrect
The Specification pattern enables composing query conditions dynamically and reusing them, which is ideal for complex and flexible filtering.