休眠从列表中删除项目。

Hibernate remove item from List.

伙计们! 如何从 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 获取分销商 ID,接下来使用 getByID,获取正确的分销商对象,​​其中包含我要删除的参展商..

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;
     }
}