-->
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.  [ 1 post ] 
Author Message
 Post subject: Better way to do this ?
PostPosted: Mon Jul 20, 2009 5:04 am 
Newbie

Joined: Mon Jul 20, 2009 4:57 am
Posts: 1
Hi,

I am currently inserting a row into a database using native SQL, using the createSQLQuery function. I was wondering if there was a better way to get the PK then doing a another query after inserting the data.

My current code it:
Code:
      // insert the item
      qry =   "INSERT INTO item(item_name)" +
            " VALUES(?)";
      q = dbSession.createSQLQuery(qry);
      q.setString(0, item_name);
      q.executeUpdate();

      // get the item id
      qry = "SELECT item_id, item_name from item" +
            " WHERE item_name like ? " +
            " ORDER BY item_id DESC " +
            " LIMIT 0,1";
      q = dbSession.createSQLQuery(qry);
      q.setString(0, item_name);
      Iterator<Object[]> i = q.list().iterator();

      if (i.hasNext()) {
         Object[] rs = i.next();
         this.item_id = (Integer) rs[0];
               }


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

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.