在加入这两个表时,我在 Hibernate 上有一个奇怪的行为。
表 1 - 国家/地区
CTRY_ID | CTRY_ALPHA2_CODE | CTRY_NAME | IS_ACTIVE |
---|---|---|---|
17 | GB | 英国 | 是 |
30 | PT | 葡萄牙 | 是 |
表 2 - 国家地图
MAP_CTRY_ID | MAP_SRC_ID | MAP_SRC_COUNTRY |
---|---|---|
17 | 1 | 英格兰 |
17 | 1 | 苏格兰 |
17 | 1 | 威尔士 |
17 | 1 | 北爱尔兰 |
30 | 1 | 葡萄牙 |
使用以下 TypedQuery 我有正确的行数 (5) 但在 ID 为 17 的映射中,所有行都具有相同的数据,例如,英格兰的 4 倍,而不是英格兰、苏格兰、威尔士和北爱尔兰.
我也尝试过相反的方法,也就是说,从 CountryMap 开始查询并加入 Country,但我得到了相同的结果。
Food
SELECT c from Country c
JOIN FETCH c.countryMap cm
WHERE c.isActive = :boolean
AND cm.id.sourceId = :id
这就是我映射这两个实体的方式。
select
country0_.ctry_id as ctry_id1_0_0_,
countrymap1_.map_ctry_id as map_ctry1_1_1_,
countrymap1_.map_src_id as map_src_2_1_1_,
country0_.ctry_alpha2_code as ctry_alp2_0_0_,
country0_.ctry_alpha3_code as ctry_alp3_0_0_,
country0_.ctry_is_active as ctry_is_4_0_0_,
country0_.ctry_name as ctry_nam5_0_0_,
country0_.ctry_numeric_code as ctry_num6_0_0_,
countrymap1_.map_src_country as map_src_3_1_1_,
countrymap1_.map_ctry_id as map_ctry1_1_0__,
countrymap1_.map_src_id as map_src_2_1_0__
from bta_country country0_
inner join bta_country_map countrymap1_ on country0_.ctry_id=countrymap1_.map_ctry_id
where country0_.ctry_is_active='Y'
and countrymap1_.map_src_id=1
@Entity
@Immutable
@Table(name = "BTA_COUNTRY")
public class Country {
@JsonIgnore
@Id
@Column(name = "CTRY_ID")
private long id;
@Column(name = "CTRY_ALPHA2_CODE")
private String alpha2Code;
@Column(name = "CTRY_ALPHA3_CODE")
private String alpha3Code;
@Column(name = "CTRY_NUMERIC_CODE")
private String numericCode;
@Column(name = "CTRY_NAME")
private String name;
@Column(name = "CTRY_IS_ACTIVE")
@Type(type = "yes_no")
private boolean isActive;
@JsonIgnore
@OneToMany(mappedBy = "countries", fetch = FetchType.LAZY)
private List<CountryMap> countryMap;
你能帮我理解我在这里遗漏了什么吗? CountryMap 的 FK 有问题吗?