0

伙计们 !如何从 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..

4

1 回答 1

0

List 的 remove 方法适用于对象的 equals 方法实现。做一些这样的事情。

Iterator x = distributor.getExhibitor().iterator();
while(x.hasNext()){
    Exhibitor ex = x.next();
    if(ex.getExhibitorId()!=null && ex.getExhibitorId().equals(exhibitorId)){
         x.remove();
         break;
     }
}
于 2016-09-05T16:32:30.490 回答