-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: spring-data with lazy load entities
- Loading branch information
1 parent
3d285ab
commit 5153125
Showing
16 changed files
with
473 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
HELP.md | ||
target/ | ||
!.mvn/wrapper/maven-wrapper.jar | ||
!**/src/main/**/target/ | ||
!**/src/test/**/target/ | ||
|
||
### STS ### | ||
.apt_generated | ||
.classpath | ||
.factorypath | ||
.project | ||
.settings | ||
.springBeans | ||
.sts4-cache | ||
|
||
### IntelliJ IDEA ### | ||
.idea | ||
*.iws | ||
*.iml | ||
*.ipr | ||
|
||
### NetBeans ### | ||
/nbproject/private/ | ||
/nbbuild/ | ||
/dist/ | ||
/nbdist/ | ||
/.nb-gradle/ | ||
build/ | ||
!**/src/main/**/build/ | ||
!**/src/test/**/build/ | ||
|
||
### VS Code ### | ||
.vscode/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>2.4.3</version> | ||
<relativePath/> <!-- lookup parent from repository --> | ||
</parent> | ||
<groupId>com.spring</groupId> | ||
<artifactId>spring-data</artifactId> | ||
<version>1.0.0</version> | ||
<name>spring-data</name> | ||
<description>Demo project for Spring Boot</description> | ||
<properties> | ||
<java.version>11</java.version> | ||
</properties> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-data-jpa</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.h2database</groupId> | ||
<artifactId>h2</artifactId> | ||
<scope>runtime</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.projectlombok</groupId> | ||
<artifactId>lombok</artifactId> | ||
<optional>true</optional> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
<configuration> | ||
<excludes> | ||
<exclude> | ||
<groupId>org.projectlombok</groupId> | ||
<artifactId>lombok</artifactId> | ||
</exclude> | ||
</excludes> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
40 changes: 40 additions & 0 deletions
40
spring-data/src/main/java/com/example/spring/data/LibraryController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.example.spring.data; | ||
|
||
import com.example.spring.data.model.entity.Book; | ||
import com.example.spring.data.model.entity.Person; | ||
import com.example.spring.data.service.LibraryService; | ||
import com.example.spring.data.service.PersonService; | ||
import lombok.AllArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.List; | ||
|
||
@RestController | ||
@AllArgsConstructor | ||
public class LibraryController { | ||
|
||
private final LibraryService libraryService; | ||
private final PersonService personService; | ||
|
||
@GetMapping(value = "/join") | ||
public ResponseEntity<List<Book>> getAll() { | ||
return ResponseEntity.ok(libraryService.getAllByJoinFetch()); | ||
} | ||
|
||
@GetMapping(value = "/limited") | ||
public ResponseEntity<List<Book>> getLimitedFieldsInfo() { | ||
return ResponseEntity.ok(libraryService.getAllByLimitLazyFields()); | ||
} | ||
|
||
@GetMapping(value = "/entity-graph") | ||
public ResponseEntity<List<Person>> getByEntityGraph() { | ||
return ResponseEntity.ok(personService.findPersons()); | ||
} | ||
|
||
@GetMapping(value = "/entity-graph-name") | ||
public ResponseEntity<List<Person>> getByEntityGraphByName() { | ||
return ResponseEntity.ok(personService.findPersonByName()); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
spring-data/src/main/java/com/example/spring/data/SpringDataApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.example.spring.data; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class SpringDataApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(SpringDataApplication.class, args); | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
spring-data/src/main/java/com/example/spring/data/model/LibraryRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.example.spring.data.model; | ||
|
||
import com.example.spring.data.model.entity.Book; | ||
import org.springframework.data.domain.Sort; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
|
||
import java.util.List; | ||
|
||
public interface LibraryRepository extends JpaRepository<Book, Long> { | ||
|
||
@Query(value = "select b from Book b left join fetch b.authors left join fetch b.readers") | ||
List<Book> findAllWithJoinFetch(Sort sort); | ||
|
||
@Query(value = "select new Book(b.id, b.name, b.description) from Book b") | ||
List<Book> findAllWithLimitedFields(); | ||
} |
17 changes: 17 additions & 0 deletions
17
spring-data/src/main/java/com/example/spring/data/model/PersonRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.example.spring.data.model; | ||
|
||
import com.example.spring.data.model.entity.Person; | ||
import org.springframework.data.jpa.repository.EntityGraph; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
|
||
import java.util.List; | ||
|
||
public interface PersonRepository extends JpaRepository<Person, Long> { | ||
|
||
@Query(value = "select new Person(p.id, p.name, p.info, p.permissions) from Person p left join fetch p.permissions") | ||
List<Person> findAll(); | ||
|
||
@EntityGraph(value = "Person.all") | ||
List<Person> findAllByNameLike(String name); | ||
} |
28 changes: 28 additions & 0 deletions
28
spring-data/src/main/java/com/example/spring/data/model/entity/Author.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.example.spring.data.model.entity; | ||
|
||
import lombok.*; | ||
|
||
import javax.persistence.*; | ||
|
||
@Entity | ||
@Builder | ||
@Data | ||
@ToString(exclude = {"book", "person"}) | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class Author { | ||
|
||
@Id | ||
@GeneratedValue | ||
private Long id; | ||
|
||
private String name; | ||
|
||
private String description; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
private Book book; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
private Person person; | ||
} |
42 changes: 42 additions & 0 deletions
42
spring-data/src/main/java/com/example/spring/data/model/entity/Book.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.example.spring.data.model.entity; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
import javax.persistence.*; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
|
||
@Entity | ||
@Data | ||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@NamedEntityGraph(name = "Book.authors", | ||
attributeNodes = @NamedAttributeNode("authors") | ||
) | ||
public class Book { | ||
|
||
@Id | ||
@GeneratedValue | ||
private Long id; | ||
|
||
private String name; | ||
|
||
private String description; | ||
|
||
@OneToMany(mappedBy = "book", cascade = CascadeType.ALL) | ||
private Set<Author> authors = new HashSet<>(); | ||
|
||
@OneToMany(mappedBy = "book", cascade = CascadeType.ALL) | ||
private Set<Reader> readers = new HashSet<>(); | ||
|
||
public Book(Long id, String name, String description) { | ||
this.id = id; | ||
this.name = name; | ||
this.description = description; | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
spring-data/src/main/java/com/example/spring/data/model/entity/Person.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.example.spring.data.model.entity; | ||
|
||
import com.example.spring.data.model.entity.Author; | ||
import com.example.spring.data.model.entity.UserPermission; | ||
import lombok.*; | ||
|
||
import javax.persistence.*; | ||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
@Entity | ||
@Data | ||
@Builder | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@NamedEntityGraphs({ | ||
@NamedEntityGraph( | ||
name = "Person.all", includeAllAttributes = true | ||
) | ||
}) | ||
public class Person { | ||
|
||
@Id | ||
@GeneratedValue | ||
private Long id; | ||
|
||
private String name; | ||
|
||
private String info; | ||
|
||
@OneToMany(mappedBy = "person", cascade = CascadeType.ALL) | ||
private List<UserPermission> permissions = new ArrayList<>(); | ||
|
||
@OneToMany(mappedBy = "person", cascade = CascadeType.ALL) | ||
private Set<Author> authors = new HashSet<>(); | ||
|
||
public Person(Long id, String name, String info, List<UserPermission> permissions) { | ||
this.id = id; | ||
this.name = name; | ||
this.info = info; | ||
this.permissions = permissions; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
spring-data/src/main/java/com/example/spring/data/model/entity/Reader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.example.spring.data.model.entity; | ||
|
||
import lombok.*; | ||
|
||
import javax.persistence.*; | ||
|
||
@Entity | ||
@Builder | ||
@Data | ||
@ToString(exclude = {"book"}) | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class Reader { | ||
|
||
@Id | ||
@GeneratedValue | ||
private Long id; | ||
|
||
private String name; | ||
|
||
private String lastName; | ||
|
||
private String address; | ||
|
||
private String description; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
private Book book; | ||
} |
23 changes: 23 additions & 0 deletions
23
spring-data/src/main/java/com/example/spring/data/model/entity/UserPermission.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.example.spring.data.model.entity; | ||
|
||
import lombok.*; | ||
|
||
import javax.persistence.*; | ||
|
||
@Entity | ||
@Data | ||
@Builder | ||
@ToString(exclude = {"person"}) | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class UserPermission { | ||
|
||
@Id | ||
@GeneratedValue | ||
private Long id; | ||
|
||
private String name; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
private Person person; | ||
} |
Oops, something went wrong.