-->
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.  [ 5 posts ] 
Author Message
 Post subject: how to execute SQL queries like following
PostPosted: Tue May 11, 2004 10:43 am 
Beginner
Beginner

Joined: Thu Apr 15, 2004 2:01 pm
Posts: 25
Hi,

How to execute queries like select sequence.nextval from dual into hibernate?

Thanks
gita


Top
 Profile  
 
 Post subject: Re: how to execute SQL queries like following
PostPosted: Tue May 11, 2004 10:56 am 
Expert
Expert

Joined: Fri Feb 06, 2004 7:49 am
Posts: 255
Location: Moscow, Russia
gita wrote:
Hi,

How to execute queries like select sequence.nextval from dual into hibernate?

Thanks
gita


I don't know if it is correct way, but in Java code I use net.sf.hibernate.id.SequenceGenerator:
Code:
   /* SequenceGenerator is not serializable, let it be transient with lazy initialization */
   transient private SequenceGenerator codeNumberSeqGenerator = null;

   private NumberFormat codeNumberFormat = null;

   private String generateCodeNumber(Request request)
   {
      String result = null;

      try
      {
         Session session = HibernateUtil.getInstance().currentSession();

         /* lazy initialization */
         if (null == codeNumberSeqGenerator)
         {
            codeNumberSeqGenerator = new SequenceGenerator();

            Dialect dialect = Dialect.getDialect(HibernateUtil.getInstance().getProperties());

            Properties props = new Properties();
            props.put("sequence", ""sq_RequestNumber""); // put your sequence name here

            codeNumberSeqGenerator.configure(Hibernate.LONG, props, dialect);

            codeNumberFormat = NumberFormat.getIntegerInstance();
            codeNumberFormat.setGroupingUsed(false);
            codeNumberFormat.setMinimumIntegerDigits(4);
            codeNumberFormat.setMaximumIntegerDigits(4);
         }

         assert (null != codeNumberSeqGenerator);
         assert (null != codeNumberFormat);

         // all the magic is here :)
         final Long tmp = (Long) codeNumberSeqGenerator.generate((SessionImpl) session, request);

         // format Long result to String according to business reguirements
         result = codeNumberFormat.format(tmp);
         assert (result.length() == 4);
      }
      catch (SQLException e)
      {
         log.error("generateCodeNumber: ", e);
         result = null;
      }
      catch (HibernateException e)
      {
         log.error("generateCodeNumber: ", e);
         result = null;
      }
      finally
      {
         Silencer.closeHibernateSession();
         if (null == result)
            result = "error";
      }

      return result;
   }


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 11, 2004 11:03 am 
Beginner
Beginner

Joined: Thu Apr 15, 2004 2:01 pm
Posts: 25
Hi

Thanks a lot for prompt reply. I'll try the same.

But what about queries which we execute in Oracle like Select sysdate from dual etc. Can we do the same using hibernate?

Thanks
gita


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 11, 2004 11:14 am 
Expert
Expert

Joined: Fri Feb 06, 2004 7:49 am
Posts: 255
Location: Moscow, Russia
gita wrote:
But what about queries which we execute in Oracle like Select sysdate from dual etc. Can we do the same using hibernate?gita


It is not portable approach but you can use Session.createSQLQuery(..)
[url]http://www.hibernate.org/hib_docs/api/net/sf/hibernate/Session.html#createSQLQuery(java.lang.String,%20java.lang.String[],%20java.lang.Class[])[/url]


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 11, 2004 11:18 am 
Beginner
Beginner

Joined: Thu Apr 15, 2004 2:01 pm
Posts: 25
Thanks a lot!!


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