伙计们 !如何从 Parent -> Child List 中删除项目?这是我的情况。
控制器
@RequestMapping(value = "/delete/{distributorId}/{exhibitorId}", method = RequestMethod.GET)
public String deleteExhibitor(Model model, @PathVariable("distributorId") Integer distributorId,
@PathVariable("exhibitorId") Integer exhibitorId) {
Distributor distributor = distributorService.getById(distributorId);
distributor.getExhibitor().remove(exhibitorId);
distributorService.update(distributor);
return "redirect:/";
}
和分销商(父)
@Entity
@Table(name = "distributor")
public class Distributor {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
@Column(name = "id")
private Integer id;
@Column(name = "name")
private String name;
@Column(name = "city")
private String city;
@Column(name = "address")
private String address;
@LazyCollection(LazyCollectionOption.FALSE)
@OrderColumn(name="orders_index")
@OneToMany(cascade = CascadeType.ALL, orphanRemoval=true)
List<Exhibitor> exhibitor = new ArrayList<Exhibitor>();
@LazyCollection(LazyCollectionOption.FALSE)
@OneToMany(cascade = CascadeType.ALL, orphanRemoval=true)
List<Merchandiser> merchandiser = new ArrayList<Merchandiser>();
Getters and setters..
我从 url 获取 Distributor Id,然后使用 getByID,获取正确的 Distributor 对象,其中包含我要删除的 Exhibitor..