gpt4 book ai didi

java - 使用 MYSQL 表结构创建 JPA 映射时遇到问题

转载 作者:行者123 更新时间:2023-12-02 03:18:48 25 4
gpt4 key购买 nike

我有一个表,它模拟了一个国家/地区可以支持多种推介类型的关系。因此US支持音高类型1、2和3。MX支持音高类型2、3和4。

pitch_type 表是一个简单的键值,其中的itch_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;
}

但此映射无法立即使用,因为我正在尝试将联接表映射到此实体,因此出现未知列错误。您能否指导我了解对 pojo 的正确添加是什么,以将此表映射到具有关联列表的单个 pojo?

我觉得 @JoinTable 注释对于某种分组是必要的,但不确定如何配置它。最终,我尝试将country_pitch表映射到CountryPitchTypes类,按国家/地区代码对所有音调类型进行分组。

顺便说一句,国家/地区代码不是我们数据库的一部分。也就是说,不存在 Country_id 数字指向 2 个字母的 Country_code 的 countries 表。

最佳答案

在这种情况下,注释 ManyToMany 不正确。这意味着许多 CountryPitchType 映射到许多 PitchType。这里你需要的是 OneToMany。

关于java - 使用 MYSQL 表结构创建 JPA 映射时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56941980/

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