gpt4 book ai didi

mysql - 从哪里检索列表?

转载 作者:行者123 更新时间:2023-11-29 09:32:38 25 4
gpt4 key购买 nike

例如,我有 2 个实体:客户、预订。一对多的双向关系。一位客户对多位预订,其中预订是所有者。当我想获得一位客户的所有预订时,我该怎么做?也许通过查询并从对象中使用 client.getReservations() 找到客户端?或者可以进行查询以查找 id 所在的预订。

客户端类:

ublic class Client extends Person {

@OneToMany(mappedBy = "client", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Reservation> reservations = new ArrayList<>();

public Client() {

}

public Client(String fullName, String phoneNumber, String email, String address) {

super(fullName, phoneNumber, email, address);
}

public List<Reservation> getReservations() {
return reservations;
}

public void addReservation(Reservation reservation) {
this.reservations.add(reservation);
reservation.setClient(this);
}

public void removeReservation(Reservation reservation) {
this.getReservations().remove(reservation);
reservation.setClient(null);

}

预订舱位:

public class Reservation {

// --- attributs ---
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "reservation_id")
private Integer id;

@Column(name = "created_on")
@Temporal(TemporalType.DATE)
@CreationTimestamp
private Date createdOn;

@Temporal(TemporalType.DATE)
@Column(name = "last_updated")
@UpdateTimestamp
private Date lastUpdated;

@Temporal(TemporalType.DATE)
@Column(name = "date_to_supply")
private Date dateToSupply;

@Column(name = "total_sum")
private double totalSum;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "client_id")
private Client client;

最佳答案

在一对多关系中,当您在示例中加载客户端时,它将加载带有所有预留的客户端。如前所述 client.getReservations()。将获得该特定客户的所有预订。

关于mysql - 从哪里检索列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58363250/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com