![](/img/trans.png)
[英]Spring boot JPA: How to keep the child entity when deleting the parent entity in a many-to-one relationship?
[英]How to configure Spring Boot one to many entity relationship
在我的其余 API 中,我有帶有@Entity
注釋的Role
和RolePrivilege
實體。 每個實體中都有一些列。 我需要和這兩個設置一對多的關系。 一個角色有多個角色權限,一個角色權限只能有一個角色。 在我的數據庫腳本文件中,我將其設置為
-- -----------------------------------------------------
-- Table `wdc`.`role`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `wdc`.`role` (
`roleId` INT NOT NULL AUTO_INCREMENT ,
`roleName` VARCHAR(45) NOT NULL ,
`disabled` INT(1) ZEROFILL NULL ,
PRIMARY KEY (`roleId`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
-- -----------------------------------------------------
-- Table `wdc`.`rolePrivileges`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `wdc`.`rolePrivileges` (
`privilegeId` INT NOT NULL AUTO_INCREMENT ,
`privilegeDescription` VARCHAR(45) NOT NULL ,
`roleId` INT NULL ,
`disabled` INT(1) ZEROFILL NULL ,
`lastModifiedUser` VARCHAR(30) NULL DEFAULT NULL,
`lastModifiedDateTime` DATETIME NULL DEFAULT NULL,
PRIMARY KEY (`privilegeId`),
FOREIGN KEY (`roleId`)
REFERENCES `wdc`.`role` (`roleId`)
ON UPDATE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
這很好地創建了數據庫。 我創建的Role
實體如下,
@Entity
@Table(name = "role", schema = "wdc", catalog = "")
public class Role implements Serializable {
@Id
@Column(name = "roleid", nullable = false, unique = true)
@GeneratedValue(strategy=GenerationType.IDENTITY)
@OneToMany(mappedBy = "role", cascade = CascadeType.ALL)
private Set<RolePrivilege> rolePrivileges;
public Role(int roleId, String roleName, Integer disabled, RolePrivilege rolePrivileges) {
this.roleId = roleId;
this.roleName = roleName;
this.disabled = disabled;
this.rolePrivileges = Stream.of(rolePrivileges).collect(Collectors.toSet());
this.rolePrivileges.forEach(x -> x.setRoleId(this));
}
}
RolePrivilege實體如下,
@Entity
@Table(name = "roleprivileges", schema = "wdc", catalog = "")
public class RolePrivilege implements Serializable {
@Id
@Column(name = "privilegeid")
@GeneratedValue(strategy = GenerationType.IDENTITY)
@ManyToOne
@JoinColumn
private Role role;
public RolePrivilege(int privilegeId, String privilegeDescription, Integer disabled, String lastModifiedUser, Timestamp lastModifiedDateTime) {
this.privilegeId = privilegeId;
this.privilegeDescription = privilegeDescription;
this.disabled = disabled;
this.lastModifiedUser = lastModifiedUser;
this.lastModifiedDateTime = lastModifiedDateTime;
}
}
我使用CrudRepository接口將新條目保存到數據庫。
@Repository
public interface RoleRepository extends CrudRepository<Role, Integer> {
}
@Repository
public interface RolePrivilegeRepository extends CrudRepository<RolePrivilege, Integer> {
}
這是我添加新角色權限的服務文件,
// ---------------------------------
// add new role privilege
// ---------------------------------
public String addRolePrivilege(RolePrivilege rolePrivilege){
try {
rolePrivilegesRepository.save(rolePrivilege);
return "Saved";
} catch (Exception e){
return "Failed";
}
}
上次我使用控制器文件作為這個,
// ---------------------------------
// add new role privilege to database
// ---------------------------------
@PostMapping(path = "/")
public @ResponseBody String addRolePrivilege(@RequestBody RolePrivilege rolePrivilege){
return rolePrivilegesService.addRolePrivilege(rolePrivilege);
}
但是當我嘗試保存新的角色權限時,它仍然會在rolePrivilege表中沒有roleId 的情況下保存。
我是這樣試的
{
"privilegeDescription": "add user",
"role": [
{
"roleId": "1",
"roleName": "user1",
"disabled": 0
}
],
"disabled": 0,
"lastModifiedUser": "1",
"lastModifiedDateTime": "2020-03-11T17:58:14.361+0000"
}
我需要在哪里改變? 請幫我。
當我發送請求以保存新角色權限時,它返回已保存,但是當我使用 mysql 工作台檢查它時, emplty中的roleId列。
你有一個原始@JoinColumn
在RolePrivilege
,改變它,所以列的名稱配置: @JoinColumn(name = "roleId")
此外,您正在保存RolePrivilege
,但更改不是級聯的,請將映射更改為:
@ManyToOne(cascade = CascadeType.ALL)
PS:出於性能原因,更喜歡List
s而不是Set
s in-to-many mapping。
首先,不要返回String(將其包裝到類例如RolePriviligueResponse
以String status
作為響應主體),其次您不需要@ResponseBody
批注,您的@PostMapping
批注已經有了它,第三-不要使用Integer
作為ID,最好使用Long
類型。 並且您沒有提供@JoinColumn(name="roleId")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.