Java 一对一的Hibernate映射
我有两个用Hibernate绑定的类,我无法确定将药物条目映射到我的处方药类的配置Java 一对一的Hibernate映射,java,hibernate,mapping,Java,Hibernate,Mapping,我有两个用Hibernate绑定的类,我无法确定将药物条目映射到我的处方药类的配置 public class PrescribedDrug implements Serializable { protected int prescribedDrugId; private int drugId; protected String sig; protected int quantity; protected int refillNumber; pro
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>
注意在这里,我试图使用一对一在PrescriptedDrug.hbm.xml配置中映射我的药物,但hibernate一直告诉我将药物的字段添加到PrescriptedDrug文件中
在org.example.smartgwt.shared.model.prescribedrug上找不到字段[name]
药物表中的条目是静态的,包含处方药物的属性
谢谢。在评论中找到了答案,显然……在您的映射中,我看到您只映射了PrescriptedDrug 将第二个映射文件类更改为Drug
<class name="org.example.smartgwt.shared.model.Drug" table="drug" lazy="false">
是的,一开始它不在那里,但Hibernate让我添加它。所以我做了。然后它告诉我添加“name”,这是Drug类中的另一个字段。否则Hibernate怎么知道我正在将处方与哪个药物id关联?@code gijoe您可以使用唯一的多对一,如这里所示。。。。实际上,一种处方药只有一个药物id。但是一种药物可以与多个处方药相关。@code gijoe,如果使用一对一,则两个类的键必须相同。我认为您的情况更适合于受限的多对一代码>在处方药
类中。不应该在那里。。。我添加它是因为Hibernate。但我在最终版本中删除了它。由于@hvgotcodes,我的映射中出现了一些错误。