【问题标题】:How to instance 2 different object with only one HTTP request (JSON file)如何仅使用一个 HTTP 请求(JSON 文件)实例化 2 个不同的对象
【发布时间】:2019-02-19 13:01:12
【问题描述】:

我用 Java Spring 和 Hibernate 创建了一个数据库。有一个责任实体和一个帐户实体,它们与 OneToOne 关系链接。 这是我的代码:

REST 控制器:

@RestController
@RequestMapping("/test")
public class test {

    @Autowired
    private final ResponsableDBRepository responsableDBRepository;

    public test(ResponsableDBRepository responsableDBRepository){
        this.responsableDBRepository = responsableDBRepository;
    }

    @RequestMapping(method = RequestMethod.POST)
    ResponseEntity<?> insert(@RequestBody ResponsableEntity responsableEntity) {
        responsableDBRepository.save(responsableEntity);
        return ResponseEntity.accepted().build();
    }
}

责任实体:

@Entity
@Table(name = "responsable")
public class ResponsableEntity {

    /**
     * Id of the resident
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    /**
     * First name of the responsable
     */
    @Column(nullable=false)
    private String firstName;

    /**
     * Lst name of the responsable
     */
    @Column(nullable=false)
    private String lastName;

    /**
     * Account id of the responsable
     */
    //@Column(nullable=false)
    @OneToOne
    @JoinColumn(name = "account", referencedColumnName = "id")
    private AccountEntity account;

账户实体:

@Entity
@Table(name = "account")
public class AccountEntity {

    /**
     * Id of the acount
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    /**
     * Username of the acount
     */
    @Column(nullable=false)
    private String username;

    /**
     * Password of the account
     */
    @Column(nullable=false)
    private String password;

我想要做的是向我的控制器 (/test) 发送一个 POST HTTP 请求并发送一个 JSON 来创建一个帐户并负责并将两者链接到数据库中

这是我为学校准备的一个项目,这是我第一次使用 Spring 和 Hibernate,所以我无法说出它是否可能以及如果可能的话如何编写 JSON

我已经尝试了一点,但没有任何效果,我在互联网上找不到答案

提前感谢您的回答!

【问题讨论】:

  • 您可以创建一个包装器对象来包装两者,并将该对象作为 JSON 正文发送。
  • 是的,最好的方法是创建一个包装器 DTO(数据传输对象),然后从那里开始工作。
  • 他为什么需要一个包装对象?一个对象已经包含另一个对象。

标签: java json spring hibernate rest


【解决方案1】:

您没有提供ResponsableDBRepository 的代码,但我认为它在ResponsableEntity 实例上的某个地方调用了persist

如果您的问题是您从ResponsableEntity 引用的AccountEntity 没有被保存,那么您的问题可能是您没有告诉Hibernate 将持久操作从ResponsableDBRepository 级联到AccountEntity。您至少需要使用@OneToOne(cascade = CascadeType.PERSIST),但您可能需要CascadeType.ALL

【讨论】:

    【解决方案2】:

    您只需在POST requestBody 部分发送对象。这是一个示例,尝试使用以下 Body (raw/JSON (application/json) ) 向您的 url 发出 POST 请求(使用 Postman 或任何类似工具)

    {
        "firstName": "your_first_name",
        "lastName": "your_last_name",
        "account": 
            {
    
                "username": "account_username",
                "password": "account_password"
            }
    
    }
    

    如果您希望与父实体 (ResponsableEntity) 一起保留子实体 (Account),您的模型必须如下所示

    //ResponsableEntity  class
    @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name = "id")
    private Account account;
    
    //Account class
    @OneToOne(mappedBy = "account", cascade = CascadeType.ALL,
    fetch = FetchType.LAZY, optional = false)
    private ResponsableEntity  responsableEntity;
    

    请记住,正如 Willis Blackburn 所说,您可能只需要使用 cascade = CascadeType.PERSIST 而不是 all(ALL 包括 PERSIST 类型)。

    你可以找到更多关于级联类型here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-12
      • 2021-11-21
      • 1970-01-01
      相关资源
      最近更新 更多