Overview
The Library API uses JPA entities to model the core domain concepts: books, authors, users, and rental transactions. These entities are mapped to database tables with relationships defined using JPA annotations.Entity Relationships
Author Entity
Defined inAuthorEntity.java:19-29, represents book authors:
Fields
Auto-generated sequence ID serving as the primary key
Author’s full name
Author’s age
Book Entity
Defined inBookEntity.java:24-43, represents books in the library:
Fields
International Standard Book Number, serving as the primary key
Book title
Many-to-one relationship with Author. Cascade operations are enabled, so saving a book can create/update its author
One-to-many relationship with Rental records. Uses
orphanRemoval = true to automatically delete rental records when removed from this collectionNumber of available copies. Decremented when books are rented and incremented when returned
URL or path to the book’s cover image (optional)
Cascade Behavior
The
@ManyToOne(cascade = CascadeType.ALL) on the author relationship means that when you save or update a book, the associated author will also be persisted automatically.User Entity
Defined inUserEntity.java:23-48, represents registered users:
Fields
Auto-generated sequence ID serving as the primary key
User’s first name
User’s last name
Unique email address used as the username for authentication. Marked as
unique = true and nullable = falseOne-to-many relationship with Rental records tracking all books this user has borrowed
BCrypt-hashed password for local authentication (optional if using OAuth)
Google OAuth identifier for social authentication (optional)
URL to user’s profile picture (optional)
The email field is the unique identifier for authentication. The
UserPrincipal.java:24-26 implementation uses email as the username.Rental Entity
Defined inRentalEntity.java:23-43, tracks book loans:
Fields
Auto-generated sequence ID serving as the primary key
Many-to-one relationship identifying who borrowed the book
Many-to-one relationship identifying which book was borrowed
Timestamp when the book was borrowed (stored in UTC as seen in
RentalController.java:78)Expected or actual return date. Set to 2 weeks after loan date by default (
RentalController.java:80)Flag indicating whether the book has been returned. Updated by the return endpoint in
RentalController.java:94Rental Logic
When a book is rented (RentalController.java:59-85):
- Validates that both book and user exist
- Checks book stock availability
- Decrements book stock by 1
- Creates rental record with loan date and return date (2 weeks)
- Returns HTTP 201 (CREATED) with the rental details
RentalController.java:88-106):
- Marks the rental as returned
- Increments book stock by 1
- Returns HTTP 200 (OK) with updated rental details
Borrowed Book Entity
Defined inBorrowedBookEntity.java:23-40, provides an alternative representation of borrowed books:
Fields
Auto-generated identity ID serving as the primary key
Many-to-one relationship identifying who borrowed the book
Many-to-one relationship identifying which book was borrowed
Date when the book was borrowed (date only, no time component)
Expected or actual return date (date only, no time component)
This entity differs from
RentalEntity by using LocalDate instead of ZonedDateTime and GenerationType.IDENTITY instead of SEQUENCE. The choice between these entities depends on whether you need timezone information and which ID generation strategy is preferred.Key Design Patterns
Lombok Annotations
All entities use Lombok annotations for boilerplate reduction:@Data- Generates getters, setters, toString, equals, and hashCode@AllArgsConstructor- Generates constructor with all fields@NoArgsConstructor- Generates no-argument constructor (required by JPA)@Builder- Implements builder pattern for object construction
Primary Key Strategies
Sequence Generation
Used by Author, User, and Rental entities. Generates IDs using database sequences for better performance with batch inserts.
Natural Key
Book entity uses ISBN as a natural primary key since ISBNs are unique and meaningful identifiers.
Identity Generation
BorrowedBook entity uses database identity columns for auto-incrementing IDs.
Bidirectional Relationships
Several bidirectional relationships exist in the model:- Book ↔ Rental: Book has
Set<RentalEntity> rentals, Rental hasBookEntity book - User ↔ Rental: User has
Set<RentalEntity> borrowedBooks, Rental hasUserEntity user