-->
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: Update table if no row exists
PostPosted: Thu Sep 02, 2010 4:24 pm 
Newbie

Joined: Wed Jul 28, 2010 3:31 pm
Posts: 6
Hello,

I tried looking for an answer to the question I have but I wasn't able to find a straight forward answer anywhere.

Need a description?

I have a table that has the following columns: Dept_Name, Dept_ID, Date_Tmst, Amount.
This table has a composite primary key - Dept_Name, Dept_ID, Date_Tmst.
There's an application that loads this table few times a day reading off of another table. And, at the end of the day this table gets purged (all the rows are deleted). When the application runs for the first time the next morning, it inserts rows for the departments reading off of another table. Second time onwards, the application has to just update the existing rows and if no row exists for a dept. it has to insert. The difference between the consecutive runs of the application is the Date_Tmst column value which changes every half-hour.

What did I try to do?

I have a saveOrUpdateMany(IEntity entity, Collection collection) method to which I pass an entity which can be either oracle or DB2 and a collection of objects needed to be inserted/updated to the table.

What did I find?
I found out that the
Code:
session.saveOrUpdate(object)
method always compares the primary key with the contents in the object.

Question?
Inside the object passed as a parameter to session.saveOrUpdate(), the Dept_Name and the Dept_ID values will be the same but the Date_Tmst value would be changed to what is in the table. When the saveOrUpdate() method searches the table with the values in the object it won't find a row existing with those values. So, it performs an INSERT. But, I would like to update by just looking up at the Dept_Name and Dept_ID.
Is there a way to do that in Hibernate? Is HQL the way?

Code:

public static int saveOrUpdateMany(IEntity entity, Collection collection) throws HibernatePersistException
  {
    if (collection == null)
    {
      throw new HibernatePersistException("null parameter to update");
    }
    int numberObjects = 0;
    if(collection.isEmpty())
    {
      return numberObjects;
    }
    Iterator iter = collection.iterator();
    Session session = null;
    try
    {
      session = getSession(entity);

      Transaction tx = session.beginTransaction();
     
      //prepare to insert or update many by sorting the entities by database
      Map typeMap = prepareMany(iter);

      //Itrerate through the mapping by database type and saveOrUpdate
      iter = typeMap.keySet().iterator();
      while(iter.hasNext())
      {
        String objectType = (String)iter.next();
        Collection many = (Collection)typeMap.get(objectType);
        if(many != null) {
          Iterator objects = many.iterator();
          while(objects.hasNext())
          {
            Object object = objects.next();
            if (session == null)
            {
              if (!(object instanceof IEntity))
              {
                throw new HibernatePersistException("Invalid object to save or update, not an IEntity");
              }
            }

            //saveOrUpdate the object
            session.saveOrUpdate(object);
            numberObjects++;
          }
        }
        tx.commit();

        //Flush transaction
        flush(session, null);
        closeSession(session);
      }
    }
    catch(HibernatePersistException e)
    {
      // just rethrow this exception
      throw e;
    }
    catch(Throwable e)
    {
      String message = "Error saving or updating many records into table: " + e.getMessage();
      Log.error("12", HibernateDAO.class, message, e);
      throw new HibernatePersistException(message, e);
    }
    return numberObjects;



Thanks.


Top
 Profile  
 
 Post subject: Re: Update table if no row exists
PostPosted: Fri Sep 03, 2010 2:00 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
I am not really sure I have understood exactly when you want to insert and when you want to update... Do you want to update the Date_Tmst value for an existing row? If so... you'll probably have to use a batch update HQL or SQL.


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.