Java 如何持久化两个对象,但仅更新一个对象(如果它已存在)

Java 如何持久化两个对象,但仅更新一个对象(如果它已存在),java,spring,hibernate,jpa,Java,Spring,Hibernate,Jpa,我基本上有一个预订系统,包括预订、员工和客户 我想保留一个预订实体。预订实体有一名员工(将提供服务)和一名客户(进行预订) 我希望能够保留一个预订对象,如果该对象已经存在,则不会在其中创建员工和/或客户。如何使用JPA和hibernate实现这一点 我还希望保留我的当前模式(预订表、员工表、客户表以及预订客户、预订员工之间的关系表…) 以下是预订实体: @Entity @Table(name = "bookings") @EntityListeners(AuditingEntityListene

我基本上有一个预订系统,包括预订、员工和客户

我想保留一个预订实体。预订实体有一名员工(将提供服务)和一名客户(进行预订)

我希望能够保留一个预订对象,如果该对象已经存在,则不会在其中创建员工和/或客户。如何使用JPA和hibernate实现这一点

我还希望保留我的当前模式(预订表、员工表、客户表以及预订客户、预订员工之间的关系表…)

以下是预订实体:

@Entity
@Table(name = "bookings")
@EntityListeners(AuditingEntityListener.class)
public class Booking
{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "booking_id")
    private Long id;

    @Column(name = "title")
    private String title;

    @Column(name = "description")
    private String description;

    @Column(name = "service")
    private String service;

    @NotNull(message = "status is mandatory")
    @Column(name = "status")
    @Enumerated(EnumType.STRING)
    private BookingStatus status;

    @Column(name = "scheduled_at", nullable = false)
    private LocalDateTime scheduledAt;

    @Column(name = "created_at")
    @CreationTimestamp
    private LocalDateTime createdAt;

    @Column(name = "updated_at")
    @UpdateTimestamp
    private LocalDateTime updatedAt;

    @ManyToOne(fetch = FetchType.EAGER, cascade=CascadeType.ALL)
    @JoinTable(name = "booking_customer", joinColumns = {@JoinColumn(name = "booking_id")}, inverseJoinColumns = {@JoinColumn(name = "customer_id")})
    private Customer customer;

    @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.PERSIST)
    @JoinTable(name = "booking_employee", joinColumns = {@JoinColumn(name = "booking_id")}, inverseJoinColumns = {@JoinColumn(name = "employee_id")})
    private Employee employee;

    @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.PERSIST)
    @JoinTable(name = "booking_address", joinColumns = {@JoinColumn(name = "booking_id")}, inverseJoinColumns = {@JoinColumn(name = "address_id")})
    private Address address;
}

以下是客户实体:

@Entity
@Table(name = "customers")
@EntityListeners(AuditingEntityListener.class)
public class Customer
{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "customer_id")
    private Long id;

    @NotBlank(message = "firstName is mandatory")
    @Column(name = "first_name")
    private String firstName;

    @NotBlank(message = "lastName is mandatory")
    @Column(name = "last_name")
    private String lastName;

    @NotBlank(message = "email is mandatory")
    @Column(name = "email_address")
    private String email;

    @NotBlank(message = "phoneNumber is mandatory")
    @Column(name = "phone_number")
    private String phoneNumber;

    @Column(name = "created_at")
    @CreationTimestamp
    private LocalDateTime createdAt;

    @Column(name = "updated_at")
    @UpdateTimestamp
    private LocalDateTime updatedAt;

    @OneToMany(mappedBy = "customer", cascade=CascadeType.ALL, fetch = FetchType.EAGER)
    private List<Booking> bookings;
 }


大多数情况下,预订中的员工实体将是数据库中已经存在的员工,但现在每次我发布此JSON时,它都会在数据库中创建一个新的员工记录。

我建议您提供实体的id字段,hibernate将自动检查和设置。如果没有,它将创建新记录。
但如果不可能,则按照@YoManToMero所述手动检查它

我建议您提供实体的id字段,hibernate将自动检查和设置。如果没有,它将创建新记录。
但如果不可能,则手动检查它,如@YoManToMero所述

您需要找到员工,并将其分配给findEmployee/findCustomer->setEmployee/setCustomer并持久化。如果找不到,则创建employee或customer。您需要找到employee,并将其分配给findEmployee/findCustomer->setEmployee/setCustomer并持久化。如果未找到,请创建员工或客户。
@Entity
@Table(name = "employees")
@EntityListeners(AuditingEntityListener.class)
public class Employee
{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "employee_id")
    private Long id;

    @NotBlank(message = "firstName is mandatory")
    @Column(name = "first_name")
    private String firstName;

    @NotBlank(message = "lastName is mandatory")
    @Column(name = "last_name")
    private String lastName;

    @Column(name = "email_address")
    private String email;

    @NotBlank(message = "phoneNumber is mandatory")
    @Column(name = "phone_number")
    private String phoneNumber;

    @Column(name = "created_at")
    @CreationTimestamp
    private LocalDateTime createdAt;

    @Column(name = "updated_at")
    @UpdateTimestamp
    private LocalDateTime updatedAt;

    @OneToMany(mappedBy = "employee", cascade=CascadeType.ALL, fetch = FetchType.EAGER)
    private List<Booking> bookings;

}

{
    "title": "Haircut & Beards with Aymen",
    "description": "I would like to clean shave my head",
    "service": "COUPE-12 Haircut",
    "status": "PENDING",
    "scheduledAt": "2021-08-10T15:50:05.609",
    "customer": {
        "firstName": "Raz",
        "lastName": "ari",
        "email": "bena97@gpil.com",
        "phoneNumber": "5149455541"
    },
    "employee": {
        "firstName": "Garcon",
        "lastName": "Mansur",
        "email": "mansur@gmail.com",
        "phoneNumber": "541659898"
    },
    "address": {
        "location": "10424 rue du loili, Montreal, H9P1Z5"
    }
}