-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Hibernate makes delete before Insert in Many-to-Many
PostPosted: Thu Oct 13, 2005 11:23 am 
Regular
Regular

Joined: Sat Sep 03, 2005 9:07 am
Posts: 87
Location: Graz, AUSTRIA
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!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 14, 2005 3:02 am 
Regular
Regular

Joined: Sat Sep 03, 2005 9:07 am
Posts: 87
Location: Graz, AUSTRIA
Nobody an idea, where the problem is and where the delete comes from???


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.