0

我有 2 个实体。约会和项目。它们都是独立的,我们可以在多个约会中拥有多个项目。

约会类:

@Entity(name = "Appointment")
@Table(name = "appointment")
public class Appointment
{
    @Id
    @JsonProperty("appointment_id")
    @Column(name = "appointment_id", nullable = false)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @JsonProperty("date")
    private Date startingTimestamp;

    @ManyToMany(mappedBy = "appointment",
        cascade = CascadeType.ALL)
    private List<Item> collectionOfItems;

    @JsonProperty("duration")
    private int duration;

    @JsonProperty("state")
    private AppoitmentState state;

    @ManyToOne
    @JsonBackReference
    @JoinColumn(name="user_appointment_owner_id", nullable = false)
    @JsonProperty("user_owner")
    private User user;

和项目类:

@Entity
@Table(name = "item")
public class Item
{
    @Id
    @Column(name = "item_id", nullable = false)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long Id;

    @JsonProperty("name")
    private String name;

    @JsonProperty("duration")
    private int duration;

    @ManyToMany(targetEntity = Appointment.class,
        cascade = CascadeType.ALL)
    @JoinTable(name = "appointment_item", joinColumns = { @JoinColumn(name = "item_id") },
        inverseJoinColumns = { @JoinColumn(name = "appointment_id") })
    private List<Appointment> appointment;

    @ManyToMany
    @JsonProperty("set_of_professionals")
    @JsonIgnore
    private Set<Professional> professional;

    @JsonProperty("business_owning")
    private Long business_id;

这里省略了构造函数、getter 和 setter。

Appointment 控制器内部还有一个 patch 方法。

@RequestMapping(value = "appointment/{itemID}", method = RequestMethod.PATCH,  consumes = MediaType.APPLICATION_JSON_VALUE)
ResponseEntity<Appointment> addItem(@PathVariable Long itemID, @RequestBody ObjectNode appointment_id_str)
{
    Long appointment_id = appointment_id_str.get("appoitment_id").asLong();
    Optional<Appointment> targetAppoitment = appointmentService.findById(appointment_id);
    Optional<Item> addedItem = itemService.findById(itemID);
    if (targetAppoitment.isPresent() && addedItem.isPresent())
    {
        Appointment appoitmentInDB = targetAppoitment.get();
        appoitmentInDB.addItem(addedItem.get());
        Appointment savedAppointment = appointmentService.save(appoitmentInDB);
        return new ResponseEntity<>(savedAppointment, HttpStatus.CREATED);
    }
    return new ResponseEntity("", HttpStatus.INTERNAL_SERVER_ERROR);
}

现在,尽管在调试器中看到,已在 appoitment 的列表中添加了一个项目,但保存不会将更改闪存到数据库中。

在此处输入图像描述

这是数据库:

数据库

知道我缺少什么吗?非常感谢,女士们,先生们。

4

1 回答 1

1

这是因为Item类自己的关系。

如果您已经描述了类内部的关系Appointment并在类内部使用mappedByItem您就不会遇到这个问题。发生这种情况是因为 Hibernate 使用定义关系的类来维护它的关联。

要解决此问题,您应该通过以下方式调整实体:

class Appointment {

...
    
@ManyToMany(targetEntity = Item.class,
        cascade = CascadeType.ALL)
    @JoinTable(name = "appointment_item", joinColumns = { @JoinColumn(name = "appointment_id") },
        inverseJoinColumns = { @JoinColumn(name = "item_id") })
    List<Item> collectionOfItems;

...

}

class Item {

...

    @ManyToMany(mappedBy = "collectionOfItems",
        cascade = CascadeType.ALL)
    private List<Appointment> appoitment;

...

}

这个问题已经在stackoverflow上得到了回答,链接

于 2022-02-04T22:49:24.550 回答