有两个实体。
位置将与路由表有2个一对多关系。
我正在尝试设置。
路由.xml
<many-to-one name="departure" class="com.nakisa.agency.Location" fetch="select" insert="false" update="false"> <column name="locationID" not-null="true" /> </many-to-one> <many-to-one name="arrival" class="com.nakisa.agency.Location" fetch="select" insert="false" update="false"> <column name="locationID" not-null="true" /> </many-to-one>
位置.xml
<set name="arrivals" table="Routes" inverse="true" lazy="true" fetch="select"> <key> <column name="arrivalID" not-null="true" /> </key> <one-to-many class="com.nakisa.agency.Route" /> </set> <set name="departures" table="Routes" inverse="true" lazy="true" fetch="select"> <key> <column name="departureID" not-null="true" /> </key> <one-to-many class="com.nakisa.agency.Route" /> </set>
我收到错误,因为即使我在路线中设置了departureID,departure ID也为空。
如何更正这些映射以便工作
我认为您在获取方面有问题:
Lazy="true|false" 控制关联是急切加载还是按需加载。 fetch="select|subselect|join|batch" 控制该实体或集合在需要加载时如何加载。
Lazy="true|false"
fetch="select|subselect|join|batch"
具有 lazy="true" 和 fetch="select" Hibernate将延迟加载集合,并使用select加载它。如果您设置 lazy="false" ,相同的select将被执行,不同的是它将被急切地执行。希望这有意义。
lazy="true"
fetch="select"
lazy="false"
尝试设置 lazy=“假” ,然后查看您的 departureID 仍然是 null .
lazy=“假”
departureID
null