所以我有以下物體:
@Entity
@Table(name = "appointment")
public class Appointment {
@EmbeddedId
private AppointmentId id = new AppointmentId();
@ManyToOne(fetch = FetchType.LAZY)
@MapsId("customerId")
@JoinColumn(name = "customerId") //Remember to use joincolumns to rename the generated column is spring creates it
private Customer customer;
@ManyToOne(fetch = FetchType.LAZY)
@MapsId("barberId")
@JoinColumn(name = "barberId") //Remember to use joincolumns to rename the generated column is spring creates it
private Barber barber;
@Column(name = "notes")
private String notes;
public Appointment(Customer customer, Barber barber, String notes) {
this.customer = customer;
this.notes = notes;
this.barber = barber;
}
public Appointment() {
}
@JsonBackReference
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
public String getNotes() {
return notes;
}
public void setNotes(String notes) {
this.notes = notes;
}
@Override
public String toString() {
return "Appointment{"
", notes='" notes '\''
'}';
}
}
這是AppointmentId:
@SuppressWarnings("serial")
@Embeddable
public class AppointmentId implements Serializable {
private Integer barberId;
private Integer customerId;
}
現在我正在學習一個教程,在該教程中我設法完成了我想做的事情。關鍵是添加@EmbeddedId注釋并創建 AppointmentId 類。
我目前在資料庫的“約會”表中有 3 條記錄。每條記錄都有一個主鍵,其值分別為1、2、3。
我正在嘗試獲取第二條記錄,因此我使用的是.findById(2)來自我的介面的擴展方法,JpaRespository<Appointment, Integer>但出現以下錯誤。我不確定為什么:
org.hibernate.TypeMismatchException: Provided id of the wrong type for class com.gogobarber.barber.entities.Appointment. Expected: class com.gogobarber.barber.entities.AppointmentId, got class java.lang.Integer
我的意思是錯誤是自我解釋的..它以 的形式要求 IDAppointmentId但我如何在不知道客戶或理發師 ID 的情況下創建這樣的物件?
編輯 = 這是我的約會表架構:
Table: appointment
Columns:
idappointment int AI PK
notes varchar(255)
barberId int
customerId int
編輯 =
控制器:
@DeleteMapping("/appointment/{appointmentId}")
public void deleteAppointment(@PathVariable String appointmentId)
{
appointmentService.deleteAppointment(appointmentId);
}
服務:
@Transactional
public void deleteAppointment(String appointmentId) {
appointmentRepo.deleteById(Integer.valueOf(appointmentId));
}
uj5u.com熱心網友回復:
我最近有類似的問題。起初我使用了復合主鍵,@EmbeddedId但后來我發現添加額外的抽象級別會增加更多的復雜性,例如使用HashMap或HashSet需要額外的邏輯來表示鍵。所以我的建議是使用內部生成的資料庫 ID 作為鍵,對于唯一的列,只需添加一個唯一的約束:
@Table(uniqueConstraints = { @UniqueConstraint(columnNames = { "barberId", "customerId" }) })
uj5u.com熱心網友回復:
我正在嘗試獲取第二條記錄,因此我正在使用擴展JpaRespository<Appointment, Integer> 的介面中的 .findById(2) 方法,但出現以下錯誤。我不知道為什么
public class Appointment { @EmbeddedId private AppointmentId id = new AppointmentId();
您已將物體 Id 宣告為型別AppointmentId,然后在 JPA 存盤庫中宣告您的 id 是錯誤的Integer
首先將您的存盤庫層架構更正為
JpaRespository<Appointment, AppointmentId>
此外,如果您進行這些更正,您可以在您的存盤庫中宣告只能從 id 的這些屬性之一進行搜索的方法。
您可以在您的存盤庫中宣告將由 spring 資料實作的自定義方法,如果它們具有 spring 資料可以理解的正確名稱。您可以使用以下
List<Appointment> findByIdCustomerId(Integer customerId); <-- will retrieve results only by customerId
List<Appointment> findByIdBarberId(Integer barberId); <-- will retrieve results only by barberId
它以 AppointmentId 的形式要求提供 ID,但是如何在不知道客戶或理發師 ID 的情況下創建這樣的物件?
正如我所見,它構建了appointment將理發師與客戶聯系起來的關系(中間表)。因此,要創建約會,您必須知道將是哪個理發師和哪個客戶。因此,這可以解釋如何創建這樣的物件,例如通過了解客戶您知道 customerId,并且通過了解理發師您知道理發師 ID。
不過,在這里我可以看到您設計中的漏洞。具有特定客戶的特定理發師是否應該在資料庫中只有 1 個約會?或者他們可能有多個未來和過去的約會。如果是這種情況,那么您在Appointment課堂上宣告的 Id是不夠的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/398268.html
上一篇:為什么即使條件在Selenium中的Java中為false也不會執行else塊
下一篇:更新到最新的SpringBoot版本spring-boot-starter-parent2.6.2后,我的測驗停止執行
