如何在休眠中添加一对多关系?
在数据库中,我有这两个表:
- 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于目的地映射,
用于机场测绘,
for destination mapping,
for airport mapping,
请阅读此处提到的示例它清楚地描述了您正在寻找的解决方案。
基本思想是使用城市到机场的多对多关系,并对 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
可能是这样的?
Could be something like this ??
CityAirport
类真的有必要吗?为什么不直接将机场
列表放入您的目的地
中?不确定 .xml 配置。但是使用注释,你会得到类似这样的东西:
和 Airport 类:
Is the
CityAirport
class really necessary? Why not just put a list ofAirport
s into yourDestination
?Not sure about the .xml configuration for this. But using annotations, you would get something like this:
And the Airport class: