【问题标题】:Establish relationship from one table to another table in Hibernate在 Hibernate 中建立从一个表到另一个表的关系
【发布时间】:2009-11-03 08:40:14
【问题描述】:

我有两张桌子,一张是

城市表(int id,字符串名称)

我的另一张桌子是

距离表(int id,int cityId (FK city),int neighbourId(FK city))

我想使用 Hibernate,但我无法在 Hibernate 中建立这些表之间的关系。

【问题讨论】:

  • 你有什么问题,为什么不能建立这些关系?
  • 我是通过 Hibernate 工具制作的,但我的问题是这些表的对象表示。我解决了它:)

标签: java database hibernate


【解决方案1】:

类似的东西呢

<class name="City" table="CITIES">
    <id name="id" type="integer">
      <generator class="native" />
    </id>
    <property name="name" />
    <set name="neighbours" table="DISTANCES">      
        <key column="city_id" />
        <many-to-one name="neighbour" class="City" />
    </set>
</class>

虽然没有测试。

【讨论】:

    【解决方案2】:

    好的,我可以看到正常执行的任何问题。

    <class name="City" table="CITY">
        <id name="id" type="integer">
          <generator class="native" />
        </id>
        <property name="name" />
    </class>
    <class name="Distance" table="DISTANCE">
        <id name="id" type="integer">
          <generator class="native" />
        </id>
        <many-to-one name="city" column="cityId" class="City"/>
        <many-to-one name="neighbour" column="neighbourId" class="City"/>
    </class>
    

    也没有测试。

    【讨论】:

      猜你喜欢
      • 2013-06-26
      • 2019-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-02
      • 1970-01-01
      • 1970-01-01
      • 2018-09-04
      相关资源
      最近更新 更多