简体   繁体   English

Spring 和 Hibernate 映射问题一对多

[英]Spring and Hibernate mapping issue One-to-Many

I have a form that tracks employee projects and the fields are: employee name (the project requestor), employee assigned to complete the task, task description, and a field with the option to reassign the task to someone else.我有一个跟踪员工项目的表单,字段是:员工姓名(项目请求者)、分配给完成任务的员工、任务描述和一个可以将任务重新分配给其他人的字段。 I have a SQL employee table with the name and email of all employees.我有一个 SQL 员工表,其中包含所有员工的姓名和 email。 Then I have a table for the projects.然后我有一个项目表。 I have it set up so all three columns refer to the employee table via Foreign keys.我已经设置好了,所以所有三列都通过外键引用员工表。

I'm experiencing a mapping issue with Spring JPA.我遇到了 Spring JPA 的映射问题。 One project can have many employees because there are three employee related fields.一个项目可以有很多员工,因为有三个员工相关领域。 Is this right?这是正确的吗?

Or should it be a many to many because an employee can have many projects and each project could have three employess stored (same or different) that it should be a many to many relationship.还是应该是多对多的,因为一个员工可以有很多项目,每个项目可以存储三个员工(相同或不同),这应该是多对多的关系。

The one to many seems right but then also is confusing because there will only be one employee listed in the employee name, one in the assigned to and one in the reassigned to field.一对多似乎是正确的,但也令人困惑,因为员工姓名中只会列出一名员工,一名在分配给字段中,一名在重新分配给字段中。 Is one-to-many right?一对多对吗?

public class ProjectTracker {
      @ManyToMany
      @JoinColumn(name = "employeeName")
      private Employee employeeName;

      private String taskDetails;

      @ManyToMany
      @JoinColumn(name = "employeeName")
      private Employee assignedTo;

      @ManyToMany
      @JoinColumn(name = "employeeName")
      private Employee reassignedTo;
}

public class Employee {
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Column(name = "employeeId")
      private Long employeeId;

      private String employeeName;
      private String emailAddress;
      private Boolean isAssignee=false;
      private Boolean isRequestor=true;
}

One project can have many employees because there are three employee related fields.一个项目可以有很多员工,因为有三个员工相关领域。 Is this right?这是正确的吗?

No.不。

The number of fields is not relevant.字段的数量不相关。 Here you have three different associations.在这里,您有三个不同的关联。 Given an employee you need an association for the project they have requested, an association for the projects the've been assigned to and an association for the projects they've been reassigned to.给定员工,您需要与他们请求的项目相关联、已分配到的项目的关联以及已重新分配到的项目的关联。 All these associations are all one-to-many because an employee can be the requester for many different projects and the same is true for the other roles.所有这些关联都是一对多的,因为员工可以是许多不同项目的请求者,其他角色也是如此。

A single project, a row on the db, can only have one requester, one employee it has been assigned to and one person it has been reassigned to.单个项目,数据库中的一行,只能有一个请求者,一个分配给它的员工,一个重新分配给它的人。 Each one of them is a many-to-one.它们中的每一个都是多对一的。

ProjectTracker should probably look something like: ProjectTracker应该看起来像:

public class ProjectTracker {
  @Id
  private Long id; 

  @ManyToOne
  @JoinColumn(name = "requestedBy_id")
  private Employee employeeName;

  private String taskDetails;

  @ManyToOne
  @JoinColumn(name = "assignedTo_id")
  private Employee assignedTo;

  @ManyToOne
  @JoinColumn(name = "reassignedTo_id")
  private Employee reassignedTo;
}

Given a project tracker, you should have all the information you need.给定一个项目跟踪器,您应该拥有所需的所有信息。 I would suggest to read the Hibernate ORM documentation chapter about associations .我建议阅读有关关联的 Hibernate ORM 文档章节 It contains many examples of all the different mappings.它包含所有不同映射的许多示例。

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

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