简体   繁体   English

Spring Data JPA:如何在插入期间在子实体上设置 2 个外键

[英]Spring Data JPA: How to set 2 foreign keys on the child entity during insert

I am new to Spring JPA and I am having some trouble with setting foreign keys on an entity when inserting a row.我是 Spring JPA 的新手,在插入行时在实体上设置外键时遇到了一些麻烦。

I have the following entities我有以下实体

@Entity
public class Department {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;
    private String departmentName;

    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name = "department_id")
    List<Employee> employees;
}
@Entity
public class Employee {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;
    private String employeeName;

    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name = "employee_id")
    List<Role> roles;

    @ManyToOne
    @JoinColumn(name = "department_id", insertable = false, updatable = false)
    private Department department;
}
@Entity
public class Role {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;
    private String roleName;

    @ManyToOne
    @JoinColumn(name = "employee_id", insertable = false, updatable = false)
    private Employee employee;

    @ManyToOne
    @JoinColumn(name = "department_id", insertable = false, updatable = false)
    private Department department;
}

And here is the Department Repository:这是部门存储库:

@Repository
public interface DepartmentRepository extends JpaRepository<Department, Integer> {

    Department findById(long id);

}

With this approach, the department_id in the employee table and the employee_id in the role table is set correctly when I save a Department object using departmentRepository.save(department) .使用这种方法,当我使用departmentRepository.save(department)保存Department对象时, employee表中的department_id ID 和role表中的employee_id ID 设置正确。

But I also want it to set the department_id in the role table.但我也希望它在role表中设置department_id How can this be achieved?如何做到这一点? Currently the relationship Department and Role is indirect (ie it is through Employee) but would I have to create a direct relationship between the 2 entities?目前部门和角色的关系是间接的(即通过员工),但我必须在这两个实体之间创建直接关系吗? I am not sure how to achieve this though.我不确定如何实现这一点。 Any input will be appreciated.任何输入将不胜感激。

Edit:编辑:

I want to model the relationship in this Entity Relationship diagram我想在这个实体关系图中建模关系

As I see that you already using我看到你已经在使用

@OneToMany(cascade = CascadeType.ALL)

So you knew that every update on Department with list Employee can do "many" things.因此,您知道 Department 的每次更新以及列表 Employee 都可以做“很多”事情。

So Employee do the same to Roles (your list) but the magic auto save/update won't help.所以员工对角色(你的列表)做同样的事情,但神奇的自动保存/更新不会有帮助。

Condition now:现在条件:

  • Save action in repo在 repo 中保存操作
  • List of Employee员工名单
  • List of Role in employee (each object don't have Department Id)员工角色列表(每个对象没有部门 ID)

As I understand:我认为:

You have Department that 1-n to Employee (Employee can only have 1 Department)您有 1-n 到员工的部门(员工只能有 1 个部门)

Employee can have many Role员工可以有很多角色

As normal logic: Deparment 1-n Employee 1-n Roles按照正常逻辑:部门 1-n 员工 1-n 角色

But some how you want Role have object of Department also.但是一些你想要的角色也有部门的对象。

So my first suggestion is change of logic code.所以我的第一个建议是更改逻辑代码。

And second: a way to solve your problem!第二:解决问题的方法!

Create Department创建部门

when add Role to Employee, please access and add deparment_id also.将 Role 添加到 Employee 时,请访问并添加 deparment_id。 (Department save List Employee, when save Employee - list Roles also save with same logic, just make sure to add it) (部门保存List Employee,保存Employee时-list Roles也保存相同的逻辑,只要确保添加它)

暂无
暂无

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

相关问题 Spring DATA JPA示例用于单个实体中的多个外键 - Spring DATA JPA example for multiple foreign keys in a single entity 如何使用 Spring Boot Data JPA 在一对多映射的子实体中设置 parentId - How to set parentId in Child Entity of one to many mapping using Spring Boot Data JPA 在Spring Data JPA中,如何在运行时添加实体? - In Spring Data JPA, How to add an Entity during runtime ? 如何在通过 Spring Boot JPA 向 MySQL 插入实体/对象时自动插入外键 - How to auto insert foreign key when insert entity/object by Spring Boot JPA to MySQL 使用两个外键创建一个实体 Class - Spring JPA - Create an Entity Class with two foreign keys - Spring JPA Spring数据JPA-具有另一个未插入表示相同表字段的外键的子实体 - Spring data jpa -Child entity having another foreign key representing same table field not inserted Spring Data Jpa一对多的关系。 无法插入子记录:外键为NULL - Spring Data Jpa one to many relationship. Not able to insert child records: foreign key is coming as NULL Spring Data JPA-如何使用复合键插入子实体? - Spring Data JPA - How to insert child entities with composite key? JPA 2 - 如何使用Spring Data JPA构建具有主键的实体,该主键也是外键? - JPA 2 - How to build entity that has Primary key that is also a foreign key using Spring Data JPA? Spring Data JPA通过从父级获取id与父级实体一起插入子级 - Spring Data JPA insert with child along with parent entity by taking id from parent
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM