Skip to content

Commit

Permalink
Feat: spring-data with lazy load entities
Browse files Browse the repository at this point in the history
  • Loading branch information
ElinaValieva committed Mar 7, 2021
1 parent 3d285ab commit 5153125
Show file tree
Hide file tree
Showing 16 changed files with 473 additions and 0 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@
<module>spring-annotation</module>
<module>spring-testing</module>
<module>spring-cache</module>
<module>spring-data</module>
</modules>
</project>
33 changes: 33 additions & 0 deletions spring-data/.gitignore
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/
63 changes: 63 additions & 0 deletions spring-data/pom.xml
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>
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());
}
}
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);
}

}
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();
}
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);
}
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;
}
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;
}
}
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;
}
}
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;
}
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;
}
Loading

0 comments on commit 5153125

Please sign in to comment.