[英]Hibernate mapping xml one-to-one (many-to-one)
I'm trying to map a relationship between two classes which have a one-to-one relationship. 我正在尝试映射具有一对一关系的两个类之间的关系。 After looking up on the internet it seems like people prefer to map it using many-to-one. 在互联网上查找后,似乎人们更喜欢使用多对一映射。
For example have have a class Order and a class Bill. 例如,有一个类Order和一个Bill。 Bill holds a FK to the invoice. Bill持有发票的FK。
Here is my mapping for Bill: 这是我对比尔的映射:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Mar 21, 2016 10:46:20 PM by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="domain.Bill" table="BILL">
<id name="id" type="int">
<column name="ID" />
<generator class="native" />
</id>
<many-to-one name="order" class="domain.Order" column="ORDER_ID" unique="true" not-null="true"/>
</class>
</hibernate-mapping>
As you can see above, in my mapping of Bill I can specify the column of Fk to the Order, but what should I put in my mapping for Order since it does not have a Fk to Bill? 如您在上方所见,在我的Bill映射中,我可以指定Fk到订单的列,但是由于Order没有Fk到Bill,我应该在我的映射中输入什么?
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Mar 21, 2016 10:46:20 PM by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="domain.Order" table="ORDER">
<id name="id" type="int">
<column name="ID" />
<generator class="native" />
</id>
<many-to-one name="bill" class="domain.Bill" ???? fetch="select"/>
</class>
</hibernate-mapping>
You should try to map it as it is: one-to-one. 您应该尝试按原样进行映射:一对一。
The only reason that I am aware of why people may recommend many-to-one is because of the lazy loading issues on the inverse side of the one-to-one associations. 我知道为什么人们会推荐多对一的唯一原因是因为一对一关联的反面存在延迟加载问题。 Then you probably want a fake one-to-many association on the inverse side (after all it's the only logical inverse side of many-to-one). 然后,您可能希望在反面建立一个假的一对多关联(毕竟,这是多对一的唯一逻辑反面)。
However, take a look at this answer for different alternatives. 但是,请查看此答案的其他替代方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.