如何设置与同一列休眠的多个一对多关系
how to set Multiple one to many relation with same column hibernate
有两个实体。
- 路线(到达、ArrivalID 和出发、DepartureID)作为位置
- 位置(到达、离开)作为路线
位置将与路线有 2 个一对多关系 table。
我正在尝试设置。
Route.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>
Location.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>
即使我在路线中设置了 departmentID,departureID 仍为 null,但我收到错误消息。
如何更正这些映射才能正常工作
我认为您在获取时遇到了问题:
Lazy="true|false"
控制关联是预先加载还是按需加载。
fetch="select|subselect|join|batch"
控制实体或集合在需要时如何加载。
使用 lazy="true"
和 fetch="select"
Hibernate 将延迟加载集合并使用 select 加载它。如果你设置lazy="false"
,同样会执行select,不同的是它会被急切执行。希望这是有道理的。
尝试设置lazy="false"
,然后看看你的departureID
是否还是null
。
有两个实体。
- 路线(到达、ArrivalID 和出发、DepartureID)作为位置
- 位置(到达、离开)作为路线
位置将与路线有 2 个一对多关系 table。
我正在尝试设置。
Route.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>
Location.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>
即使我在路线中设置了 departmentID,departureID 仍为 null,但我收到错误消息。
如何更正这些映射才能正常工作
我认为您在获取时遇到了问题:
Lazy="true|false"
控制关联是预先加载还是按需加载。
fetch="select|subselect|join|batch"
控制实体或集合在需要时如何加载。
使用 lazy="true"
和 fetch="select"
Hibernate 将延迟加载集合并使用 select 加载它。如果你设置lazy="false"
,同样会执行select,不同的是它会被急切执行。希望这是有道理的。
尝试设置lazy="false"
,然后看看你的departureID
是否还是null
。