代码之家  ›  专栏  ›  技术社区  ›  Shan Khan

如何将同一列的多个一对多关系设置为休眠

  •  0
  • Shan Khan  · 技术社区  · 10 年前

    有两个实体。

    1. 路线(到达、到达ID和离开、离开ID)作为位置
    2. 位置(到达、离开)作为路线

    位置将与路由表有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也为空。

    如何更正这些映射以便工作

    1 回复  |  直到 10 年前
        1
  •  -1
  •   m.aibin    10 年前

    我认为您在获取方面有问题:

    Lazy="true|false" 控制关联是急切加载还是按需加载。 fetch="select|subselect|join|batch" 控制该实体或集合在需要加载时如何加载。

    具有 lazy="true" fetch="select" Hibernate将延迟加载集合,并使用select加载它。如果您设置 lazy="false" ,相同的select将被执行,不同的是它将被急切地执行。希望这有意义。

    尝试设置 lazy=“假” ,然后查看您的 departureID 仍然是 null .

    推荐文章