大家好,我想在 JPA 存盤庫中創建一個 HashMap/Map,但我不知道如何。
@Repository
public interface CurrentDeployedReservations extends JpaRepository<Reservation, CustomTable>{
//Map(CustomTable, Reservation) findMap()?
}
謝謝
@SuppressWarnings("serial")
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Reservation implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;
private Boolean accepted;
private Long t_ID;
@OneToOne
@JoinColumn(name = "user_id")
@JsonManagedReference
private User user;
@OneToOne
@JoinColumn(name = "table_id")
@JsonBackReference
private CustomTable table;
private String time;
private int numberOfPeople;
}
@SuppressWarnings("serial")
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class CustomTable implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;
private Boolean busy;
private Boolean full;
@JsonIgnore
@OneToMany(mappedBy = "table", orphanRemoval = true, fetch = FetchType.EAGER)
@JsonManagedReference
private List<Reservation> reservations;
public void addReservation(Reservation r) {
this.reservations.add(r);
r.setTable(this);
}
public void removeReservation(Reservation r) {
r.setTable(null);
this.reservations.remove(r);
}
}
這是模型。謝謝你的慰問 ........................ ...................... ……………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………… ……………………………………………………………………………………………………………………………………………………………………………………………… ……………………………… …………………………………………………………………………………………………………………………………………………………………………
uj5u.com熱心網友回復:
我想你誤解了JpaRepository,用法是:JpaRepository<T,ID>. 在你的情況下,這意味著:
@Repository
public interface CurrentDeployedReservations extends JpaRepository<Reservation, Long> {
}
然后您可以使用它JpaRepository#findAll來獲取所有 Reservations,但這并不等于您的 Map。我仍然不確定為什么當你有這種關系時你需要一個 Map,但下面的方法會起作用(假設你的 CustomTable 有一個用于 Reservation 的 getter):
@Repository
public interface CustomTableRepository extends JpaRepository<CustomTable, Long> {
public Map<CustomTable, List<Reservation>> getAll() {
return findAll().stream()
.collect(Collectors.toMap(c -> c, CustomTable::getReservations));
}
}
您@OneToOne在 ReservationscustomeTable欄位上的關系應該是@ManyToOne。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/393251.html
