-->
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.  [ 6 posts ] 
Author Message
 Post subject: session.saveorUpdate(this) does a DELETE followed by INSERT!
PostPosted: Thu Apr 22, 2004 5:10 pm 
Beginner
Beginner

Joined: Tue Apr 06, 2004 6:13 pm
Posts: 39
Location: Pune, Maharashtra, INDIA
Hi,

In my hydrated object I have a store method, in which I do a
session.saveOrUpdate(this)

On executing the hibernate log shows 2 queries:

Code:
Hibernate: delete from OFFLINE_PARTNER_PROFILE where PARTNER_ID=?
Hibernate: insert into OFFLINE_PARTNER_PROFILE (PARTNER_ID, PROFILE_ID) values (?, ?)


I expected to see ONE Update query.

Why is this so ?

I am using Hibernate 2.1

_________________
thks
G1


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 22, 2004 9:41 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Really gonna need some more information like the pertinent mappings and what you are doing before saveOrUpdate(this).


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 23, 2004 7:36 am 
Regular
Regular

Joined: Wed Dec 31, 2003 4:26 am
Posts: 108
Location: Berkeley, CA
More info please.

If you're mapping using Set, the semantics of a Set requier the delete and insert. It's covered in the ref. manual.


Top
 Profile  
 
 Post subject: My code is:
PostPosted: Fri Apr 23, 2004 8:58 am 
Beginner
Beginner

Joined: Tue Apr 06, 2004 6:13 pm
Posts: 39
Location: Pune, Maharashtra, INDIA
I am calling Partner.storeProfile().

OfflinePartnerProfile is an ASSOC table between Partner and TransmissionProfile. It has no P.keys but only 2 Foreign Keys
(partnerID pointing to Partner table)
(profileID pointing to TransmissionProfile table)

So OfflinePartnerProfile.java as a OfflinePartnerProfilePK (composite key) as the only attribute

I cannot do a store on Partner directly (read only) so have to do a store
on the TransmissionProfile table followed by a store on the assoc table (OfflinePartnerProfile)

The code is as below :-

/**** storeProfile() is a method in Partner.java ****/
Code:
/**
* @
*/
public void storeProfile() throws PersistenceException {
      try {
         Session session = PersistenceSession.currentSession();
         //session.saveOrUpdate(this);
         for (Iterator iter = offlineProfile.iterator(); iter.hasNext();) {
            TransmissionProfile tp = (TransmissionProfile) iter.next();
            tp.store();
            OfflinePartnerProfilePK oppPK = new OfflinePartnerProfilePK(this.id, tp.getProfileID());
            //oppPK.setPartnerID(this.id);
            //oppPK.setProfileID(tp.getProfileID());
            OfflinePartnerProfile opp = new OfflinePartnerProfile();
            opp.setCompositeKey(oppPK);
            opp.store();
         }
         session.flush();         
      } catch (HibernateException e) {
         throw new PersistenceException(e);
      }
   }



/**** The code of OfflinePartnerProfile.java ****/
Code:
package equicom.offline.partner;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;

import com.equifax.persistence.PersistenceSession;
import com.equifax.persistence.PersistenceException;

import equicom.offline.transmission_profile.TransmissionProfile;
import equicom.offline.utils.BaseDomainObject;

/**
* The hydrated equivalent of the OfflinePartnerProfile assoc table.
*
* @hibernate.class table="OFFLINE_PARTNER_PROFILE"
*
* @author: aru (Jeevan S)
* @version: $Id: v $
*/
public class OfflinePartnerProfile extends BaseDomainObject {
   private OfflinePartnerProfilePK compositeKey;

   /**
    * @param sets
    */
   public void store() throws PersistenceException {
      try {
         Session session = PersistenceSession.currentSession();
         session.saveOrUpdate(this);
         //session.flush();         //RULE of the THUMB (G1),
         //Never call session.flush() if the table being written to is a child of another.
         //Hibernate tries to flush everything (including the parent) which still has not been written too.
      } catch (HibernateException e) {
         throw new PersistenceException(e);
      }
   }

