Сохранение двунаправленного ManyToMany

Вы можете сделать это с помощью счетчика index, renderRow дает индекс в rowID. Например:


           {...}>
            
              
                {rowData.name}
              
            
          
    }
/>

Дайте мне знать, если это поможет. Рабочий пример: https://snack.expo.io/rkC_FUEUV

11
задан beerbajay 19 December 2011 в 10:16
поделиться

4 ответа

The shortest answer seems to be you cannot and it makes sense. In a bidirectional many-to-many association one side must be master and is used to persist changes to the underlying join table. As JPA will not maintain both side of the association, you could end up with a memory situation that could not be reloaded once stored in the database. Example:

A a1 = new A();
A a2 = new A();
B b = new B();
a1.getB().add(b);
b.getA().add(a2);

If this state could be persisted, you would end up with the following entries in the join table:

a1_id, b_id
a2_id, b_id

But upon loading, how would JPA know that you intended to only let b know about a2 and not a1 ? and what about a2 that should not know about b ?

This is why you have to maintain the bidirectional association yourself (and make the above example impossible to get to, even in memory) and JPA will only persist based on the state of one side of it.

16
ответ дан 3 December 2019 в 05:36
поделиться

Вы пытались добавить параметр mappedBy в поле A в классе B, например,

@Entity
class B {
   @ManyToMany(cascade=CascadeType.ALL, mappedBy = "b")
   private List<A> a;
 ..
}
0
ответ дан 3 December 2019 в 05:36
поделиться

Вы указали столбцы обратного соединения?

@Entity
class A {
   @ManyToMany(mappedBy="A", cascade=CascadeType.ALL)
   private List <B> b;
   ..
}

@Entity 
class B { 
   @ManyToMany 
   @JoinTable (
       name="A_B",
       joinColumns = {@JoinColumn(name="A_ID")},
       inverseJoinColumns = {@JoinColumn(name="B_ID")}
   )
   private List<A> a; 
   .. 
} 

Это предполагает, что таблица соединения называется A_B со столбцами A_ID и B_ID.

4
ответ дан 3 December 2019 в 05:36
поделиться

Возможно, в ответе A_M есть небольшая ошибка. На мой взгляд, это должно быть:

   @Entity
   class B { 
   @ManyToMany 
   @JoinTable (
       name="A_B",
       joinColumns = {@JoinColumn(name="B_ID")},
       inverseJoinColumns = {@JoinColumn(name="A_ID")}
   )
   private List a; 
   .. 
}
0
ответ дан 3 December 2019 в 05:36
поделиться
Другие вопросы по тегам:

Похожие вопросы: