Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
436 views
in Technique[技术] by (71.8m points)

spring - JPA,Spring映射多个实体(JPA , Spring Mapping multiple entities)

I have 3 tables Consultant, Patient and Diagnosis.

(我有3张桌子顾问,患者和诊断。)

Diagnosis has attributes of both Consultant and Patient as foreign keys.

(诊断具有作为外键的顾问和患者属性。)

I want to know how to show this in spring.

(我想知道如何在春季展示这个。)

What i have so far is, Consultant.java

(到目前为止,我是Consultant.java)

   private String name;
   private String pos;
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "consultant")
    private List<Diagnosis> diagnosis;

Diagnosis.java

public class Request {
    private String token;
    private String comment;
    private boolean status;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "Patient_id")
@JoinColumn(name = "Consultant_id") //not sure about this syntax
    private Consultant consultant;
    private Patient patient;

Patient.java

 private String name;
   private int pid;
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "patient")
    private List<Diagnosis> diagnosis;

Please is this how to go about it?

(请这是怎么回事?)

Also Im using postgresql.

(我也使用postgresql。)

PS I'm new to Spring framework

(PS我是Spring框架的新手)

  ask by B.obed translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Why are you annotating one field if you have 2 entities?

(如果您有2个实体,为什么要注释一个字段?)

Every relationship should be properly annotated:

(每个关系都应正确标注:)

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "Patient_id")
private Patient patient;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "Consultant_id")
private Consultant consultant;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...