簡體   English   中英

Spring 不在數據庫中保存多對多映射

[英]Spring not saving many-to-many mapping in DB

我有 2 個實體。 約會和項目。 它們都是獨立的,我們可以在多個約會中擁有多個項目。

預約class:

@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;

和項目 class:

@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 controller里面還有一個補丁方法。

@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 的列表中添加了一項,但保存不會 flash 將更改保存到數據庫中。

在此處輸入圖像描述

這是數據庫:

數據庫

知道我缺少什么嗎? 非常感謝,女士們,先生們。

這是因為Item class 自己的關系。

如果您已經在Appointment class 中描述了關系,並在Item class 中使用了mappedBy ,則不會遇到此問題。 發生這種情況是因為 Hibernate 使用 class 來定義關系以維護其關聯。

要解決此問題,您應該通過以下方式調整實體:

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上得到了回答, 鏈接

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM