package com.ankurm.hibernatedemo.proxy; import jakarta.persistence.Entity; import jakarta.persistence.FetchType; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; import jakarta.persistence.JoinColumn; import jakarta.persistence.ManyToOne; import jakarta.persistence.NamedAttributeNode; import jakarta.persistence.NamedEntityGraph; import jakarta.persistence.OneToMany; import java.util.ArrayList; import java.util.List; /** * Backs docs/11-proxies-and-lazy-initialization.md (post 4870 rewrite). Two associations on purpose: * * * *

With a {@code jakarta.persistence.loadgraph} hint carrying {@code Book.reviews-only}, * {@code reviews} gets join-fetched AND {@code publisher} still gets its default EAGER join -- * loadgraph only promotes attributes, it never demotes ones the mapping already marks EAGER. * With a {@code jakarta.persistence.fetchgraph} hint carrying the same named graph, * {@code reviews} still gets join-fetched, but {@code publisher} is forced down to LAZY (a * proxy, no join) even though the mapping says EAGER -- fetchgraph treats the graph as the * complete fetch plan, not an addition to the mapping's own defaults. */ @Entity @NamedEntityGraph(name = "Book.reviews-only", attributeNodes = @NamedAttributeNode("reviews")) public class ProxyBook { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String title; @ManyToOne @JoinColumn(name = "publisher_id") private ProxyPublisher publisher; @OneToMany(mappedBy = "book", fetch = FetchType.LAZY) private List reviews = new ArrayList<>(); protected ProxyBook() { // JPA } public ProxyBook(String title, ProxyPublisher publisher) { this.title = title; this.publisher = publisher; } public Long getId() { return id; } public String getTitle() { return title; } public ProxyPublisher getPublisher() { return publisher; } public List getReviews() { return reviews; } }