简体   繁体   English

休眠从列表中删除项目。

[英]Hibernate remove item from List.

guys ! 伙计们! How can i remove item from Parent - > Child List ? 如何从“家长->子列表”中删除项目? Here is my situation. 这是我的情况。

Controller 控制者

@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:/";
}

And Distributor (Parent) 和发行人(父母)

@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..

I'm getting the Distributor Id from the url, and next using getByID, getting the proper Distributor object, which contains the Exhibitor that i want to delete.. 我从URL获取分发服务器ID,然后使用getByID获取正确的分发服务器对象,该对象包含我要删除的参展商。

List's remove method works on equals method implementation of the object .Do some thing like this. 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;
     }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM