我正在學習 SpringBoot,為此我嘗試在問題的幫助下實作我所學的知識。
這是基本問題:我們有 2 個物體:
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是正確處理body。 //請提出更好的 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);
}
但我只添加了員工和團隊,但沒有添加外鍵。
任何人請幫助我我做錯了什么。并告訴你做事的好方法。
uj5u.com熱心網友回復:
你需要做body.getDevelopers().forEach(d -> d.setTeam(team))。
為什么?在 JPA 術語中,員工是關系的擁有方。這是違反直覺的(就像在日常使用中可以說“一個團隊有員工”)。但是在 JPA 中,擁有方是(在這里簡化)具有外鍵的一方(使用關系表時例外)。另一面,相反,是mappedBy在關系注釋中具有屬性的那一面。
最重要的是,您需要在 JPA 中從擁有方設定關系。您甚至可以跳過設定team.employees,從資料庫的角度來看,效果是一樣的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/418752.html
標籤:
上一篇:當nvarchar(50)不以數字字母結尾時的SQLServerCASE
下一篇:多個字符與LIKE子句匹配
