views:

49

answers:

2

Hi, 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.

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...

field [name] not found on org.example.smartgwt.shared.model.PrescribedDrug

Entries in drug table are a static and contains the properties of the prescribed drugs.

Thank you.

+1  A: 

worked out answer in the comments, evidently....

hvgotcodes
Yeah, it wasn't there at the beginning, than Hibernate told me to add it. So I did. Than it told me to add "name" which is another field in the Drug class.
code-gijoe
How else can Hibernate know that to which drug_id I'm relating the prescription?
code-gijoe
@code-gijoe you can use a unique many-to-one, as layed out here....http://docs.jboss.org/hibernate/core/3.3/reference/en/html/mapping.html#mapping-declaration-onetoone
hvgotcodes
Actually 1 prescribed drug has a single drug id. But a drug can be related to multiple prescriptions.
code-gijoe
@code-gijoe, if you use a one-to-one, the keys of the two classes have to be the same. I think your situation is more appropriate for a constrained many to one.
hvgotcodes
@code-gijoe, are PrescribedDrug instances prescriptions? If so, you might want a one-to-many from drug to prescription.
hvgotcodes
Thanks a lot! Saved me from going home to late on a friday night.Found a couple of bugs in the .hbm.xml file for Drug (class name was wrong...) and also changed the mapping to drug table as so:<many-to-one name="drugInfo" class="org.example.smartgwt.shared.model.Drug" column="drug_id" unique="true"/>
code-gijoe
+1  A: 

In your mappings, I saw you only mapped for PrescribedDrug.

Change your 2nd mapping file class to Drug

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