Spring Data JPA vs. MyBatis vs. Spring Data JDBC — What Changes When You Update an Order’s Status?
On this page
Canceling an order may change a single value, orders.status. The SQL that reaches the database depends on how the application persists the order. Spring Data JPA applies changes to entities managed by the persistence context. MyBatis runs the UPDATE you write. Spring Data JDBC’s default save() persists the order and its line items as one unit.
Compare the same order model
Suppose orders holds the status and customer ID, and order_items stores each SKU, unit price, and quantity at the time of purchase. The order and its line items are managed together. Customers and products can be shared by many orders and are managed separately. Use this model to compare cancellation and order search for an admin screen. Commerce database design
| Criterion | Spring Data JPA | MyBatis | Spring Data JDBC |
|---|---|---|---|
| Default approach | Map entities and relationships and let the JPA provider generate SQL | Write the SQL and define how results are mapped | The repository generates basic CRUD SQL for the aggregate |
| Change an existing order’s status | Flush changes to a managed entity within a transaction | Run an UPDATE for the status and get the affected row count |
Default save() deletes the line items, updates the order, then reinserts the items |
| Search orders for an admin screen | Use derived queries, JPQL DTO queries, or native SQL | Write joins, aggregates, and dynamic conditions in SQL and map the results to a DTO | Keep simple queries in the repository and use dedicated SQL and a DTO for complex screens |
The table describes the default approach for each tool. JPA persistence documentation · MyBatis mapper documentation · JDBC persistence documentation
What cancellation reveals about writes
With Spring Data JPA, you can load the order in a transaction and call order.cancel(). If the persistence context manages the entity, it detects field changes and writes them to the database on flush. This is called dirty checking. The entity method can enforce which states allow cancellation. Optimistic locking with @Version can prevent concurrent requests from overwriting changes. The columns in the actual UPDATE and the number of reads depend on the mappings and JPA provider settings. JPA transaction documentation
With MyBatis, you can put the transition condition in the SQL. This mapper XML fragment cancels orders that are still awaiting payment. Place it inside the namespace of a mapper registered with MyBatis and bind it to an int cancelPending(...) method that takes the order ID with @Param("orderId"). The example assumes orders.id is the primary key and PENDING means awaiting payment. MyBatis mapper documentation
The diagram shows how to interpret the affected row count from one conditional update.
<update id="cancelPending">
UPDATE orders
SET status = 'CANCELLED'
WHERE id = #{orderId}
AND status = 'PENDING'
</update>
One affected row means the cancellation was applied. Zero means the order is missing or its current status does not allow cancellation. If the API needs to distinguish those cases, it needs a separate read and a rule for choosing the response. To record cancellation history too, use MyBatis-Spring to put the update and history write in the same service transaction. MyBatis-Spring transaction documentation
Spring Data JDBC saves an aggregate with the order as its root and the line items as its children. Data shared by multiple orders, such as a customer, can be referenced by ID or AggregateReference. Even if the status is the only change, default save() deletes the line items, updates the order, then reinserts the items. With many items and frequent transitions, check the write cost and consider a dedicated UPDATE. Design the status condition and version check together when adding custom SQL. JDBC mapping documentation · JDBC persistence documentation
What admin order search reveals about reads
Suppose an admin screen shows the order number, status, customer name, and item count for the 50 most recent orders. With JPA, if a loop accesses lazy-loaded customers and line items after loading the orders, it may trigger extra queries after the initial read. This is the N+1 problem. Query only the values the DTO needs, or use something like @EntityGraph to define the fetch scope for a screen that needs entities and inspect the actual SQL. MyBatis can express the joins and aggregation in the mapper and return the same DTO. Dedicated SQL in Spring Data JDBC also lets the screen avoid loading the entire order aggregate. JPA query documentation · MyBatis mapper documentation · JDBC persistence documentation
How to choose for a project
- Spring Data JPA fits when domain objects change state often and you want entity mapping and dirty checking. Inspect the fetch scope and SQL for key screens.
- MyBatis fits when you have complex searches or aggregations, or need to retain proven legacy SQL. In code review, check the transition conditions and result mappings together.
- Spring Data JDBC fits when parents and children share a lifecycle and you often read and save small aggregates as a whole. Check the write cost of partial changes when an aggregate has many children.
Before choosing, compare one status change and one page of the admin list using the same data. Check whether only one success is recorded when cancellation requests compete, what writes the status change causes to the line items, and how many SQL statements the listing runs.
The write and read behavior was checked against official documentation. The order scenario is illustrative. The example was not run, and actual SQL and performance were not measured.
Discussion