Stage 4 - CRUD Operations: Create, Read, Update, Delete

CRUD is not just four repository calls; it is the basic data lifecycle wrapped in validation, errors, and transactions. Most business features combine Create, Read, Update, and Delete with validation, authorization, transactions, and domain rules. In Spring Data JPA, repositories provide the low-level CRUD methods, while services decide how those methods are used safely.

CRUD service flow

Create and Read

Create means inserting a new record. In Java code, you usually create an entity object, fill required fields, validate business rules, and call save. Hibernate generates an insert when the object has no database identity yet.

public User createUser(CreateUserRequest request) {
    User user = new User(request.email(), request.name());
    return userRepository.save(user);
}

Read means loading data. Reading by id uses findById. Reading a list can use findAll, but production APIs should normally use pagination. Pageable describes page number, page size, and sorting. Page<T> contains the content and metadata such as total elements and total pages.

public Page<Product> listProducts(Pageable pageable) {
    return productRepository.findAll(pageable);
}

Update

Update should usually start by loading the existing entity. This matters because the database row may not exist, and because the current state may be needed for validation. After loading the entity inside a transaction, change its fields. Hibernate tracks managed entities and flushes changes at commit.

@Transactional
public User renameUser(Long id, String name) {
    User user = userRepository.findById(id)
        .orElseThrow(() -> new EntityNotFoundException("User not found: " + id));
    user.setName(name);
    return user;
}

Calling save after changing a managed entity is often not required, but many teams still do it for explicitness. The important point is to avoid updating by creating a new object with only an id and a few fields unless you understand merge behavior. That approach can accidentally overwrite fields with null.

Delete and service layer

Delete removes data. deleteById is concise, but for business operations it is often better to load first, check rules, and then call delete. For example, deleting a product may be forbidden if it has completed orders. Such rules belong in the service layer, not in the controller.

Controllers should translate HTTP input and output. Services should coordinate use cases. Repositories should persist entities. If CRUD code is placed directly in controllers, error handling, validation, and transactions become scattered.

CRUD scenario Correct service flow Common beginner mistake
Create a user Validate request, create a new entity, set defaults, call save. Trusting request fields directly and forgetting required defaults such as createdAt.
Read one user Call findById, convert empty Optional to a domain error or HTTP 404. Calling .get() and producing a vague runtime exception.
Read product list Accept Pageable, choose sorting, return DTO page. Calling findAll() and loading the whole table for one screen.
Update product price Load existing entity, check rules, change fields inside a transaction. Creating a new partial entity and accidentally overwriting fields with null.
Delete record Load entity, verify deletion is allowed, then delete. Deleting by id without checking ownership, status, or dependent records.

Error handling

EntityNotFoundException or a custom exception makes missing data explicit. In a REST API, the exception can be mapped to HTTP 404 by a @ControllerAdvice. Custom exceptions are often better when the domain language matters, such as UserNotFoundException or ProductNotFoundException.

Errors should not be swallowed. If an update target does not exist, the API should not silently create another row unless the feature explicitly supports upsert. Clear failures make clients easier to debug and prevent hidden data corruption.

Glossary
ORM
Object-Relational Mapping, a technique that maps objects in code to relational database tables.
JDBC
The standard Java API for connecting to databases and executing SQL.
entity
A Java class mapped to a database table and managed by JPA as persistent data.
persistence context
The JPA runtime area that tracks managed entities and synchronizes them with the database.
repository
A data access abstraction that hides database operations behind methods such as save, find, and delete.
JpaRepository
A Spring Data JPA interface that provides common repository operations for an entity.
CRUD
The basic data operations create, read, update, and delete.
DTO
A Data Transfer Object used to carry data between layers or across an API boundary.
projection
A reduced or custom view of data selected from a larger entity or query result.
JPQL
A JPA query language that queries entities and fields instead of raw tables and columns.
derived query
A Spring Data query generated from a repository method name.
lazy loading
Loading related data only when it is first accessed.
eager loading
Loading related data immediately together with the main object.
cascade
A persistence rule that propagates an operation from one entity to related entities.
orphan removal
A JPA rule that deletes a child entity when it is removed from its parent relationship.
transactional
Describes code that runs inside a transaction boundary.
rollback
Cancelling a transaction so its database changes are not committed.
propagation
A transaction setting that defines how a method joins, creates, or rejects a transaction.