如何在springboot中将具有多个数据的json主体插入到具有关系的多个表中

How to insert a json body with multiple data to multiple tables with relationship in springboot

我有两个实体:UserEntity 和 LoginEntity,每个实体都有 crudrepositories。这些实体具有 OneToOne 关系,其中一个用户将拥有一个登录帐户。我还创建了一个控制器,我可以从数据库中获取所有数据,即当调用 getalluser 时,我让所有用户及其关系登录。当我调用 getAllLogins 时,我得到了所有登录帐户。我还设法分别使用 API 插入用户和登录名,并且工作正常,但这将省略外键 user_id.

现在我知道如何通过一个 json body

分别插入用户和登录他们的关系
@Entity@Table(name="user_table")public class UserEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long uid;

private String fname;
private String lname;

@OneToOne( cascade = CascadeType.ALL, mappedBy = "userEntityFk")
private LoginEntity logins;

public UserEntity() {
    super();
}
public Long getUid() {
    return uid;
}
public void setUid(Long uid) {
    this.uid = uid;
}
public String getFname() {
    return fname;
}
public void setFname(String fname) {
    this.fname = fname;
}
public String getLname() {
    return lname;
}
public void setLname(String lname) {
    this.lname = lname;
}
public LoginEntity getLogins() {
    return logins;
}
public void setLogins(LoginEntity logins) {
    this.logins = logins;
}
public UserEntity(Long uid, String fname, String lname, LoginEntity logins) {
    super();
    this.uid = uid;
    this.fname = fname;
    this.lname = lname;
    this.logins = logins;
}

@Entity @Table(name="login_table") public class LoginEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long lid;

private String username;
private String password;

@OneToOne(cascade = CascadeType.ALL )
private UserEntity userEntityFk;

public LoginEntity() {
    super();
}
public LoginEntity(Long lid, String username, String password, UserEntity userEntityFk) {
    super();
    this.lid = lid;
    this.username = username;
    this.password = password;
    this.userEntityFk = userEntityFk;
}
public Long getLid() {
    return lid;
}
public void setLid(Long lid) {
    this.lid = lid;
}
public String getUsername() {
    return username;
}
public void setUsername(String username) {
    this.username = username;
}
public String getPassword() {
    return password;
}
public void setPassword(String password) {
    this.password = password;
}
public UserEntity getUserEntityFk() {
    return userEntityFk;
}
public void setUserEntityFk(UserEntity userEntityFk) {
    this.userEntityFk = userEntityFk;
}   

}

@Repository 
public interface LoginRepo extends CrudRepository<LoginEntity, Integer> {

} 

@Repository 
public interface UserRepo extends CrudRepository<UserEntity, Integer> {

}

@RestController
@RequestMapping(path="/api")
public class MainController {

@Autowired 
private UserRepo userRepository;

@Autowired
private LoginRepo loginRepository;

//===this works fine i can get all the users after insert
@GetMapping(path="/user_acc")
  public @ResponseBody Iterable<UserEntity> getAllUsers() {
    // This returns a JSON or XML with the users
    return userRepository.findAll();
  }
//================this too works fine after insert
@GetMapping(path="/login_acc")
  public @ResponseBody Iterable<LoginEntity> getAlllogins() {
    // This returns a JSON or XML with the users
    return loginRepository.findAll();
  }
//===adding single user works fine. and it returns user_id
@PostMapping("/user_acc")
public Long addNewUser(@RequestBody UserEntity userz){
    UserEntity ue = userRepository.save(userz);
    return ue.getUid(); 
}

     //===this works but the foreign key not inserted and thats where my problem is
 @PostMapping("/login_acc")
    public LoginEntity addNewLogin(@RequestBody LoginEntity loginz){
        return loginRepository.save(loginz);
    }

}

class UserLogin{ 
   UserEntity myuser; 
   LoginEntity mylogin; 
   
   public UserEntity getMyuser() { 
       return myuser; 
    } 
 
   public void setMyuser(UserEntity myuser) { 
       this.myuser = myuser; 
   } 

   public LoginEntity getMylogin() { 
      return mylogin; 
   } 
    
   public void setMylogin(LoginEntity mylogin) { 
     this.mylogin = mylogin; 
    }

}

result on post http://localhost:8080/api/login_acc. account created but no foreign key
result on post http://localhost:8080/api/login_acc. account created but no foreign key
{
"lid": 1,
"username": "admin1",
"password": "11111",
"userEntityFk": null
}

result on geting all users on get method http://localhost:8080/api/user_acc
{
"uid": 1,
"fname": "hassan",
"lname": "zahor",
"logins": null
}

what i want to post is this body below to multiple tables
{
"fname":"hassan",
"lname":"zahor",
"username": "admin5",
"password": "55555"
}

这个应该效果更好:

{
"fname":"hassan",
"lname":"zahor",
"userEntityFk" : {
   "username": "admin5",
   "password": "55555"
   }
}

但是,如果您的端点 returns 一个包含循环包含的对象,您将收到错误消息:spring 将无限期地尝试将其映射到 json。

您可以更正此问题以将您的结果映射到另一个对象,然后再从您的控制器返回它,或者只是从 UserEntity 中删除 属性“登录”。