1

我有 3 张桌子:

  • customer(idCustomer,...)
  • is_managed(idCustomer,idPerson)
  • sales_person(idPerson,...)

和之间存在@ManyToMany关系。customersales_person

当我执行删除时,它工作正常:customeris_managedsales_person删除。

但是当我执行更新时,customerandis_managed会更新,但sales_person不会。

例如,如果我customer通过删除更新 a sales_person,它会在is_managed表中删除,但不会在sales_person表中删除。

这是如何引起的,我该如何解决?

以下是相关代码:

// update customer
public String updateCustomer(Customer customer,ArrayList<Sales_person> sales_persons,ArrayList<Involved_group_relation> involved_groups, Macro_market macro_market)throws IOException {

   // insert the sales_person attached to the customer
   ArrayList<Sales_person> sales_personC = new ArrayList<Sales_person>();
   sales_personC.addAll(sales_persons);

    customer.setSalesPersons_BelongTo(sales_personC); // insert in customer the sales_persons

    em.merge(customer);
    return customer.getNameCustomer();
}

// entity customer
@Entity
@Table(name="customer")
public class Customer implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue( strategy = GenerationType.IDENTITY)
    private Long idCustomer;

    private String titleTypeAccount;
    private String nameCustomer;

    /** RELATIONS **/
    // CUSTOMER - SALES_PERSON
    @ManyToMany(
            cascade={CascadeType.ALL}
    )
    @JoinTable(
        name="is_managed",
        joinColumns=@JoinColumn(name="idCustomer"),
        inverseJoinColumns=@JoinColumn(name="idPerson")
    )
    private Collection<Sales_person> salesPersons_BelongTo;
        ...
        ...

// entity sales_person
@Entity
@Table(name="sales_person")
public class Sales_person implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    private Long idPerson;

    private String nameSalesPerson;
    private String jobFunction;
    private String titleOrganization;

    @ManyToMany(
        mappedBy="salesPersons_BelongTo"
    )
    private Collection<Customer> customers;
    ...
    ...
4

1 回答 1

0

如果要从数据库中删除 SalesPerson,则必须在 SalesPerson 上调用 em.remove()。调用 merge() 是不够的。

此外,如果您希望合并 SalesPerson 的更改,您还必须在 SalesPerson 上调用 merge(),或者在 salesPersons_BelongTo 关系上设置级联合并。

还要确保您维护双向关系的双方,而不仅仅是一方。

于 2013-04-08T14:02:56.220 回答