我有3個表:JPA更新不工作一個多一對多的關係
customer(idCustomer,...)
is_managed(idCustomer,idPerson)
sales_person(idPerson,...)
有customer
之間的關係@ManyToMany
和sales_person
。
當我執行刪除時,它工作正常:刪除customer
,is_managed
和sales_person
。
但是,當我執行更新,然後customer
和is_managed
被更新,但sales_person
沒有。
例如,如果我通過刪除sales_person
更新customer
,它將在表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",
[email protected](name="idCustomer"),
[email protected](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;
...
...
請發佈相關代碼 – kostja 2013-04-05 13:56:05
如果您展示瞭如何在JPA中定義關係,則對JPA用戶更有幫助。 – BalusC 2013-04-05 14:03:26
您的實體如何註釋?你使用什麼級聯行爲?你使用orphanRemoval嗎?很多問題都可以通過向我們展示實體代碼(或DD,如果您使用它)來回答...... – 2013-04-05 14:04:28