Add Hibernate 7 batches 2-6, batch 7, and batch 8: mapping styles, JPA annotations, natural IDs, @Immutable, stored procedures, in-memory test databases, JNDI mocking, proxies, associations, temporal mapping, named queries, HQL, Criteria API, EntityManager bootstrapping, Ehcache 3 L2 cache configuration, HikariCP connection pooling, Hibernate Validator CDI integration, aggregate functions, sorting, pagination, interceptors, and Hibernate Search 8 (Hibernate 7.4.5.Final + Spring Boot 4.1.1 + JDK 25)
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
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:
|
||||
*
|
||||
* <ul>
|
||||
* <li>{@code publisher} -- {@code @ManyToOne} with the JPA-default fetch type, EAGER.
|
||||
* Nothing in this class overrides it.</li>
|
||||
* <li>{@code reviews} -- {@code @OneToMany}, explicitly LAZY, and the only attribute named
|
||||
* in {@code Book.reviews-only}. This is the association the fetchgraph/loadgraph demo
|
||||
* turns on and off.</li>
|
||||
* </ul>
|
||||
*
|
||||
* <p>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<ProxyReview> 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<ProxyReview> getReviews() {
|
||||
return reviews;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.ankurm.hibernatedemo.proxy;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
|
||||
/**
|
||||
* The default-fetch-type EAGER side of the entity-graph demo in docs/11-proxies-and-lazy-initialization.md
|
||||
* (fetchgraph vs loadgraph). {@link ProxyBook#publisher} points here with the default
|
||||
* {@code @ManyToOne} fetch type, which is EAGER -- deliberately, so that a
|
||||
* {@code jakarta.persistence.fetchgraph} hint has something to force back to LAZY that a
|
||||
* {@code jakarta.persistence.loadgraph} hint leaves alone.
|
||||
*/
|
||||
@Entity
|
||||
public class ProxyPublisher {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
private String name;
|
||||
|
||||
protected ProxyPublisher() {
|
||||
// JPA
|
||||
}
|
||||
|
||||
public ProxyPublisher(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
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;
|
||||
|
||||
/**
|
||||
* Child side of {@link ProxyBook#reviews}. See {@link ProxyBook} for why this association
|
||||
* exists and how the entity-graph demo in docs/11-proxies-and-lazy-initialization.md uses it.
|
||||
*/
|
||||
@Entity
|
||||
public class ProxyReview {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
private String comment;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "book_id")
|
||||
private ProxyBook book;
|
||||
|
||||
protected ProxyReview() {
|
||||
// JPA
|
||||
}
|
||||
|
||||
public ProxyReview(String comment, ProxyBook book) {
|
||||
this.comment = comment;
|
||||
this.book = book;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getComment() {
|
||||
return comment;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user