根据另一个实体中存在的 id 获取列表
fetch list based on id present in another entity
这是我的订单实体,
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Entity
@Table(name = "ordertab")
public class Order {
@Id
private int orderId;
private String orderDate;
@ManyToMany(targetEntity = Medicine.class,cascade = CascadeType.ALL)
@JoinTable(name="ord_med",
joinColumns = {@JoinColumn(name="ord_id")},
inverseJoinColumns = {@JoinColumn(name="med_id")})
private List<Medicine> medicineList;
private String dispatchDate;
private float totalCost;
@ManyToOne(targetEntity = Customer.class,cascade = CascadeType.ALL)
@JoinColumn(name= "custord_fk",referencedColumnName = "customerId")
private Customer customer;
private String status;
}
and this is my medicine entity,
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Entity
public class Medicine {
@Id
private String medicineId;
private String medicineName;
private float medicineCost;
private LocalDate mfd;
private LocalDate expiryDate;
**@ManyToMany(cascade = CascadeType.ALL, mappedBy = "medicineList")
private List<Order> orderList;** //order/ medicine many to many mapping
// OneToOne Mapping
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "categoryId", referencedColumnName = "categoryId")
private Category category;
在我的订单服务接口中,我有一个方法,
List showAllOrder(string medId);
我必须获取所有具有匹配 med id 的订单。
这个多对多映射创建了一个额外的 table ord_med,其中有两列名为 ord_id、med_id(类型外键)。除此之外,由于这种双向映射(我相信它是),在创建医学实体对象时它要求我添加订单列表,如何处理这个方法或者我应该如何解决这个问题。谢谢。
在您的 OrderRepository 中您可以实现这个方法
findByMedicineId(String id);
如果我去找 findByMedicineId(String id);
它给出错误说在 Order 实体中找不到 属性 medicineId,因为 属性 medicineId 在 Medicine 实体中,同时在存储库中定义自定义方法遵循规则,参考 https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods.query-creation
无论如何我已经找到了解决方案,
public List<Order> getOrderListBasedOnMedicineId(String medicineid) {
Optional<Medicine> med=medicineRepo.findById(medicineid);//find if medicine is present in database with the id.
if(med.isEmpty()) {
return null;
}
List<Order> orders = medicineServ.getOrderList(); //getorderlist defined in service implementation of medicine.
List<Order> ordersWithMedId = new ArrayList();//new list to add all orders that has atleast one medicineId that matches.
for(int i=0;i<orders.size();i++) {
List<Medicine> medicines= orders.get(i).getMedicineList();
for(int j=0;j<medicines.size();j++) {
ordersWithMedId.add(orders.get(i));
}
}
return ordersWithMedId;//returning the list of orders.
}
@Override
public List<Order> getOrderList() {//medicine service implementation
return orderRepo.findAll();
}
//订单控制器
@GetMapping("/orders/list/{id}")
public ResponseEntity<List<Order>> getOrderListBasedOnMedicineId(@PathVariable("id") String id) {
List<Order> ord= orderService.getOrderListBasedOnMedicineId(id);
if(ord==null) {
throw new OrderNotFoundException("Order not found with medicine id:"+id);
}
return new ResponseEntity<List<Order>>(orderService.getOrderListBasedOnMedicineId(id),HttpStatus.OK);
}
这是我的订单实体,
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Entity
@Table(name = "ordertab")
public class Order {
@Id
private int orderId;
private String orderDate;
@ManyToMany(targetEntity = Medicine.class,cascade = CascadeType.ALL)
@JoinTable(name="ord_med",
joinColumns = {@JoinColumn(name="ord_id")},
inverseJoinColumns = {@JoinColumn(name="med_id")})
private List<Medicine> medicineList;
private String dispatchDate;
private float totalCost;
@ManyToOne(targetEntity = Customer.class,cascade = CascadeType.ALL)
@JoinColumn(name= "custord_fk",referencedColumnName = "customerId")
private Customer customer;
private String status;
}
and this is my medicine entity,
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Entity
public class Medicine {
@Id
private String medicineId;
private String medicineName;
private float medicineCost;
private LocalDate mfd;
private LocalDate expiryDate;
**@ManyToMany(cascade = CascadeType.ALL, mappedBy = "medicineList")
private List<Order> orderList;** //order/ medicine many to many mapping
// OneToOne Mapping
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "categoryId", referencedColumnName = "categoryId")
private Category category;
在我的订单服务接口中,我有一个方法, List showAllOrder(string medId); 我必须获取所有具有匹配 med id 的订单。 这个多对多映射创建了一个额外的 table ord_med,其中有两列名为 ord_id、med_id(类型外键)。除此之外,由于这种双向映射(我相信它是),在创建医学实体对象时它要求我添加订单列表,如何处理这个方法或者我应该如何解决这个问题。谢谢。
在您的 OrderRepository 中您可以实现这个方法
findByMedicineId(String id);
如果我去找 findByMedicineId(String id); 它给出错误说在 Order 实体中找不到 属性 medicineId,因为 属性 medicineId 在 Medicine 实体中,同时在存储库中定义自定义方法遵循规则,参考 https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods.query-creation
无论如何我已经找到了解决方案,
public List<Order> getOrderListBasedOnMedicineId(String medicineid) {
Optional<Medicine> med=medicineRepo.findById(medicineid);//find if medicine is present in database with the id.
if(med.isEmpty()) {
return null;
}
List<Order> orders = medicineServ.getOrderList(); //getorderlist defined in service implementation of medicine.
List<Order> ordersWithMedId = new ArrayList();//new list to add all orders that has atleast one medicineId that matches.
for(int i=0;i<orders.size();i++) {
List<Medicine> medicines= orders.get(i).getMedicineList();
for(int j=0;j<medicines.size();j++) {
ordersWithMedId.add(orders.get(i));
}
}
return ordersWithMedId;//returning the list of orders.
}
@Override
public List<Order> getOrderList() {//medicine service implementation
return orderRepo.findAll();
}
//订单控制器
@GetMapping("/orders/list/{id}")
public ResponseEntity<List<Order>> getOrderListBasedOnMedicineId(@PathVariable("id") String id) {
List<Order> ord= orderService.getOrderListBasedOnMedicineId(id);
if(ord==null) {
throw new OrderNotFoundException("Order not found with medicine id:"+id);
}
return new ResponseEntity<List<Order>>(orderService.getOrderListBasedOnMedicineId(id),HttpStatus.OK);
}