   /**
    * @hibernate.id 
    *
    * @return OfflinePartnerProfilePK
    */
   public OfflinePartnerProfilePK getCompositeKey() {
      return compositeKey;
   }

   /**
    * @param profilePK
    */
   public void setCompositeKey(OfflinePartnerProfilePK profilePK) {
      compositeKey = profilePK;
   }

}

_________________
thks
G1


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 23, 2004 1:42 pm 
Regular
Regular

Joined: Wed Dec 31, 2003 4:26 am
Posts: 108
Location: Berkeley, CA
Show your mapping. Are the profiles mapped as a Set?


Top
 Profile  
 
 Post subject: My mapping
PostPosted: Fri Apr 23, 2004 2:23 pm 
Beginner
Beginner

Joined: Tue Apr 06, 2004 6:13 pm
Posts: 39
Location: Pune, Maharashtra, INDIA
Yes the profile is mapped as a Set.
(Partner to TransmissionProfile is an Many-to-Many relationship)

But as I don't hv access to write to the Partner table, I do not issue a
saveOrUpdate() on the Partner,
but Iterate thru the Set of TransmissionProfiles, invoking saveOrUpdate(), on each one-by-one and then commit the partnerID and profileID to the assoc table.

I guess thats why I get a delete followed by an Insert.
(I think I may not get it if I can do a save() on the Partner itself)

The console output when I invoke the storeProfile method

Code:
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into TRANSMISSION_HOSTS (PORT, HOST_URL, HOST_TYPE, HOST_ID) values (?, ?, '3', ?)
Hibernate: insert into TRANSMISSION_CREDENTIALS (PASSWORD, USERNAME, HOST_ID, CREDENTIAL_ID) values (?, ?, ?, ?)
Hibernate: insert into TRANSMISSION_PROFILE (DESCRIPTION, PURPOSE, PATH, CREDENTIAL_ID, PROFILE_ID) values (?, ?, ?, ?, ?)
Hibernate: delete from OFFLINE_PARTNER_PROFILE where PARTNER_ID=?
Hibernate: insert into OFFLINE_PARTNER_PROFILE (PARTNER_ID, PROFILE_ID) values (?, ?)


The mapping files:
Code:
<hibernate-mapping>
   <class name="equicom.offline.partner.Partner" table="PARTNER" dynamic-update="false" dynamic-insert="false">

      <id name="id" column="PARTNER_ID" type="long">
         <generator class="assigned"></generator>
      </id>

      <property name="code" type="string" update="false" insert="false" column="PARTNER_CD" />

      <property name="isActive" type="yes_no" update="false" insert="false" column="ACTIVE_IND" />

      <property name="name" type="string" update="false" insert="false" column="NAME" />

      <set name="subscribedProducts" table="PARTNER_PROD" lazy="true" inverse="false" cascade="none" sort="unsorted" order-by="PROD_ID ASC" where="CHANNEL_ID=(select cc.channel_id from channel_code cc where cc.channel_cd = 'OFFLINE')">

         <key column="PARTNER_ID" />

         <many-to-many class="equicom.offline.partner.Product" column="PROD_ID" outer-join="false" />

      </set>

      <set name="offlineProfile" table="OFFLINE_PARTNER_PROFILE" lazy="true" inverse="false" cascade="none" sort="unsorted">

         <key column="PARTNER_ID" />

         <many-to-many class="equicom.offline.transmission_profile.TransmissionProfile" column="PROFILE_ID" outer-join="auto" />

      </set>

      <!--
         To add non XDoclet property mappings, create a file named
         hibernate-properties-Partner.xml
         containing the additional properties and place it in your merge dir.
      -->

   </class>


_________________
thks
G1


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.