Hi
I have two classes: Lista and Adresaci.
When I insert values from jTable Hibernate fills join table with 2 FK (one from Lista, second from Adresaci).
This works fine:
Session session=factory.openSession();
Transaction tx=session.beginTransaction();
Lista list = new Lista();
list.setNazwa(jTextField7.getText());
list.setUwagi(jTextField5.getText());
session.save(list);
int j = jTable1.getRowCount();
for (int i=0;i<j;i++){
String klucz= jTable1.getValueAt(i,0).toString();
int kluczyk = Integer.parseInt(klucz);
Adresaci adresat = (Adresaci) session.get(Adresaci.class,kluczyk);
list.getAdresaci().add(adresat);
adresat.getListy().add(list);
}
tx.commit();
session.close();
but how I should update if for example I inserted two rows:
join table Listy
id_adr id_lista
186 187
185 187
188 187
and I have in jTable1 just 2 rows like:
join table Listy
id_adr id_lista
181 187
188 187
So Hibernate should delete rows: id_adr 186,185 and insert id_adr 181
lista.hbm.xml
<set name="adresaci" table="listy" lazy="true" inverse="true">
<key column="id_lista"/>
<many-to-many column="id_adr" class="Adresaci"/>
</set>
adresaci.hbm.xml
<set name="listy" table="listy">
<key column="id_adr"/>
<many-to-many column="id_lista" class="Lista"/>
</set>
thanks
mk
|