第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 JPA 關(guān)系實體創(chuàng)建 RestAPI

如何使用 JPA 關(guān)系實體創(chuàng)建 RestAPI

白衣非少年 2021-10-13 13:38:01
我有個問題。當(dāng)我有其他實體時,我不知道如何創(chuàng)建 API。我與 Postman 合作,當(dāng)我請求查看數(shù)據(jù)庫中的所有項目時,我也想接收實體。例如,這是我的實體:@Entity@Table(name = "project")public class Project {    @Id    @GeneratedValue(strategy = GenerationType.IDENTITY)    @Column(name = "proj_id")    private int projectId;    @Column(name = "project_name")    private String projectName;    @Column(name = "dg_number")    private int dgNumber;    @ManyToMany    @JoinTable(name = "project_gate_relation", joinColumns = @JoinColumn(name = "proj_id"), inverseJoinColumns = @JoinColumn(name = "gate_id"))    @JsonBackReference    private  List<Gate> gates;    @ManyToMany    @JoinTable(name = "project_threshold_relation", joinColumns = @JoinColumn(name = "proj_id"), inverseJoinColumns = @JoinColumn(name = "thresholdgates_id"))    @JsonBackReference    private  List<Threshold> thresholds;這是門實體@Entity@Table(name = "gate")public class Gate {    @Id    @GeneratedValue(strategy = GenerationType.IDENTITY)    @Column(name = "gate_id")    private int gateId;    @Column(name = "gate_type")    private String gateType;    @Column(name = "gate_value")    private float value;閾值實體@Entity@Table(name = "threshold")public class Threshold {    @Id    @GeneratedValue(strategy = GenerationType.IDENTITY)    @Column(name = "threshold_id")    private int thresholdId;    @Column(name = "threshold_value")    private int thresholdValue;    @Column(name = "threshold_type")    private String thresholdType;控制器@RestController@RequestMapping(ProjectController.PROJECT_URL)public class ProjectController {    public static final String PROJECT_URL = "/cidashboard/projects";    @Autowired    private final ProjectService projectService;    public ProjectController(ProjectService projectService) {        this.projectService = projectService;    }    @GetMapping    public List<Project> getAllProjects(){        return projectService.findAllProjects();    }    @GetMapping("/{id}")    public Project getProjectById(@PathVariable int id) {        return projectService.findProjectById(id);    }
查看完整描述

2 回答

?
阿波羅的戰(zhàn)車

TA貢獻(xiàn)1862條經(jīng)驗 獲得超6個贊

JPA 中默認(rèn)不加載相關(guān)實體。你必須在@ManyToMany 關(guān)系中定義fetch = FetchType.EAGER


@ManyToMany(fetch = FetchType.EAGER)

@JoinTable(name = "project_gate_relation", joinColumns = @JoinColumn(name = "proj_id"), inverseJoinColumns = @JoinColumn(name = "gate_id"))

@JsonBackReference

private  List<Gate> gates;


@ManyToMany(fetch = FetchType.EAGER)

@JoinTable(name = "project_threshold_relation", joinColumns = @JoinColumn(name = "proj_id"), inverseJoinColumns = @JoinColumn(name = "thresholdgates_id"))

@JsonBackReference

private  List<Threshold> thresholds;


查看完整回答
反對 回復(fù) 2021-10-13
?
犯罪嫌疑人X

TA貢獻(xiàn)2080條經(jīng)驗 獲得超4個贊

與 a 關(guān)聯(lián)的數(shù)據(jù)@ManyToMany默認(rèn)是延遲加載的。您需要在急切加載中指定您想要的(如果您使用的是 spring-data,則可以使用實體圖)。


查看完整回答
反對 回復(fù) 2021-10-13
  • 2 回答
  • 0 關(guān)注
  • 163 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號