【问题标题】:Having trouble creating JPA mapping with MYSQL Table structure使用 MYSQL 表结构创建 JPA 映射时遇到问题
【发布时间】: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 的“国家”表。

标签: java hibernate jpa


【解决方案1】:

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

【讨论】:

  • 或者你可以建立国家和球场的多对多关系,然后让休眠解决剩下的问题。它会自动为您生成桥接表来解决多对多关系。
  • 我将示例更新为同时使用 OneToMany 和 ElementCollection,但仍然无法正常工作
  • 您在更改为 OneToMany 时遇到的问题是什么?你是如何利用这种关系的?
【解决方案2】:

由于国家代码可以映射到 n 个音高类型,单个音高类型可以映射到 n 个国家 所以我们有一个ManyToMany关系

您有 2 种方法来实现多对多关系,或者使用复合主键使用 2 个关系列,或者使用单个主 id 以及 2 个关系列,就像您对 country_pitch_id 所做的那样,因此遵循所有这些实体应该如下:

@Entity(name = "country_pitch")
@Data
public class CountryPitchTypes {
    public CountryPitchTypes() {}

    @Id
    @Column(name = "country_pitch_id", updatable = false, nullable = false)
    @GeneratedValue(strategy = GenerationType.IDENTITY) // or whatever strategy you see good
    private Long countryPitchId

    @Column(name = "country_code")
    private String countryCode;

    @JoinColumn(name = "pitch_type_id", referencedColumnName = "pitch_type_id")
    @ManyToOne
    @Convert(converter = PitchTypeConverter.class)
    private PitchType pitchType;
}

现在你可以去做类似的事情了

SELECT cpt.pitchType from CountryPitchTypes cpt group by cpt.countryCode;

旁注 请记住,ManyToMany 关系基本上是创建第三个表country_pitch,其中您有ManyToOne 字段到另一个表pitch_type,该表具有第三个表的OneToMany,因此您现在可以更新PitchType 实体以拥有CountryPitch 的OneToMany,但您赢了'无论如何都不要使用它,所以其中一种关系就足够了,顺便说一句,这就是所谓的单向关系

在 JPA 中,当第三个表没有任何额外的列 country_pitch_id 时使用 @ManyToMany 注释,只有 2 个关系列 JPA 隐含地理解该表用于执行多对多关系,然后您可以使用 @ManyToMany没有创建第三个实体的相关实体。

【讨论】:

  • 不确定我是否理解,您是说无法映射到 List 并且我需要重构为单个 PitchType?我认为我最初的示例不清楚...使用 countryPitchId 没有必要/不可行,因为我希望将多行映射到一个实体,按国家/地区代码分组。
  • 是的,您需要重构您的代码country_pitch 表,即关系的所有者,因此实体CountryPitchTypes 遵循相同的想法,只有一个引用PitchType 作为ManyToOne 一侧关系
  • I'm looking to map multiple rows to a single entity, grouping by country code 你可以通过插入多个具有相同country_code 值的pitch_type_id 来做到这一点
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-01-19
  • 2014-07-21
  • 2019-11-26
  • 1970-01-01
  • 2021-09-06
  • 1970-01-01
  • 2016-10-02
相关资源
最近更新 更多