SpringBoot JPA中的实体映射错误.实体正在创建,但未添加foreign_key



我正在学习SpringBoot,为此,我试图在Problem的帮助下实现我所学的内容。

这是一个基本问题:我们有两个实体:

Team: id, name Developer: id, team_id, name, phone number

Create team API : This api takes in a team and a list of developers to be mapped with this team, and is expected to create the corresponding entries in the database.
Sample request: {"team": {"name": "claims"}, "developers": [{"name": "someone", "phone_number": "9999999999"}, {"name": "somebody", "phone_number": "9111111111"}]}

我使用了这些实体:

**Employee.java**
package com.PagerDuty.PagerDutyManager.Employee;
import com.PagerDuty.PagerDutyManager.Team.Team;
import com.fasterxml.jackson.annotation.JsonBackReference;
import javax.persistence.*;
import javax.validation.constraints.*;
@Entity
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@NotNull
@NotBlank(message="Invalid Name")
private String name;
@Column(columnDefinition="varchar(10)", unique = true)
@NotBlank(message = "Phone_number Number Required!")
@Pattern(regexp="(^$|[0-9]{10})", message="Invalid Phone_number Number")
private String phone_number;
@ManyToOne
@JoinColumn(referencedColumnName = "id")
@JsonBackReference
private Team team;
public Employee(){}
public Employee(Long id,
@NotNull @NotBlank(message = "Invalid Name") String name,
@NotBlank(message = "Phone_number Number Required!") @Pattern(regexp = "(^$|[0-9]{10})", message = "Invalid Phone_number Number") String phone_number, String teamId) {
this.id = id;
this.name = name;
this.phone_number = phone_number;
this.team = new Team(teamId, "");
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone_number() {
return phone_number;
}
public void setPhone_number(String phone_number) {
this.phone_number = phone_number;
}
public Team getTeam() {
return team;
}
public void setTeam(Team team) {
this.team = team;
}
@Override
public String toString() {
return "Employee{" +
"id=" + id +
", name='" + name + ''' +
", phone_number='" + phone_number + ''' +
", team=" + team +
'}';
}
}
**Team.java**
package com.PagerDuty.PagerDutyManager.Team;
import com.PagerDuty.PagerDutyManager.Employee.Employee;
import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import javax.persistence.*;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import java.util.List;
@Entity
public class Team {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@NotNull
@NotBlank(message="Invalid Name")
@Column(unique = true)
private String name;
@OneToMany(cascade=CascadeType.ALL, mappedBy = "team")
private List<Employee> employees;
public Team(){}
public Team(String id,
@NotNull @NotBlank(message = "Invalid Name") String name) {
this.id = Long.parseLong(id);
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Employee> getEmployees() {
return employees;
}
public void setEmployees(List<Employee> employees) {
this.employees = employees;
}
@Override
public String toString() {
return "Team{" +
"id=" + id +
", name='" + name + ''' +
", employees=" + employees +
'}';
}
}

我已经完成了上面的样品请求,但无法建立关系

在添加团队时成功添加员工。添加团队:这里的RequestBody是为了正确处理主体//请建议一种更好的方法来执行requestBody结构处理``

**AddTeam Controller Function**
@PostMapping("/team")
public void addTeam(@RequestBody RequestCustomBody body){
System.out.println("Add team request body "+body.toString());
Team team = body.getTeam();
team.setEmployees(body.getDevelopers());
System.out.println("New Team "+team.toString());
teamService.addTeam(team);
}
**AddTeam Service Function**
public void addTeam(Team team){
teamRepository.save(team);
}

但我只添加了Employee和Team,但没有添加外键。

任何人都请帮帮我,我做错了什么。并告诉你一个好的做事方法。

您需要执行body.getDevelopers().forEach(d -> d.setTeam(team))

为什么?用JPA术语来说,雇员是关系的拥有方。这是违反直觉的(在日常使用中,可以说"一个团队有员工"(。但在JPA中,拥有方是(在这里简化(拥有外键的一方(使用关系表时除外(。另一侧(相反(是在关系注释中具有mappedBy属性的一侧。

最重要的是,您需要在JPA中从拥有方设置关系。您甚至可以跳过设置team.employees,从数据库的角度来看,效果是一样的。

最新更新