MySql:疾病与症状之间的关系

时间:2018-08-17 13:42:59

标签: java mysql many-to-many

我正在开发一个Spring Boot App项目,该项目可以处理疾病和症状。所以我在想,如果一种疾病可以有更多的症状,而且一种症状可以与更多的疾病相关联,这意味着我将在MySql中建立多对多关系。同样,症状可以是一种疾病的一般症状,也可以是另一种疾病的特定症状(每种疾病都有其一般症状和特定症状)。这是否意味着我必须在疾病和症状之间建立两个多对多的关系?

我不确定我是否真的需要针对每种症状在一般症状上列出疾病清单,在特定症状上列出清单。

我对MySql还是很陌生,所以我想知道这是否是最佳解决方案。任何输入将不胜感激:

@Entity
public class Disease implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private String id;
    @Column(nullable = false)
    private String name;
    @Column(nullable = false)
    private DiseaseGroup group;
    @ManyToMany(fetch = FetchType.LAZY,
            cascade = {
                CascadeType.PERSIST,
                CascadeType.MERGE
            })
    @JoinTable(name = "disease_generalSymptoms",
            joinColumns = { @JoinColumn(name = "disease_id") },
            inverseJoinColumns = { @JoinColumn(name = "symptom_id") })
    private ArrayList<Symptom> generalSymptoms = new ArrayList<Symptom>();
    @ManyToMany(fetch = FetchType.LAZY,
            cascade = {
                CascadeType.PERSIST,
                CascadeType.MERGE
            })
    @JoinTable(name = "disease_specificSymptoms",
            joinColumns = { @JoinColumn(name = "disease_id") },
            inverseJoinColumns = { @JoinColumn(name = "symptom_id") })
    private ArrayList<Symptom> specificSymptoms = new ArrayList<Simptom>();
}

@Entity
public class Symptom implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @Column(nullable = false)
    private String name;
    @ManyToMany(fetch = FetchType.LAZY,
            cascade = {
                CascadeType.PERSIST,
                CascadeType.MERGE
            },
            mappedBy = "symptom")
    private ArrayList<Disease> diseasesWhereThisIsAGeneralSymptom = new ArrayList<Disease>();
    @ManyToMany(fetch = FetchType.LAZY,
        cascade = {
            CascadeType.PERSIST,
            CascadeType.MERGE
        },
        mappedBy = "symptom")
    private ArrayList<Disease> diseasesWhereThisIsASpecificSymptom = new ArrayList<Disease>();

}

0 个答案:

没有答案