2010-07-23 217 views
0

我有兩個類與Hibernate綁定映射,我無法弄清楚將配置映射到藥物條目到我的規定藥物類。休眠映射一對一

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

我的數據模型是這樣的:

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

這裏是我的Hibernate配置:

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

注意在這裏,我想我的藥物映射在PrescribedDrug.hbm.xml配置使用一到一個但休眠不斷告訴我添加字段藥物PrescribedDrug文件...

區[name]不org.example.smartgwt.shared找到。 model.PrescribedDrug

條目中藥物表是靜態的,包含處方藥的性質。

謝謝。

+0

我不明白的是這'私人詮釋drugId;'的'PrescribedDrug'類。 – 2010-07-24 00:39:01

+0

不應該在那裏......我因爲Hibernate而添加了它。但是我在最終版本中刪除了它。我在映射中遇到了一些錯誤,由於@ hvgotcodes而得到修復。 – 2010-07-24 12:26:44

回答

0

摸索出答案的評論,顯然....

+0

是的,它在開始的時候並不存在,比Hibernate告訴我要添加它。所以我做了。比它告訴我添加「名稱」,這是毒品類中的另一個字段。 – 2010-07-23 22:57:55

+0

Hibernate怎麼能知道我在處方中與哪個drug_id相關? – 2010-07-23 23:02:30

+0

@ code-gijoe你可以使用一個獨特的多對一,如這裏所展示的.... http://docs.jboss.org/hibernate/core/3.3/reference/en/html/mapping。 html#mapping-declaration-onetoone – hvgotcodes 2010-07-23 23:05:18

1

在您的映射,我看見你只映射爲PrescribedDrug。

更改你的第二映射文件類藥物

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