[英]Load single entity without relations jpa
我有兩個實體的學生和成績。 它們之間是一對多的關系。 但是,當我撥打 api 電話以獲取學生時,我會與他們一起獲得所有成績。 有沒有辦法只加載學生實體? 我試過 FetchType.LAZY 但它沒有用。
學生 model:
@Entity
@Table
public class Student {
@Id
@GeneratedValue(
strategy= GenerationType.AUTO,
generator="native"
)
@GenericGenerator(
name = "native",
strategy = "native"
)
private Long id;
@NotBlank(message = "Name cannot be null")
private String name;
@NotBlank(message = "Lastname cannot be null")
private String lastname;
@NotNull(message = "Age cannot be null")
private int age;
@NotBlank(message = "Email cannot be null")
private String email;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "student")
private Set<Grade> grades = new HashSet();
}
牌號 model:
@Entity
@Table
public class Grade {
@Id
@GeneratedValue(
strategy= GenerationType.AUTO,
generator="native"
)
@GenericGenerator(
name = "native",
strategy = "native"
)
private Long id;
private String subject;
private double value;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "student_id", nullable = false)
private Student student;
學生服務:
@Service
public class StudentService {
private final IStudentRepository studentRepository;
public StudentService(IStudentRepository studentRepository){
this.studentRepository = studentRepository;
}
public List<Student> GetAll(){
return studentRepository.findAll();
}
Hibernate output:
Hibernate: select student0_.id as id1_1_, student0_.age as age2_1_, student0_.email as email3_1_, student0_.lastname as lastname4_1_, student0_.name as name5_1_ from student student0_
Hibernate: select grades0_.student_id as student_4_0_0_, grades0_.id as id1_0_0_, grades0_.id as id1_0_1_, grades0_.student_id as student_4_0_1_, grades0_.subject as subject2_0_1_, grades0_.value as value3_0_1_ from grade grades0_ where grades0_.student_id=?
要僅加載學生實體,您可以創建一個單獨的投影,如StudenDTO
並使用它來傳遞 repo-service-controller。
投影的相關部分在這里
interface StudentDTO {
String getName();
String getLastname();
Integer getAge();
String getEmail();
}
現在當你點擊localhost:8080/students/create-dto-return?id=1
您不會看到下一個查詢被觸發,這是在序列化期間觸發的 jackson。
整個代碼如下:
package com.example.demo;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.HashSet;
import java.util.Set;
@RestController
@RequestMapping("/students")
public class StudentsController {
private final StudentService studentService;
@Autowired
public StudentsController(StudentService studentService) {
this.studentService = studentService;
}
@GetMapping
public Iterable<Student> list() {
return studentService.list();
}
@PostMapping
public Student createEntityReturn(@RequestBody Student student) {
return studentService.save(student);
}
@GetMapping(value = "/create-dto-return")
public StudentDTO getByDto(@RequestParam("id") Integer id) {
return studentService.findStudentOnlyByIdDto(id);
}
@GetMapping(value = "/create-entity-return")
public Student getById(@RequestParam("id") Integer id) {
return studentService.findStudentById(id);
}
}
@Getter
@Setter
@ToString
@Entity
class Student {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@NotBlank(message = "Name cannot be null")
private String name;
@NotBlank(message = "Lastname cannot be null")
private String lastname;
@NotNull(message = "Age cannot be null")
private int age;
@NotBlank(message = "Email cannot be null")
private String email;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "student")
private Set<Grade> grades = new HashSet<>();
}
@Getter
@Setter
@Entity
@Table
class Grade {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String subject;
private double value;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "student_id", nullable = false)
private Student student;
}
@Service
class StudentService {
private final StudentRepository studentRepository;
@Autowired
StudentService(StudentRepository studentRepository) {
this.studentRepository = studentRepository;
}
@Transactional
public Student save(Student student) {
return studentRepository.save(student);
}
@Transactional(readOnly = true)
public Iterable<Student> list() {
return studentRepository.findAll();
}
@Transactional(readOnly = true)
public StudentDTO findStudentOnlyByIdDto(Integer id) {
return studentRepository.findStudentById(id);
}
@Transactional(readOnly = true)
public Student findStudentById(Integer id) {
return studentRepository.findById(id).orElseThrow(() -> new RuntimeException("Unable to find student"));
}
}
interface StudentDTO {
String getName();
String getLastname();
Integer getAge();
String getEmail();
}
@Repository
interface StudentRepository extends JpaRepository<Student, Integer> {
StudentDTO findStudentById(Integer id);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.