简体   繁体   English

一对一的Hibernate映射

[英]Hibernate mapping for one-to-one

I have two classes binded mapped with Hibernate and I can't figure out the configuration to map a Drug entry to my PrescribedDrug class. 我有两个绑定到Hibernate的类,无法确定将Drug条目映射到我的PrescribedDrug类的配置。

public class PrescribedDrug implements Serializable {

    protected int prescribedDrugId;
    private int drugId;
    protected String sig;
    protected int quantity;
    protected int refillNumber;
    protected Drug drugInfo;
    ...
}

public class Drug implements Serializable {
    protected int drugId;
    protected String name;
    protected double strength;
    protected String strengthUnits;
    ...
}

My data model looks like this: 我的数据模型如下所示:

prescribed_drug             drug
----------                  --------------
prescribed_drug_id*         drug_id*
drug_id*                    name
sig                         ....
...

Here is my hibernate configuration: 这是我的休眠配置:

<hibernate-mapping>
    <class name="org.example.smartgwt.shared.model.PrescribedDrug" table="prescribed_drug" lazy="false">

        <id name="prescribedDrugId" column="prescribed_drug_id">
            <generator class="assigned"/>
        </id>

        <property name="drugId">
            <column name="drug_id"/>
        </property>
        <property name="sig">
            <column name="sig"/>
        </property>
        <property name="quantity">
            <column name="quantity"/>
        </property>
        <property name="refillNumber">
            <column name="refill_number"/>
        </property>

        <one-to-one name="drugInfo" class="org.example.smartgwt.shared.model.Drug" lazy="false" />

    </class>
</hibernate-mapping>

<hibernate-mapping>
    <class name="org.example.smartgwt.shared.model.PrescribedDrug" table="prescribed_drug" lazy="false">

        <id name="drugId" column="drug_id">
            <generator class="assigned"/>
        </id>

        <property name="name">
            <column name="name"/>
        </property>
        <property name="strength">
            <column name="strength"/>
        </property>
        <property name="strengthUnits">
            <column name="strength_units"/>
        </property>
    </class>
</hibernate-mapping>

NOTE here that I tried to map my Drug in the PrescribedDrug.hbm.xml config using a one-to-one but hibernate keeps telling me to add the fields of Drug to the PrescribedDrug file... 注意这里我尝试使用一对一的方式PrescribedDrug.hbm.xml配置中映射我的Drug ,但是休眠状态一直告诉我将Drug的字段添加到PrescribedDrug文件中...

field [name] not found on org.example.smartgwt.shared.model.PrescribedDrug 在org.example.smartgwt.shared.model.PrescribedDrug上找不到字段[名称]

Entries in drug table are a static and contains the properties of the prescribed drugs. 药物表中的条目是静态的,包含处方药的属性。

Thank you. 谢谢。

In your mappings, I saw you only mapped for PrescribedDrug. 在您的映射中,我看到您仅映射了PrescribedDrug。

Change your 2nd mapping file class to Drug 将第二个映射文件类更改为Drug

<class name="org.example.smartgwt.shared.model.Drug" table="drug" lazy="false">

很明显地在评论中给出了答案。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM