SpringBootRESTJPA数据未在数据库中发布

dojqjjoe  于 2021-06-29  发布在  Java
关注(0)|答案(2)|浏览(307)

我正在尝试用spring boot rest api将数据发布到数据库。我有两个实体,user和roll。对于roll,我已经手动将数据发布到数据库中,当我试图通过postman为用户发布数据时,只有null被发布到数据库中。
用户.java

package com.anurag.entities;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="uid")
    private int id;
    private String uName;
    private String uNickName;
    private String uEmail;

    @ManyToOne
    private Roll roll;

}

滚动.java

package com.anurag.entities;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Roll {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)

    @Column(name="rId")
    private int id;
    private String  roll;

    @OneToMany(mappedBy ="roll")
    private  List<User> user;

}

在数据库中它显示如下

user database

uid u_email u_name u_nick_name roll_r_id
1     null  null     null         101

roll database(manually filled data)
r_id    roll
101     admin
102     employee
103    employeer

用户控制器.java

package com.anurag.controllers;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.anurag.entities.User;
import com.anurag.services.UserService;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService service;

    //1. save all
    @PostMapping("/save")
    public ResponseEntity<String> saveAll(@RequestBody User user){
        Integer id = service.saveAll(user);

        return new ResponseEntity<String>(id +"User is saved",HttpStatus.OK);
    }
}

在《 Postman 》中,我发送这样的数据

{
    "uName" : "sabin",
    "uNickName" : "swa",
    "uEmail" : "aadi@gmail.com",
    "roll"   : {"id":101}
}

用户服务.java

package com.anurag.services;

import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.anurag.Repository.UserRepository;
import com.anurag.entities.User;
import com.anurag.exception.UserNotFoundException;

@Service
public class UserServiceImp implements UserService {

    @Autowired
    private UserRepository repo;

    @Override
    public Integer saveAll(User user) {
     Integer id =   repo.save(user).getId(); 
        return id;
    }
}
efzxgjgh

efzxgjgh1#

我认为您需要为user对象中的role添加一个join列。请尝试以下更改。 @ManyToOne @JoinColumn(name="id", referencedColumnName="id") private Roll roll;

v64noz0r

v64noz0r2#

我认为您应该先保存用户,然后再查找和设置 Roll ```
@PostMapping("/save")
public ResponseEntity saveAll(@RequestBody User user, @RequestParam Ineger rollId){
Integer id = service.saveAll(user, rollId);
return new ResponseEntity(id +"User is saved",HttpStatus.OK);
}

 Postman :

http://localhost:8080/save?rollId=

{
"uName" : "sabin",
"uNickName" : "swa",
"uEmail" : "aadi@gmail.com"
}

和服务:

@Override
public Integer saveAll(User user, Integer rollId) {
Roll roll = rollRepo.findById(rollId);
user.setRoll(roll);
Integer id = repo.save(user).getId();
return id;
}

更好地利用 `Integer` 相反 `int` 实体id字段

相关问题