如何在springboot中将包含多个数据的json正文插入到具有关系的多个表中

发布于 2025-01-17 03:56:41 字数 4493 浏览 1 评论 0原文

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

现在,我知道如何通过一个 json 正文插入用户并分别以其关系登录每个用户

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

I have two entities: UserEntity and LoginEntity and crudrepositories for each. The entities have a relationship of OneToOne where one user will have one login account. I also created a controller and I can get all the data from the database i.e when call getalluser I get all users with their relationship to login. and when I call getAllLogins I get all logins accounts. I also managed to insert the user and the login using API each individually and it's working fine but this will omit the foreign-key user_id.

Now since am knew am stack on how to insert the user and login each respectively with their relationships through one 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"
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流云如水 2025-01-24 03:56:41

这个应该效果更好:

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

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

您可以更正此问题,以便在从控制器返回结果之前将结果映射到另一个对象,或者只是从 UserEntity 中删除属性“logins”。

This one should works better :

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

However, you will get an error if your endpoint returns an object that contains a loop inclusion : spring will try to map it into json indefinitely.

You can correct this issue to map your result into another object before returning it from your controller, or just remove the property "logins" from UserEntity.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文