简体   繁体   English

具有两个以上外键的表的JPA映射

[英]JPA Mapping for table having more than two foreign keys

Below is my database where "table_relation" is having foreign key of other 3 tables "person, address, salary" 以下是我的数据库,其中“ table_relation”具有其他3个表的“外键”,“人,地址,薪水”

在此处输入图片说明

Here are my entities for 3 main tables 这是我的3个主表的实体

在此处输入图片说明

Below is the Entity for "table_relation" 以下是“ table_relation”的实体

@Table(name="table_relation")
@Entity
public class TableRelationEntity {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="id")
    private int id;

    @JoinColumn(name="person_id", referencedColumnName="person_id")
    @ManyToOne
    private PersonEntity person;

    @JoinColumn(name="address_id", referencedColumnName="address_id")
    @ManyToOne
    private AddressEntity address;

    @JoinColumn(name="salary_id", referencedColumnName="salary_id")
    @ManyToOne
    private SalaryEntity salary;

    //getters and setters

I've written getters and setters for all properties. 我已经为所有属性编写了getter和setter方法。

I'm able to get data from the "table_relation" entity using spring data JPA in the below format 我可以使用以下格式的spring数据JPA从“ table_relation”实体中获取数据

{
    id: 1,
    person: { 
        id: 4,
        name: "name 1"
    },
    address: {
        id: 1,
        city: "city 1"
    },
    salary: {
        id: 1,
        amount: "100000"
    }
}

I want to get the "Person" with list of "Address" when searched with "Salary.amount" in the below format. 当我以以下格式搜索“ Salary.amount”时,我想获取带有“地址”列表的“人”。

{ 
    id: 4,
    name: "name 1",
    address: [
        {
            id: 1,
            city: "city 1"
        },
        {
            id: 2,
            city: "city 2"
        }
    ]
}

Can this be achieved by using Mappings in the Provider and Address Entities? 是否可以通过在提供者和地址实体中使用映射来实现? Please guide me in achieving this. 请指导我实现这一目标。 This is replication of my original problem but not the problem itself. 这是我原始问题的复制,而不是问题本身。

Any help is greatly appreciated. 任何帮助是极大的赞赏。

Thanks in advance. 提前致谢。

First off, your database design is not in normal form. 首先,您的数据库设计不是正常形式。 You are now repeating a persons salary for each of the persons addresses. 您现在要为每个人员地址重复人员工资。 What you should have is two join-tables, one between person and salary, and one between person and address. 您应该有两个联接表,一个在人与薪之间,一个在人与地址之间。 Or even better, as none of the relationships are many-many, use a join column. 甚至更好,因为没有很多关系,所以使用联接列。

Second, when using JPA, you have to model based on objects, not database tables. 其次,使用JPA时,必须基于对象而不是数据库表进行建模。 A person has a salary, the relationship between a person and its salary is not a concrete "thing". 一个人有薪水,一个人与其薪水之间的关系不是具体的“事物”。

Your person-entity should look something like this: 您的人物实体应如下所示:

@Entity
public class Person {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="id")
    private int id;

    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
    @JoinColumn(name = "person_id")
    private List<Address> adresses = new ArrayList<>();

    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "salary_id")
    private Salary salary;

    //setters, getters, etc
}

Entities for address and salary need not change. 地址和薪水的实体无需更改。

As for tables, you should have: 至于表,您应该具有:

Person(id, name, salary_id)
Address(id, city, person_id)
Salary(id, amount)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM