【问题标题】:Saving data to Mysql with SpringBoot使用 Spring Boot 将数据保存到 Mysql
【发布时间】:2021-04-02 13:55:37
【问题描述】:

请考虑以下场景

我希望能够按 ID 搜索员工并获取员工详细信息,以及与该员工关联的联系详细信息。所有者也一样。将是相同的工作。

我想搜索电话号码并找到员工或所有者并获取与该记录相关的所有详细信息。

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@MappedSuperclass
public abstract class BaseEntity implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private Long id;

    public boolean isNew() {
        return this.id == null;
    }
}

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@MappedSuperclass
public class Person extends BaseEntity {
    private String firstName;
    private String lastName;
    // maybe other fields
}

public class Owner extends Person{
    private String emailAddress;
    private String mobileTelNo;
    private String homeTelNo;
    private String workTelNo;
        @OneToOne(cascade = {CascadeType.MERGE, CascadeType.PERSIST}, fetch = FetchType.EAGER)
    private Employee employee;
}

@Data
@Entity
@AllArgsConstructor
@NoArgsConstructor
    @OneToOne(cascade = {CascadeType.MERGE, CascadeType.PERSIST})
    private ContactDetails contactDetails;

@Data
@Entity
@AllArgsConstructor
@NoArgsConstructor
public class ContactDetails extends BaseEntity {
    private String emailAddress;
    private String mobileTelNo;
    private String homeTelNo;
    private String workTelNo;
    @OneToOne(cascade = {CascadeType.MERGE, CascadeType.PERSIST}, fetch = FetchType.EAGER)
    private Employee employee;
}
}


public interface UsersRepository extends JpaRepository<Employee, Integer> {
}

@RestController
@RequestMapping(value = "/rest/users")
public class UsersController {

    @Autowired
    UsersRepository usersRepository;

    @GetMapping(value = "/all")
    public List<Employee> getAll() {
        return usersRepository.findAll();
    }

    @PostMapping(value = "/load")
    public List<Employee> persist(@RequestBody final Employee employee) {
        usersRepository.save(employee);
        return usersRepository.findAll();
    }

}


我如何映射我的实体来实现这个结果?

更新

使用以下json时


I would like to save a record like this using the /load endpoint

{
   "id": 2,
   "firstName": "Scilla",
   "lastName": "Biffolia",
   "middleName": "None",
   "alias": "Cr",
   "dateOfBirth": "2020-02-12",
   "gender": "male",
   "contactDetails": {
       "emailAddress": "scilla@stack.com"
   }
}

但是我得到了这个结果


[
   {
       "id": 2,
       "firstName": null,
       "lastName": null,
       "middleName": null,
       "alias": null,
       "dateOfBirth": null,
       "gender": null,
       "contactDetails": {
           "id": 8,
           "emailAddress": "scilla@stack.com",
           "mobileTelNo": null,
           "homeTelNo": null,
           "workTelNo": null,
           "employee": null,
           "new": false
       },
       "new": false
   }
]

我可以看到我的 CustomerDetails 表已更新。

我做错了什么?

【问题讨论】:

    标签: java mysql spring-boot hibernate jpa


    【解决方案1】:

    还有比这更好的方法吗? 根据上一个答案。

       @Data
       @Entity
       @AllArgsConstructor
       @NoArgsConstructor
       @Builder
       public class Owner extends Person {
       
           @OneToOne(cascade = {CascadeType.MERGE, CascadeType.PERSIST})
           private ContactDetails contactDetails;
       }
       
       @Data
       @Entity
       @AllArgsConstructor
       @NoArgsConstructor
       public class ContactDetails extends BaseEntity {
           private String emailAddress;
           private String mobileTelNo;
           private String homeTelNo;
           private String workTelNo;
       
           @OneToOne(mappedBy = "contactDetails", fetch = FetchType.EAGER)
           @JsonIgnore
           private Owner owner;
       }
           
    

    【讨论】:

      猜你喜欢
      • 2020-02-12
      • 1970-01-01
      • 2016-05-18
      • 1970-01-01
      • 2018-05-12
      • 1970-01-01
      • 2018-05-11
      • 2019-08-28
      • 1970-01-01
      相关资源
      最近更新 更多