package com.ankurm.hibernatedemo.model; import jakarta.persistence.CascadeType; import jakarta.persistence.Entity; import jakarta.persistence.FetchType; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; import jakarta.persistence.OneToMany; import jakarta.persistence.Version; import java.util.ArrayList; import java.util.List; /** * The entity used by {@code get-vs-load} and {@code merge-vs-refresh}. * *

Docs: docs/01-get-vs-load.md, docs/02-merge-vs-refresh.md. * *

Carries a {@code @Version} column on purpose — the merge/refresh tests need a real * optimistic-lock field to show what merge() does when the version it is holding is stale, not * just what it does to a plain column. The {@code status} field exists specifically for * {@code MergeRefreshTest#refreshDiscardsUnflushedEditSilently}, framed as the * "USER_EDIT" vs "ADMIN_EDIT" scenario in docs/02-merge-vs-refresh.md. The * {@code notes} collection is LAZY and cascades MERGE only — it exists solely for * {@code MergeRefreshTest#mergeDoesNotRequireLazyCollectionToBeInitialized}, which shows that an * unfetched collection is never navigated during merge(). */ @Entity public class Book { @Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "book_seq") private Long id; private String title; private String author; private String status; @Version private Long version; @OneToMany(mappedBy = "book", cascade = CascadeType.MERGE, fetch = FetchType.LAZY) private List notes = new ArrayList<>(); protected Book() { // JPA } public Book(String title, String author) { this.title = title; this.author = author; } public Long getId() { return id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getStatus() { return status; } public void setStatus(String status) { this.status = status; } public Long getVersion() { return version; } public List getNotes() { return notes; } @Override public String toString() { return "Book{id=%s, title=%s, author=%s, status=%s, version=%s}" .formatted(id, title, author, status, version); } }