拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 如何在不使用embeddedID的情况下检索记录?

如何在不使用embeddedID的情况下检索记录?

白鹭 - 2022-02-16 1986 0 0

所以我有以下物体:

@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但后来我发现添加额外的抽象级别会增加更多的复杂性,例如使用HashMapHashSet需要额外的逻辑来表示键。所以我的建议是使用内部生成的数据库 ID 作为键,对于唯一的列,只需添加一个唯一的约束:

@Table(uniqueConstraints = { @UniqueConstraint(columnNames = { "barberId", "customerId" }) })

uj5u.com热心网友回复:

您已将物体 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

正如我所见,它构建了appointment将理发师与客户联系起来的关系(中间表)。因此,要创建约会,您必须知道将是哪个理发师和哪个客户。因此,这可以解释如何创建这样的物件,例如通过了解客户您知道 customerId,并且通过了解理发师您知道理发师 ID。

不过,在这里我可以看到您设计中的漏洞。具有特定客户的特定理发师是否应该在数据库中只有 1 个约会?或者他们可能有多个未来和过去的约会。如果是这种情况,那么您在Appointment课堂上宣告的 Id是不够的。

标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *