删除关联多对多关系

时间:2015-04-13 07:44:08

标签: java hibernate

我在两个对象之间得到了以下映射 - 事件和客户:

//In the customer object
private List<Events> events = new ArrayList<Events>();
@ManyToMany(cascade=CascadeType.ALL,fetch=FetchType.LAZY)
@JoinTable(name="Customers_Events",
    joinColumns = @JoinColumn(name="CUSTOMER_ID", referencedColumnName="PK"),
    inverseJoinColumns = @JoinColumn(name="EVENTS_ID", referencedColumnName="PK")
)
@OrderColumn
public List<Events> getEvents() {
    return events;
}

//in the Events object
private List<Customer> customers = new ArrayList<Customer>();
@ManyToMany(mappedBy="events", fetch=FetchType.LAZY)
public List<Customer> getCustomers() {
    return customers;
}

然后在我的道具中

@Override
public Customer getCustomerWithEvents(long aPk) {
    Customer customer = (Customer) getSession().get(Customer.class, aPk);
    Hibernate.initialize(customer.getEvents());
}

//The same method for events..

在我的控制器中,我只是这样做:

Customer customer = customerService.getCustomerWithEvents(aPk);
Events event = eventsService.getEventWithCustomers(eventPk);
customer.getEvents().remove(event);
event.getCustomers().remove(customer);

customerService.saveOrUpdate(customer);
eventsService.saveOrUpdate(event);

因此代码不会删除关系。当我这样做时:

bool = false;
if(customer.getEvents().contains(event)) {
    bool = true;
}

它总是返回false。好像我无法从列表中删除对象,因为hibernate不能以某种方式匹配它们?关于如何删除多对多关联关系的任何想法?

0 个答案:

没有答案