gpt4 book ai didi

java - Spring JPA 多对一 - 与 RestController 的一对多关系

转载 作者:行者123 更新时间:2023-12-01 16:52:50 26 4
gpt4 key购买 nike

我正在尝试使用具有外键数据的 Rest Controller 从 Postgresql 数据库检索所有多对一关系数据。喜欢类(class)和讲师:

{
{
"id": 1,
"course_name": "IT 101",
"instructor":{
"instructor_name":"Jack King"
}
},
{
"id": 2,
"course_name": "CS 101",
"instructor":{
"instructor_name":"Homer Love"
}
},
{
"id": 3,
"course_name": "DB 101",
"instructor":{
"instructor_name":"Jack King"
}
}
}

我的代码如下:

数据库

create table instructor
(
id integer generated always as identity,
instructor_name text,

primary key (instructor_name),
unique (id)
);

create table course
(
id integer generated always as identity,
course_name text not null,
instructor_fk integer references instructor (id),

primary key (course_name, instructor_fk),
unique (id)
);
insert into instructor (instructor_name) values ('Jack King');
insert into instructor (instructor_name) values ('Homer Love');

insert into author (course_name, instructor_fk) values ('IT 101', 1, 1);
insert into author (course_name, instructor_fk) values ('CS 101', 2, 2);
insert into author (course_name, instructor_fk) values ('DB 101', 3, 1);

实体

  • 我正在使用Project Lombok使用注释代替构造函数,使用 getter 和 setter 来减少样板代码。

类(class)

import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;

@Entity
@Table(name = "course")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Author {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String course_name;

@ManyToOne(cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH} )
@JsonIgnore
@JoinColumn(name = "instructor_fk", nullable = false)
private Instructor instructor;
}

讲师

@Entity
@Table(name = "instructor")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Instructor {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(unique = true)
private String instructor_name;

@OneToMany(
mappedBy = "instructor",
cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH}
)
private List<Course> courses;
}

存储库

@Repository
public interface CourseRepository extends CrudRepository<Author, Integer> {
}

服务

@Service
public class CourseService {
@Autowired
private CourseRepository courseRepository;

public List<Author> getAllCourses() {
return (List<Author>) courseRepository.findAll();
}
}

Controller

@RestController
public class CourseController {
@Autowired
private CourseService courseService;

@GetMapping("/courses")
private List<Author> getAllCourses() {
return courseService.getAllCourses();
}
}

我得到什么:

{
{
"id": 1,
"course_name": "IT 101"
},
{
"id": 2,
"course_name": "CS 101"
},
{
"id": 3,
"course_name": "DB 101"
}
}

最佳答案

@JsonIgnore 注释应该位于一对多关系上,这意味着它应该位于教师实体上,而不是类(class)实体上:

类(class)

import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;

@Entity
@Table(name = "course")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Author {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String course_name;

@ManyToOne(cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH} )
@JoinColumn(name = "instructor_fk", nullable = false)
private Instructor instructor;
}

讲师

@Entity
@Table(name = "instructor")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Instructor {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(unique = true)
private String instructor_name;

@OneToMany(
mappedBy = "instructor",
cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH}
)
@JsonIgnore
private List<Course> courses;
}

关于java - Spring JPA 多对一 - 与 RestController 的一对多关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61656299/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com