Spring Boot:保存一对多的json请求,外键不会自动保存

时间:2017-08-14 12:21:12

标签: json spring rest spring-boot one-to-many

我有2个实体,角色和资源。角色可以有很多资源。

@Entity
public class Resource {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;

@Column(name="firstname")
private String firstName;

@Column(name="lastname")
private String lastName;

private String email;

@ManyToOne
@JoinColumn(name="roleId", nullable = false)
private Role role;

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}


public Role getRole() {
    return role;
}

public void setRole(Role role) {
    this.role = role;
}

}

@Entity
public class Role {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;

@Column(name = "rolename")
private String roleName;

@OneToMany(mappedBy = "role", cascade = CascadeType.ALL)
private List<Resource> resources;

public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}

public String getRoleName() {
    return roleName;
}
public void setRoleName(String roleName) {
    this.roleName = roleName;
}
public List<Resource> getResources() {
    return resources;
}
public void setResources(List<Resource> resources) {
    this.resources = resources;
}

}

我试图保存一个包含资源的Role对象。这是邮差中我的json的身体。

{
"roleName" : "Business Analyst",
"resources" : [{
    "firstName" : "John",
    "lastName" : "Doe",
    "email" : "John@Doe.com"
}]
}

post post中的http post电话:

http://localhost:8080/app/admin/roles/role

角色控制器

@RestController
@RequestMapping(value="/admin/roles")
public class RoleController {
@Autowired 
private RoleService roleService;

private static final Logger log = LoggerFactory.getLogger(RoleController.class);

@RequestMapping(value="/role", method = RequestMethod.POST)
public ResponseEntity<?> addRole(@RequestBody Role role, UriComponentsBuilder ucBuilder){
    log.info("Adding Role {}" + role);
    log.info("Adding Rolename:" + role.getRoleName());

    roleService.addRole(role);
    HttpHeaders headers = new HttpHeaders();
    headers.setLocation(ucBuilder.path("/admin/roles/role/{id}").buildAndExpand(role.getId()).toUri());
    return new ResponseEntity<String> (headers,HttpStatus.CREATED);
}

@RequestMapping(value="role", method = RequestMethod.GET)
public ResponseEntity<List<Role>> listAllRoles(){
    List<Role> roles = roleService.getAllRoles();
    return new ResponseEntity<List<Role>>(roles, HttpStatus.OK);
}

}

RoleRepository

public interface RoleRepository extends CrudRepository<Role, Integer> {

}

RoleService

public interface RoleService {

public void addRole(Role role);


}

RoleServiceImpl

@Service
public class RoleServiceImpl implements RoleService {

@Autowired
private RoleRepository roleRepository;

@Override
public void addRole(Role role) {
    roleRepository.save(role);
}
}

发生的事情是,Business Analyst的角色保存在Role表的roleName字段中。所述行的id是自动生成的。同时,firstName = John,lastName = Doe和email = John@Doe.com的资源将保存在Resource表中。

但是,role_id不会自动保存在Resource表中,所以现在它为null(表Resource将role_id设置为nullable)。我期待当我执行json帖子时,数据将自动保存在Role表和Resource表中。除了没有保存role_id之外,这两种情况都在发生。我错过了什么?

1 个答案:

答案 0 :(得分:3)

更改addRole,如下所示:

public void addRole(Role role) {


    for(Resource resource: role.getResources()){        
        resource.setRole(role); 
    }

    roleRepository.save(role);
}