Skip to content
This repository was archived by the owner on Feb 23, 2023. It is now read-only.

Commit 942d4dc

Browse files
committed
Add hint to support @entitygraph
- Adjust the sample to use a entity graph Closes gh-1619
1 parent 2db718d commit 942d4dc

File tree

6 files changed

+160
-14
lines changed

6 files changed

+160
-14
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
#!/usr/bin/env bash
22

3-
${PWD%/*samples/*}/scripts/compileWithMaven.sh && ${PWD%/*samples/*}/scripts/test.sh $*
3+
${PWD%/*samples/*}/scripts/compileWithMaven.sh $* && ${PWD%/*samples/*}/scripts/test.sh $*

samples/data-jpa/src/main/java/app/main/SampleApplication.java

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,15 @@
1515
*/
1616
package app.main;
1717

18+
import java.util.Optional;
19+
20+
import app.main.model.Author;
21+
import app.main.model.Book;
22+
import app.main.model.BookRepository;
1823
import app.main.model.Flurb;
1924
import app.main.model.Foo;
2025
import app.main.model.FooRepository;
26+
2127
import org.springframework.boot.CommandLineRunner;
2228
import org.springframework.boot.SpringApplication;
2329
import org.springframework.boot.autoconfigure.SpringBootApplication;
@@ -27,36 +33,56 @@
2733
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
2834
import org.springframework.web.servlet.function.RouterFunction;
2935

30-
import java.util.Optional;
31-
32-
import static org.springframework.web.servlet.function.RequestPredicates.*;
33-
import static org.springframework.web.servlet.function.RouterFunctions.*;
34-
import static org.springframework.web.servlet.function.ServerResponse.*;
36+
import static org.springframework.web.servlet.function.RequestPredicates.GET;
37+
import static org.springframework.web.servlet.function.RouterFunctions.route;
38+
import static org.springframework.web.servlet.function.ServerResponse.ok;
3539

3640
@SpringBootApplication
3741
@EnableJpaAuditing(auditorAwareRef = "fixedAuditor")
3842
@EnableJpaRepositories(basePackageClasses = FooRepository.class)
3943
public class SampleApplication {
4044

41-
private final FooRepository entities;
45+
private final FooRepository fooRepository;
46+
47+
private final BookRepository bookRepository;
4248

43-
public SampleApplication(FooRepository entities) {
44-
this.entities = entities;
49+
public SampleApplication(FooRepository fooRepository, BookRepository bookRepository) {
50+
this.fooRepository = fooRepository;
51+
this.bookRepository = bookRepository;
4552
}
4653

4754
@Bean
4855
public CommandLineRunner runner() {
4956
return args -> {
5057

51-
Optional<Foo> maybeFoo = entities.findById(1L);
58+
Optional<Foo> maybeFoo = fooRepository.findById(1L);
5259
Foo foo;
53-
foo = maybeFoo.orElseGet(() -> entities.save(new Foo("Hello")));
60+
foo = maybeFoo.orElseGet(() -> fooRepository.save(new Foo("Hello")));
5461
Flurb flurb = new Flurb();
5562
flurb.setVal("Balla balla");
5663
foo.setFlurb(flurb);
57-
entities.save(foo);
64+
fooRepository.save(foo);
65+
66+
fooRepository.findWithBetween("a", "X");
67+
};
68+
}
69+
70+
@Bean
71+
public CommandLineRunner runner2() {
72+
return args -> {
73+
Book book = new Book();
74+
book.setTitle("Spring in Action");
75+
Author author = new Author();
76+
author.setName("Craig Walls");
77+
book.getAuthors().add(author);
78+
bookRepository.save(book);
79+
80+
// Book loaded = bookRepository.findByTitleWithNamedGraph("Spring in Action");
81+
// System.out.println("Loaded via named EntityGraph: " + loaded);
82+
83+
Book loaded = bookRepository.findByTitleWithAdHocGraph("Spring in Action");
84+
System.out.println("Loaded via ad-hoc EntityGraph: " + loaded);
5885

59-
entities.findWithBetween("a", "X");
6086
};
6187
}
6288

@@ -71,7 +97,7 @@ public RouterFunction<?> userEndpoints() {
7197
}
7298

7399
private Foo findOne() {
74-
return entities.findById(1L).get();
100+
return fooRepository.findById(1L).get();
75101
}
76102

77103
public static void main(String[] args) {
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package app.main.model;
2+
3+
import javax.persistence.Column;
4+
import javax.persistence.Entity;
5+
import javax.persistence.GeneratedValue;
6+
import javax.persistence.Id;
7+
8+
/**
9+
* @author Moritz Halbritter
10+
*/
11+
@Entity
12+
public class Author {
13+
@Id
14+
@GeneratedValue
15+
private Long id;
16+
17+
@Column
18+
private String name;
19+
20+
public Long getId() {
21+
return id;
22+
}
23+
24+
public String getName() {
25+
return name;
26+
}
27+
28+
public void setName(String name) {
29+
this.name = name;
30+
}
31+
32+
@Override
33+
public String toString() {
34+
return "Author{" +
35+
"id=" + id +
36+
", name='" + name + '\'' +
37+
'}';
38+
}
39+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package app.main.model;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
import javax.persistence.CascadeType;
7+
import javax.persistence.Column;
8+
import javax.persistence.Entity;
9+
import javax.persistence.GeneratedValue;
10+
import javax.persistence.Id;
11+
import javax.persistence.OneToMany;
12+
13+
/**
14+
* @author Moritz Halbritter
15+
*/
16+
@Entity
17+
//@NamedEntityGraph(name = "Book.authors",
18+
// attributeNodes = @NamedAttributeNode("authors")
19+
//)
20+
public class Book {
21+
@Id
22+
@GeneratedValue
23+
private Long id;
24+
25+
@Column
26+
private String title;
27+
28+
@OneToMany(cascade = CascadeType.ALL)
29+
private Set<Author> authors = new HashSet<>();
30+
31+
public Long getId() {
32+
return id;
33+
}
34+
35+
public String getTitle() {
36+
return title;
37+
}
38+
39+
public void setTitle(String title) {
40+
this.title = title;
41+
}
42+
43+
public Set<Author> getAuthors() {
44+
return authors;
45+
}
46+
47+
@Override
48+
public String toString() {
49+
return "Book{" +
50+
"id=" + id +
51+
", title='" + title + '\'' +
52+
", authors=" + authors +
53+
'}';
54+
}
55+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package app.main.model;
2+
3+
import org.springframework.data.jpa.repository.EntityGraph;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
import org.springframework.data.jpa.repository.Query;
6+
7+
/**
8+
* @author Moritz Halbritter
9+
*/
10+
public interface BookRepository extends JpaRepository<Book, Long> {
11+
// @EntityGraph(value = "Book.authors")
12+
// @Query("SELECT b FROM Book b WHERE b.title = :title")
13+
// Book findByTitleWithNamedGraph(String title);
14+
15+
@EntityGraph(attributePaths = "authors")
16+
@Query("SELECT b FROM Book b WHERE b.title = :title")
17+
Book findByTitleWithAdHocGraph(String title);
18+
}

spring-native-configuration/src/main/java/org/springframework/data/jpa/repository/config/DataJpaHints.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@
1515
*/
1616
package org.springframework.data.jpa.repository.config;
1717

18+
import javax.persistence.NamedEntityGraph;
19+
1820
import org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect;
1921
import org.springframework.context.annotation.Import;
2022
import org.springframework.data.DataAuditingHints;
2123
import org.springframework.data.DataNonReactiveAuditingHints;
2224
import org.springframework.data.jpa.domain.support.AuditingBeanFactoryPostProcessor;
2325
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
26+
import org.springframework.data.jpa.repository.EntityGraph;
2427
import org.springframework.data.jpa.repository.support.JpaRepositoryFactory;
2528
import org.springframework.nativex.hint.JdkProxyHint;
2629
import org.springframework.nativex.hint.NativeHint;
@@ -49,6 +52,11 @@
4952
}
5053

5154
)
55+
@NativeHint(
56+
// https://github.com/spring-projects-experimental/spring-native/issues/1619
57+
trigger = EntityGraph.class,
58+
types = @TypeHint(types = NamedEntityGraph.class, access = {})
59+
)
5260
public class DataJpaHints implements NativeConfiguration {
5361

5462
}

0 commit comments

Comments
 (0)