Fix performance issue in order details API
Problem Context
The order details page is making multiple database calls. A developer added an EntityGraph to fetch everything in one query to improve performance.
Expert
300 points
File Changes (2)
src/main/java/com/example/entity/Order.java
MODIFIED
@@ -1 +1 @@
| 1 | 1 | @Entity |
| 2 | 2 | @Table(name = "orders") |
| 3 | +@NamedEntityGraph( |
|
| 4 | + name = "Order.fullDetails", |
|
| 5 | + attributeNodes = { |
|
| 6 | + @NamedAttributeNode("customer"), |
|
| 7 | + @NamedAttributeNode(value = "items", subgraph = "items-subgraph"), |
|
| 8 | + @NamedAttributeNode("payments"), |
|
| 9 | + @NamedAttributeNode("shippingAddress"), |
|
| 10 | + @NamedAttributeNode("auditLogs") |
|
| 11 | + }, |
|
| 12 | + subgraphs = { |
|
| 13 | + @NamedSubgraph(name = "items-subgraph", |
|
| 14 | + attributeNodes = { |
|
| 15 | + @NamedAttributeNode("product"), |
|
| 16 | + @NamedAttributeNode("reviews") |
|
| 17 | + }) |
|
| 18 | + } |
|
| 19 | +) |
|
| 3 | 20 | public class Order { |
| 4 | 21 | @Id |
| 5 | 22 | @GeneratedValue(strategy = GenerationType.IDENTITY) |
| 6 | 23 | private Long id; |
src/main/java/com/example/repository/OrderRepository.java
MODIFIED
@@ -1 +1 @@
| 1 | 1 | public interface OrderRepository extends JpaRepository<Order, Long> { |
| 2 | + |
|
| 3 | + @EntityGraph(value = "Order.fullDetails") |
|
| 4 | + Optional<Order> findById(Long id); |
|
| 5 | + |
|
| 6 | + @EntityGraph(value = "Order.fullDetails") |
|
| 7 | + List<Order> findByCustomerId(Long customerId); |
|
| 8 | + |
|
| 9 | + @EntityGraph(value = "Order.fullDetails") |
|
| 10 | + List<Order> findByStatus(OrderStatus status); |
|
| 2 | 11 | } |
Login Required: You must be registered to submit reviews and receive AI feedback.
Register or
login to start reviewing!
Your Review
Review Tips
- Look for security vulnerabilities (SQL injection, XSS, etc.)
- Check for null pointer exceptions and error handling
- Consider performance implications
- Evaluate code maintainability and readability
- Check for proper resource management
- Look for logic errors or edge cases