如何在休眠中添加一对多关系?

发布于 2025-01-01 07:57:21 字数 1497 浏览 3 评论 0原文

在数据库中,我有这两个表:

- Destination:
 - idDestination
 - name

- Airport:
 - idAirport
 - idDestination // FK into Destination city
 - name

其中:

  • 1 目的地(读:城市)有许多机场
  • 1 机场属于 1 个城市
  • 因此: 目的地-机场之间的 1-许多关系

我的 Java 类如下所示:

class Destination{
 private Integer idDestination;
 private String name;

 // getter and setters
}

class Airport{
 private Integer idAirport;
 private Destination city;
 private String name;
}

// Separate class for airports in city, since city is being used in a lot of other places
// and I'd like to keep Destination class clean
class CityAirports{
 private Destination City;
 private Set<Airport> airports;
}

Hibernate 映射:机场。 hbm.xml

<hibernate-mapping>
    <class name="org.wah.dao.Airport" table="AIRPORT">
        <id name="idAirport" type="java.lang.Integer">
            <column name="IDAIRPORT" />
            <generator class="identity" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="NAME" />
        </property>
        <many-to-one name="city" class="org.wah.dao.Destination">
            <column name="IDCITY" />
        </many-to-one>
    </class>
</hibernate-mapping>

我需要为 CityAirports 定义另一个映射 - 检索城市内的所有机场。 - 在城市中添加一个新机场。

我不确定休眠映射会是什么样子?有人可以指导我该怎么做吗?

On the database, I have these two tables:

- Destination:
 - idDestination
 - name

- Airport:
 - idAirport
 - idDestination // FK into Destination city
 - name

where:

  • 1 Destination(read: city) has many Airports
  • 1 Airport belongs to 1 City
  • Hence: 1-Many relation between Destination-Airports

My Java classes look like this:

class Destination{
 private Integer idDestination;
 private String name;

 // getter and setters
}

class Airport{
 private Integer idAirport;
 private Destination city;
 private String name;
}

// Separate class for airports in city, since city is being used in a lot of other places
// and I'd like to keep Destination class clean
class CityAirports{
 private Destination City;
 private Set<Airport> airports;
}

Hibernate Mappings: Airport.hbm.xml

<hibernate-mapping>
    <class name="org.wah.dao.Airport" table="AIRPORT">
        <id name="idAirport" type="java.lang.Integer">
            <column name="IDAIRPORT" />
            <generator class="identity" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="NAME" />
        </property>
        <many-to-one name="city" class="org.wah.dao.Destination">
            <column name="IDCITY" />
        </many-to-one>
    </class>
</hibernate-mapping>

I need to define another mapping for CityAirports to
- retrieve all the airports within the city.
- add a new airport to the city.

I am not sure what the hibernate mapping would look like ? Can someone please guide me on how to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

丢了幸福的猪 2025-01-08 07:57:21

对于目的地映射,

<hibernate-mapping>
 <class name="org.wah.dao.Destination" table="DESTINATION">
 <id name="idDestination" type="int" column="DESTINATION_ID">
     <generator class="native" />
 </id>
 <property name="name" type="string" not-null="true" length="100" 
    column=DESTINATION_NAME" />
 <set name="destinationAirPorts" table="DESTINATION_AIRPORT" cascade="all">
  <key column="DESTINATION_ID" />
  <many-to-many column="IDAIRPORT" unique="true" class="org.wah.dao.Airport" />
 </set>
</class>

用于机场测绘,

<hibernate-mapping>
 <class name="org.wah.dao.Airport" table="AIRPORT">
 <id name="idAirport" type="int" column="IDAIRPORT">
     <generator class="native" />
 </id>
 <property name="name" type="string" not-null="true" length="100" 
    column=NAME" />

</class>
</hibernate-mapping>

for destination mapping,

<hibernate-mapping>
 <class name="org.wah.dao.Destination" table="DESTINATION">
 <id name="idDestination" type="int" column="DESTINATION_ID">
     <generator class="native" />
 </id>
 <property name="name" type="string" not-null="true" length="100" 
    column=DESTINATION_NAME" />
 <set name="destinationAirPorts" table="DESTINATION_AIRPORT" cascade="all">
  <key column="DESTINATION_ID" />
  <many-to-many column="IDAIRPORT" unique="true" class="org.wah.dao.Airport" />
 </set>
</class>

for airport mapping,

<hibernate-mapping>
 <class name="org.wah.dao.Airport" table="AIRPORT">
 <id name="idAirport" type="int" column="IDAIRPORT">
     <generator class="native" />
 </id>
 <property name="name" type="string" not-null="true" length="100" 
    column=NAME" />

</class>
</hibernate-mapping>
忆沫 2025-01-08 07:57:21

请阅读此处提到的示例它清楚地描述了您正在寻找的解决方案。
基本思想是使用城市到机场的多对多关系,并对 IDAIRPORT 施加唯一约束

Read the example mentioned here which describes the solution you are looking for neatly.
Basic idea is that you use many-to-many relationship from city-to-airport and place unique constraint on IDAIRPORT

记忆で 2025-01-08 07:57:21

可能是这样的?

<class name="org.wah.dao.CityAirports">
        <id name="caID">
            <generator class="assigned" />
        </id>

        <set name="Airport" cascade="save-update" >
            <key column="apID" />
            <one-to-many class="org.wah.dao.Airport" />
        </set>
        <many-to-one name="city" class="org.wah.dao.Destination">
            <column name="IDCITY" />
        </many-to-one>

</class>

Could be something like this ??

<class name="org.wah.dao.CityAirports">
        <id name="caID">
            <generator class="assigned" />
        </id>

        <set name="Airport" cascade="save-update" >
            <key column="apID" />
            <one-to-many class="org.wah.dao.Airport" />
        </set>
        <many-to-one name="city" class="org.wah.dao.Destination">
            <column name="IDCITY" />
        </many-to-one>

</class>
柒七 2025-01-08 07:57:21

CityAirport 类真的有必要吗?为什么不直接将机场列表放入您的目的地中?

不确定 .xml 配置。但是使用注释,你会得到类似这样的东西:

@Entity
public class Destination {
   @Id
   @GeneratedValue
   private Integer idDestination;

   private String name;
   @OneToMany(mappedBy="destination")
   Set<Airport> airports;
    [...]
}

和 Airport 类:

@Entity
public class Airport {
    @Id
    @GeneratedValue
    private Integer id;

    @ManyToOne
    private Destination destination;
    [...]
}

Is the CityAirport class really necessary? Why not just put a list of Airports into your Destination?

Not sure about the .xml configuration for this. But using annotations, you would get something like this:

@Entity
public class Destination {
   @Id
   @GeneratedValue
   private Integer idDestination;

   private String name;
   @OneToMany(mappedBy="destination")
   Set<Airport> airports;
    [...]
}

And the Airport class:

@Entity
public class Airport {
    @Id
    @GeneratedValue
    private Integer id;

    @ManyToOne
    private Destination destination;
    [...]
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文