Skip to content

Commit cd5e4f4

Browse files
Update data-jpa sample with NamedEntityGraph
1 parent 93726ac commit cd5e4f4

File tree

4 files changed

+80
-5
lines changed

4 files changed

+80
-5
lines changed

data-jpa/src/aotSmokeTest/java/com/example/data/jpa/DataJpaApplicationAotTests.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ void insert(AssertableOutput output) {
2525
void listAllAuthors(AssertableOutput output) {
2626
Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> {
2727
assertThat(output).hasSingleLineContaining("listAllAuthors(): author = Author{name='Josh Long'")
28-
.hasSingleLineContaining("Book{title='Cloud Native Java'}")
29-
.hasSingleLineContaining("Book{title='Reactive Spring'}")
28+
.hasSingleLineContaining("Book{title='Cloud Native Java'")
29+
.hasSingleLineContaining("Book{title='Reactive Spring'")
3030
.hasSingleLineContaining("listAllAuthors(): author = Author{name='Martin Kleppmann'}")
31-
.hasSingleLineContaining("Book{title='Designing Data Intensive Applications'}");
31+
.hasSingleLineContaining("Book{title='Designing Data Intensive Applications'");
3232
});
3333
}
3434

@@ -70,4 +70,12 @@ void callback(AssertableOutput output) {
7070
});
7171
}
7272

73+
@Test
74+
void entityGraph(AssertableOutput output) {
75+
Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> {
76+
assertThat(output).hasSingleLineContaining(
77+
"namedEntityGraph: Book{title='Spring in Action', authors=[Author{name='Craig Walls'}]}");
78+
});
79+
}
80+
7381
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.example.data.jpa;
17+
18+
import com.example.data.jpa.model.Book;
19+
import org.springframework.data.jpa.repository.EntityGraph;
20+
import org.springframework.data.jpa.repository.Query;
21+
import org.springframework.data.repository.ListCrudRepository;
22+
23+
public interface BookRepository extends ListCrudRepository<Book, Long> {
24+
25+
@EntityGraph(value = "Book.authors")
26+
@Query("SELECT b FROM Book b WHERE b.title = :title")
27+
Book findByTitleWithNamedGraph(String title);
28+
29+
@EntityGraph(attributePaths = "authors")
30+
@Query("SELECT b FROM Book b WHERE b.title = :title")
31+
Book findByTitleWithAdHocGraph(String title);
32+
33+
}

data-jpa/src/main/java/com/example/data/jpa/CLR.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.example.data.jpa;
22

33
import java.util.Arrays;
4+
import java.util.Collections;
45
import java.util.List;
56
import java.util.Set;
67

@@ -16,8 +17,11 @@ class CLR implements CommandLineRunner {
1617

1718
private final AuthorRepository authorRepository;
1819

19-
CLR(AuthorRepository authorRepository) {
20+
private final BookRepository bookRepository;
21+
22+
CLR(AuthorRepository authorRepository, BookRepository bookRepository) {
2023
this.authorRepository = authorRepository;
24+
this.bookRepository = bookRepository;
2125
}
2226

2327
@Override
@@ -29,6 +33,7 @@ public void run(String... args) {
2933
findByPartialName();
3034
queryFindByName();
3135
deleteAll();
36+
entityGraph();
3237
}
3338

3439
private void deleteAll() {
@@ -61,6 +66,17 @@ private void findById(List<Author> authors) {
6166
System.out.printf("findById(): author2 = %s%n", author2);
6267
}
6368

69+
private void entityGraph() {
70+
71+
Book book = new Book(null, "Spring in Action");
72+
Author author = new Author(null, "Craig Walls", Collections.emptySet());
73+
book.getAuthors().add(author);
74+
bookRepository.save(book);
75+
76+
Book loaded = bookRepository.findByTitleWithNamedGraph("Spring in Action");
77+
System.out.println("namedEntityGraph: " + loaded);
78+
}
79+
6480
private void listAllAuthors() {
6581
List<Author> authors = this.authorRepository.findAll();
6682
for (Author author : authors) {

data-jpa/src/main/java/com/example/data/jpa/model/Book.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
package com.example.data.jpa.model;
22

3+
import java.util.HashSet;
34
import java.util.Objects;
5+
import java.util.Set;
46

7+
import jakarta.persistence.CascadeType;
58
import jakarta.persistence.Entity;
69
import jakarta.persistence.GeneratedValue;
710
import jakarta.persistence.Id;
11+
import jakarta.persistence.NamedAttributeNode;
12+
import jakarta.persistence.NamedEntityGraph;
13+
import jakarta.persistence.OneToMany;
814

915
@Entity
16+
@NamedEntityGraph(name = "Book.authors", attributeNodes = @NamedAttributeNode("authors"))
1017
public class Book {
1118

1219
@Id
@@ -15,6 +22,9 @@ public class Book {
1522

1623
private String title;
1724

25+
@OneToMany(cascade = CascadeType.ALL)
26+
private Set<Author> authors = new HashSet<>();
27+
1828
protected Book() {
1929
}
2030

@@ -35,6 +45,14 @@ public void setTitle(String title) {
3545
this.title = title;
3646
}
3747

48+
public Set<Author> getAuthors() {
49+
return authors;
50+
}
51+
52+
public void setAuthors(Set<Author> authors) {
53+
this.authors = authors;
54+
}
55+
3856
@Override
3957
public boolean equals(Object o) {
4058
if (this == o) {
@@ -54,7 +72,7 @@ public int hashCode() {
5472

5573
@Override
5674
public String toString() {
57-
return "Book{" + "title='" + title + '\'' + '}';
75+
return "Book{" + "title='" + title + '\'' + ", authors=" + authors + '}';
5876
}
5977

6078
}

0 commit comments

Comments
 (0)