gpt4 book ai didi

java - 如何配置Spring Boot一对多实体关系

转载 作者:行者123 更新时间:2023-12-01 17:41:19 24 4
gpt4 key购买 nike

在我的 REST API 中,我有带有 @Entity 注释的 RoleRolePrivilege 实体。每个实体中有一些列。我需要与这两者建立一对多的关系。一个角色可以拥有多个角色权限,一个角色权限只能拥有一个角色。在我的数据库脚本文件中,我将其设置为

-- -----------------------------------------------------
-- 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接口(interface)将新条目保存到数据库。

@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";
}
}

最后我使用了Controller文件,

// ---------------------------------
// 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工作台检查它时,roleId列为空。

最佳答案

您在 RolePrivilege 中有一个原始的 @JoinColumn,请更改它,以便配置列的名称:@JoinColumn(name = "roleId")

此外,您还保存了 RolePrivilege,但更改不是级联的,请将映射更改为:

@ManyToOne(cascade = CascadeType.ALL)

P.S.:在 performance reasons 的多对多映射中,优先选择 List 而不是 Set .

关于java - 如何配置Spring Boot一对多实体关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60750982/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com