理解onetomany和manytoone JPA

问题描述 投票:-1回答:1

我无法理解如何在JPA中运行oneToMany和manyToOne。对于我必须实体的样本。

@Entity
public class Customer {
    @Id
    @GeneratedValue
    private long id;
    private String name;
    private List<Skills> skillList
}

还有一个

@Entity
public class SkillList {
    private String skillName;
    private byte skillLevel;
}

如何纠正这个实体的链接?此外,如果有人能够以可访问的方式解释它。

java hibernate jpa spring-boot spring-data-jpa
1个回答
0
投票

在数据库中,通过外键实现一对多关系。为了根据JPA规范链接Java中的两个实体,如果需要双向关联,则应使用@ManyToOne注释或@ManyToOne@OneToMany

@Entity
public class Customer {
    @Id
    @GeneratedValue
    private Long id;
    private String name;
    @OneToMany(mappedBy = "customer")
    private List<Skill> skills;
}

@Entity
public class Skill {
    @Id
    @GeneratedValue
    private Long id;
    private String skillName;
    private byte skillLevel;
    @ManyToOne
    private Customer customer;
}

它将在数据库中生成两个表。表SKILLCUSTOMER_ID列,与CUSTOMER表有关。

© www.soinside.com 2019 - 2024. All rights reserved.