【发布时间】:2019-07-08 20:12:24
【问题描述】:
我有一个表格,它模拟了一个国家可以支持多种音高类型的关系。所以 US 支持音高类型 1、2 和 3。MX 支持音高类型 2、3 和 4。
pitch_type 表是一个简单的键值对,其中 pitch_type_id 引用了一个字符串值。将类型连接到国家代码的表在这里:
CREATE TABLE `country_pitch` (
`country_pitch_id` INT NOT NULL AUTO_INCREMENT,
`country_code` VARCHAR(2) NOT NULL,
`pitch_type_id` INT NOT NULL,
PRIMARY KEY (`country_pitch_id`),
INDEX `fk_country_code_to_pitch_type_id_idx` (`pitch_type_id` ASC),
CONSTRAINT `fk_country_code_to_pitch_type_id`
FOREIGN KEY (`pitch_type_id`)
REFERENCES `pitch_type` (`pitch_type_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION);
屈服:
---------------------------------------------------
| country_pitch_id | country_code | pitch_type_id |
---------------------------------------------------
| 1 | us | 1 |
| 2 | us | 2 |
| 3 | us | 3 |
| 4 | mx | 2 |
| 5 | mx | 3 |
| 6 | mx | 4 |
---------------------------------------------------
country_pitch_id 未使用,重要的概念是将国家/地区代码与其关联的 pitch_type 匹配。
我相信这最好用多对多关系来表示,因为一个国家可以有多个音高类型,而一个音高类型可以属于多个国家。我的目标是在类似于这样的 POJO 中对此进行建模:
/**
* For a given market, US, CA, MX, etc, identify the available reach
* generation curve types
*/
@Entity(name = "country_pitch")
@Data
public class CountryPitchTypes {
public CountryPitchTypes() {}
@Column(name = "country_code")
private String countryCode;
@ElementCollection
@Convert(converter = PitchTypeConverter.class)
private List<PitchType> pitchTypes;
}
但是这个映射不能开箱即用,因为我正在尝试将一个连接表映射到这个实体,所以我得到了Unknown column 错误。您能指导我正确添加到 pojo 中的内容是将此表映射到具有关联列表的单个 pojo 吗?
我觉得 @JoinTable 注释对于某种分组是必要的,但不确定如何配置它。在一天结束时,我正在尝试将 country_pitch 表映射到 CountryPitchTypes 类,按国家代码对所有音高类型进行分组。
国家代码不是我们数据库的一部分,顺便说一句。也就是说,没有countries 表,其中 country_id num 指向 2 个字母的 country_code。
【问题讨论】:
-
Country codes are not part of our db, btw.你的意思是你可以从country_pitch表中删除它? -
不,只是没有将代码映射到 id 的“国家”表。