简体   繁体   English

如何使用 JPA 与关联 class 建立递归的多对多关系?

[英]How do I set up a recursive, many-to-many relationship WITH an association class using JPA?

So I'm trying to create a pharmacy database that, among other things, keeps track of drug-to-drug interactions.因此,我正在尝试创建一个药房数据库,其中包括跟踪药物与药物之间的相互作用。 I have a Drug entity with various members and associations and they all seem to work out fine thus far.我有一个拥有各种成员和协会的药物实体,到目前为止,它们似乎都运行良好。 The one implementation I'm not sure I have down correct is a recursive, many-to-many relationship with the Drug entity using an association class.我不确定我是否正确的一个实现是使用关联 class 与 Drug 实体建立递归的多对多关系。 The association class (DrugDrugIX) has 2 foreign keys - one from each drug - and a description and assignment of how severe is that interaction.关联 class (DrugDrugIX) 有 2 个外键 - 来自每种药物的一个 - 以及该相互作用的严重程度的描述和分配。

What I have so far works, in that it will compile and run and let me add Drugs and interactions.到目前为止我所做的工作,因为它将编译和运行,让我添加药物和交互。 However, I certainly don't think what I'm doing is the most optimal -- or even correct.但是,我当然不认为我正在做的事情是最优化的——甚至不是正确的。 Any advice on how to optimize what I'm trying to do?关于如何优化我正在尝试做的任何建议?

The Drug entity (some members omitted for brevity): Drug实体(为简洁起见省略了一些成员):

@Entity
@Table (name = "drugs")
@NamedQueries({
        @NamedQuery (name = Drug.FIND_ALL_BY_NAME,
                     query = "SELECT d FROM Drug d WHERE LOWER (d.chemical_name) LIKE LOWER (CONCAT ('%', :searchString, '%'))" )
})
public class Drug
{
    // QUERY STRING(S)
    public static final String FIND_ALL_BY_NAME = "Drug.findAllByName";

    @Id
    @GeneratedValue (strategy = GenerationType.IDENTITY)
    private long did;

    private String chemical_name;
    private String description;

    // ASSOCIATION(S)
    is this the correct implementation (???)
<----------------------------------------------------------------------------------->
    @OneToMany (mappedBy = "base", cascade = CascadeType.PERSIST)
    private List<DrugDrugIX> interxAsBase;
    
    @OneToMany (mappedBy = "offender", cascade = CascadeType.PERSIST)
    private List<DrugDrugIX> interxAsOffender;
<----------------------------------------------------------------------------------->

    @ManyToOne (cascade = CascadeType.PERSIST)
    private DEA_Class schedule;

    @OneToOne (cascade = CascadeType.PERSIST)
    private Pharmacology PK_profile;

    (...) other associations, getters and setters here (...)

    public void addInterxAsBase (DrugDrugIX interaction)
    {
        if (this.interxAsBase == null)
            interxAsBase = new ArrayList<>();

        interxAsBase.add (interaction);
    }

    public void addInterxAsOffender (DrugDrugIX interaction)
    {
        if (this.interxAsOffender == null)
            interxAsOffender = new ArrayList<>();

        interxAsOffender.add (interaction);
    }
}

The DrugDrugIX association entity: DrugDrugIX关联实体:

@Entity
@Table (name = "drug_drug_interactions")
public class DrugDrugIX
{
    @EmbeddedId
    private DrugDrugIX_PK interaction_CPK;
    private int severityLevel = -1;
    
    // ASSOCIATION(S)
    @ManyToOne
    @MapsId ("base")
    private Drug base;
    
    @ManyToOne
    @MapsId ("offender")
    private Drug offender;

    (...) getters, setters omitted for brevity (...)

    public void registerInteraction (Drug object, Drug precipitate)
    {
        if (this.base == null)
            this.base = object;
        if (this.offender == null)
            this.offender = precipitate;

        if ((this.base == object && this.offender == precipitate) && (severityLevel == -1))
        {
            object.addInterxAsBase(this);
            precipitate.addInterxAsOffender(this);
        }
    }
}

...and just for posterity, the Embeddable that comprises the primary key of the DrugDrugIX entity: ...并且只是为了后代,包含 DrugDrugIX 实体的主键的 Embeddable:

@Embeddable
public class DrugDrugIX_PK implements Serializable
{
    private long base;
    private long offender;
    private String description;

    // CONSTRUCTORS
    public DrugDrugIX_PK () {}
    public DrugDrugIX_PK (Drug base, Drug offender, String description)
    {
        this.base = base.getDID();
        this.offender = offender.getDID();
        this.description = description;
    }
    
    // ACCESSORS
    public String getDescription () { return this.description; }

    // MISCELLANEOUS
    @Override
    public boolean equals (Object other)
    {
        if (other instanceof DrugDrugIX_PK)
        {
            DrugDrugIX_PK second = (DrugDrugIX_PK) other;

            return (this.base == second.base) && (offender == second.offender) && (this.description.equals (description));
        }
        else
            return false;
    }

    @Override
    public int hashCode () { return (int) (base + offender); }
}

I pray to the stackoverflow gods for guidance..我向stackoverflow大神祈祷寻求指导..

You do not need to use recursion.您不需要使用递归。 Just use two nested loops.只需使用两个嵌套循环。 Something like this pseudo code:像这样的伪代码:

drugs = {1, 4, 6}
for drug1 in drugs
    for drug2 in drugs
         get_interaction drug1, drug2

Drugs being administered are represented by integers in the drugs array.正在给药的药物由药物数组中的整数表示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用JPA / EclipseLink与Junction Table建立多对多关系 - How do you Set Up a Many-to-Many Relationship with Junction Table using JPA/EclipseLink 如何在多对多关系中使用hibernate和JPA删除孤立实体? - How do I delete orphan entities using hibernate and JPA on a many-to-many relationship? 如何在不删除映射表中先前数据的情况下使用 Hibernate 或 JPA 建立多对多关系 - How to do many-to-many relationship without deleting the previous data in mapping table using Hibernate or JPA 当使用具有休眠多对多关系的Set时,是否需要重写hashCode? - Do I need to override hashCode when using a Set with a Hibernate many-to-many relationship? StackOverflow错误设置多对多关系Spring JPA - StackOverflow Error Setting up Many-to-Many Relationship Spring JPA 如何访问 jpa 中的多对多表? - How do I access a many-to-many table in jpa? 如何在JPA 2中创建映射为Entity类的3或4方式多对多关联 - How to create 3 or 4 way many-to-many association mapped as Entity class in JPA 2 如何使用Hibernate与@WhereJoinTable保持多对多的关系? - How do I persist a many-to-many relationship using Hibernate with @WhereJoinTable? JPA条件使用IN运算符以多对多关系查询 - JPA criteria query in a many-to-many relationship using IN operator 在JPA中使用多对多关系中的复合键 - Using a composite key in a many-to-many relationship in JPA
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM