gpt4 book ai didi

java - Hibernate 传递集合映射

转载 作者:行者123 更新时间:2023-12-02 06:55:23 26 4
gpt4 key购买 nike

我正在使用 Hibernate 3.6 版本,带有 xml 映射文件。就我而言,我有三个映射实体,分别是探测器天线位置。基本上,有 Detector -> Set<Antenna>Location -> Set<Antenna>关系,我也想有Detector -> Set<Location>可用。

每个探测器都有一组天线实体,映射如下:

<set name="_Antennas" table="tantenna" inverse="true" cascade="all">
<key>
<column name="id_detector" not-null="true" />
</key>
<one-to-many class="Antenna" />
</set>

此外,每个天线都属于特定位置和特定探测器。这是多对一映射来引用:

<many-to-one name="_Detector" class="com.tadic.model.detector.Detector"
column="id_detector" />

<many-to-one name="_Location" class="com.tadic.model.location.Location"
column="id_location" />

同样,位置也有一组天线:

<set name="_Antennas" table="tantenna">
<key>
<column name="id_location" />
</key>
<one-to-many class="com.tadic.model.detector.Antenna" />
</set>

所以探测器知道它的天线,天线知道它们的探测器和位置。位置实体有一组天线,但tlocation表没有外键。

但是,我有兴趣了解探测器在特定点的所有位置。我知道我可以编写 HQL,但我想知道当检测器加载时这是否可行,只需将其映射为一组位置实体即可。

记住tlocation表没有idDetector列来链接它,而且我认为没有必要。

最佳答案

如果我从数据库角度得到了正确的结果

t探测器 [1]--[id_探测器]-->[n] 天线

tlocation [1]--[id_location]-->[m]天线

含义 tantenna 有一个列元组 (id_Detector, id_location),本质上是 tDetector 和 tlocation 之间的链接表。这可用于促进探测器和位置之间的多对多映射。

这是检测器 hibernate 映射的映射片段。

<set name="locations" table="tantenna">
<key column="id_detector" />
<many-to-many class="com.tadic.model.location.Location" column="id_location" />
</set>

还有一件事。根据我的经验,在 ORM 上映射如此复杂的关系方案并不是没有代价的。即使 hibernate 发现您的映射文件在 session 工厂初始化期间没有问题,我也强烈建议您彻底测试,并在必要时使用 insert="false"update 将某些关系指定为只读(即仅在读取数据时有用) =“假”。

关于java - Hibernate 传递集合映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17443505/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com