I have a problem with a Many-To-Many Insert. The Problem is, that Hibernate makes a delete before he inserts the new IDs.
I don't know why this is. Perhaps you could help me.
The user in my application can add contacts to a partner via a Popup. When he presses the Save Button, I save the data for the contact as well as write into the association table ( the Primary Key of the Table COntact and the Table Partner)
This is the output by hibernate:-->HERE IS THE DELETE!!!
Code:
Hibernate: select EDITOOLS.hibernate_sequence.nextval from dual
Hibernate: /* insert com.magnasteyr.editool.hibernate.EtContacts */ insert into EDITOOLS.ET_CONTACTS (NAME, PHONE, FAX, EMAIL, PRIMARY, G_ID, CON_ID) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: /* delete collection com.magnasteyr.editool.hibernate.EtEdiPartner.etContactsMany */ delete from EDITOOLS.ET_PA2CON where PA_ID=?
Hibernate: /* insert collection row com.magnasteyr.editool.hibernate.EtContacts.etContactTypeMany */ insert into EDITOOLS.ET_CON2CONTYPE (CON_ID, CON_TYPE_ID) values (?, ?)
Hibernate: /* insert collection row com.magnasteyr.editool.hibernate.EtEdiPartner.etContactsMany */ insert into EDITOOLS.ET_PA2CON (PA_ID, CON_ID) values (?, ?)
Here are my mapping files.
Mapping Contact:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping schema="EDITOOLS" package="com.magnasteyr.editool.hibernate">
<class name="EtContacts" dynamic-update="true" dynamic-insert="true" lazy="false" table="ET_CONTACTS">
<id name="conId" type="long" unsaved-value="null">
<column name="CON_ID" not-null="true" sql-type="NUMBER"/>
<generator class="sequence"/>
</id>
...
<set name="etEdiPartnerMany" table="ET_PA2CON" lazy="true" inverse="true" cascade="none">
<key foreign-key="SYS_C0063178">
<column name="CON_ID" not-null="true" sql-type="NUMBER"/>
</key>
<many-to-many entity-name="com.magnasteyr.editool.hibernate.EtEdiPartner" foreign-key="SYS_C0063226">
<column name="PA_ID" not-null="true" sql-type="NUMBER"/>
</many-to-many>
</set>
</class>
</hibernate-mapping>
The mapping file of table Partner
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping schema="EDITOOLS" package="com.magnasteyr.editool.hibernate">
<class name="EtEdiPartner" dynamic-update="true" dynamic-insert="true" lazy="false" table="ET_EDI_PARTNER">
<id name="paId" type="long" unsaved-value="null">
<column name="PA_ID" not-null="true" sql-type="NUMBER"/>
<generator class="sequence"/>
</id>
...
<set name="etContactsMany" table="ET_PA2CON" lazy="true" cascade="none">
<key foreign-key="SYS_C0063226">
<column name="PA_ID" not-null="true" sql-type="NUMBER"/>
</key>
<many-to-many entity-name="com.magnasteyr.editool.hibernate.EtContacts" foreign-key="SYS_C0063178">
<column name="CON_ID" not-null="true" sql-type="NUMBER"/>
</many-to-many>
</set>
</class>
</hibernate-mapping>
Here is the java method:
Code:
public String addContact() throws Exception {
if (this.b_primary.booleanValue() == true) {
primary = new Long(1);
}
else {
primary = new Long(0);
}
Session session = HibernateUtil.currentSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
EtContacts contacts = new EtContacts();
if(faxvorwahl!=null)
{
EtFaxPrefix faxprefix = (EtFaxPrefix) session.get(
EtFaxPrefix.class, faxvorwahl);
contacts.setFaxId(faxprefix);
}
if(telvorwahl!=null)
{
EtPhonePrefix phoneprefix = (EtPhonePrefix) session.get(
EtPhonePrefix.class, telvorwahl);
contacts.setPhoneId(phoneprefix);
}
EtGendertype gender = (EtGendertype) session.get(
EtGendertype.class, geschlechtId);
contacts.setgId(gender);
contacts.setName(contactname);
contacts.setPhone(telefon);
contacts.setFax(fax);
contacts.setEmail(email);
contacts.setPrimary(primary);
EtContactType type = (EtContactType) session.get(
EtContactType.class, contacttype);
contacts.setEtContactTypeMany(new HashSet());
contacts.getEtContactTypeMany().add(type);
type.setEtContactsMany(new HashSet());
type.getEtContactsMany().add(contacts);
session.refresh(edipartner);
edipartner.setEtContactsMany(new HashSet());
edipartner.getEtContactsMany().add(contacts);
contacts.setEtEdiPartnerMany(new HashSet());
contacts.getEtEdiPartnerMany().add(edipartner);
session.save(contacts);
session.save(type);
session.save(edipartner);
session.flush();
tx.commit();
.....
}
Does anybody know, where the delete comes from?THX!