<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>All About Unidev &#187; Oracle</title>
	<atom:link href="http://blog.unidev.com/index.php/tag/oracle/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.unidev.com</link>
	<description>Custom Software, Website and Mobile Development News</description>
	<lastBuildDate>Wed, 25 Jan 2012 20:54:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Paging the query result</title>
		<link>http://blog.unidev.com/index.php/2009/05/01/paging-the-query-result/</link>
		<comments>http://blog.unidev.com/index.php/2009/05/01/paging-the-query-result/#comments</comments>
		<pubDate>Fri, 01 May 2009 21:00:19 +0000</pubDate>
		<dc:creator>George Zheng</dc:creator>
				<category><![CDATA[General Technology]]></category>
		<category><![CDATA[Oracle]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Sql Server]]></category>

		<guid isPermaLink="false">http://blog.unidev.com/?p=104</guid>
		<description><![CDATA[If you have 5000 products in database, you may like to show them in pages on the screen. It&#8217;s a common request to only retrieve the records on a certain page. This query must be quick and efficient if we are talking about millions of records in a table for an AJAX control. In Oracle, [...]]]></description>
			<content:encoded><![CDATA[<p>If you have 5000 products in database, you may like to show them in pages on the screen. It&#8217;s a common request to only retrieve the records on a certain page. This query must be quick and efficient if we are talking about millions of records in a table for an AJAX control.</p>
<p>In Oracle, we can do this:</p>
<p><code><span style="color: #0000ff;">SELECT * from<br />
(<br />
SELECT *, ROWNUM as rownumber from Product order by name<br />
)<br />
where rownumber between (PageNbr - 1)*PageSize + 1 and PageNbr*PageSize</span></code></p>
<p>As you can see, this query needs to fetch all rows first then narrows down the result. A more efficient query is:</p>
<p><code><span style="color: #0000ff;">select *<br />
from ( select a.*, rownum rnum<br />
from ( select * from product order by name) a<br />
where rownum &lt;= PageNbr*PageSize)<br />
where rnum &gt;= (PageNbr - 1)*PageSize + 1</span></code></p>
<p>Oracle will optimize this query, so it is not as resource intensive as the first one.</p>
<p>In SQL Server 2005, we can do similar query:</p>
<p><code><span style="color: #0000ff;">WITH Temp AS<br />
(<br />
SELECT row_number() OVER (ORDER BY [name]) AS rowNum, * from Product<br />
)<br />
SELECT * FROM Temp<br />
WHERE rowNum between (@PageNbr - 1) * @PageSize + 1 and @PageNbr * @PageSize<br />
ORDER BY [name]</span></code></p>
<p>The query had been optimized in SQL Server as well.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.unidev.com/index.php/2009/05/01/paging-the-query-result/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Calling Oracle Stored Procedure from Hibernate</title>
		<link>http://blog.unidev.com/index.php/2009/03/03/calling-oracle-stored-procedure-from-hibernate/</link>
		<comments>http://blog.unidev.com/index.php/2009/03/03/calling-oracle-stored-procedure-from-hibernate/#comments</comments>
		<pubDate>Tue, 03 Mar 2009 17:26:45 +0000</pubDate>
		<dc:creator>Dhanya James</dc:creator>
				<category><![CDATA[General Technology]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Oracle]]></category>

		<guid isPermaLink="false">http://blog.unidev.com/?p=75</guid>
		<description><![CDATA[While calling Oracle stored procedure from Hibernate  there is couple of rules that we need to follow. There can be only one return value and this must be a reference cursor. This return reference cusor must be the first and only out value for the stored procedure. It is required that stored procedure must return a [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: left;">While calling Oracle stored procedure from Hibernate  there is couple of rules that we need to follow. There can be only one return value and this must be a reference cursor. This return reference cusor must be the first and only out value for the stored procedure. It is required that stored procedure must return a reference cursor to be used from Hibernate.</p>
<p style="text-align: left;">Here is my stored procedure which return book name and ISBN number for a given branch and for a given author</p>
<p style="text-align: left;">CREATE OR REPLACE PROCEDURE SP_LIB_DTL(p_cursor    out sys_refcursor,<br />
                                       in_brnch_cd in number,<br />
                                       in_auth_cd in number)<br />
as<br />
  bookName varchar2(8);<br />
  ISBN     number;<br />
begin<br />
  bookName := null;<br />
  ISBN     := 0;<br />
  open p_cursor for<br />
    select l.book_name, l.isbn_nbr<br />
      into bookName, ISBN<br />
      from LIB_BRNCH_DTL l<br />
     where l.branch_code = in_brnch_cd<br />
     and l.auth_code = in_auth_cd;</p>
<p style="text-align: left;">end;</p>
<p style="text-align: left;">HIbernate Mapping for return class and Named Query:</p>
<p style="text-align: left;">&lt;?xml version=&#8221;1.0&#8243; encoding=&#8221;utf-8&#8243;?&gt;<br />
&lt;!DOCTYPE hibernate-mapping PUBLIC &#8220;-//Hibernate/Hibernate Mapping DTD 3.0//EN&#8221;<br />
&#8220;<a href="http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd</a>&#8220;&gt;<br />
&lt;hibernate-mapping&gt;<br />
&lt;class name=&#8221;com.org.lib.LibraryDetails&#8221;&gt;<br />
&lt;id name=&#8221;ISBN&#8221; type=&#8221;long&#8221; /&gt;<br />
&lt;property name=&#8221;bookName&#8221; type=&#8221;string&#8221; /&gt;<br />
&lt;/class&gt;<br />
 &lt;sql-query name=&#8221;LIB_SP&#8221; callable=&#8221;true&#8221;&gt;<br />
 &lt;return class=&#8221;com.org.lib.LibraryDetails&#8221;&gt;<br />
   &lt;return-property name=&#8221;ISBN&#8221; column=&#8221;isbn_nbr&#8221; /&gt;<br />
   &lt;return-property name=&#8221;bookName&#8221; column=&#8221;book_name&#8221; /&gt;<br />
 &lt;/return&gt;<br />
  {  call SP_LIB_DTL(? , :branchCD ,:authorCD) }<br />
 &lt;/sql-query&gt;<br />
&lt;/hibernate-mapping&gt;</p>
<p style="text-align: left;">Make sure you have used the correct database field name value for the column attribute for the return property mapping. You will get the following error if you don&#8217;t map the correct databse field name</p>
<p style="text-align: left;">could not execute query; bad SQL grammar [{  call SP_LIB_DTL(? , ?) }];<br />
nested exception is java.sql.SQLException: Invalid column name</p>
<p style="text-align: left;">Here is the DAO implementation for executing query and to set the bind parameter values.</p>
<p style="text-align: left;">public  List selectBooks(final BigDecimal branchCode,final BigDecimal authorCode){<br />
        return (List) getHibernateTemplate().execute(new HibernateCallback() {<br />
              public Object doInHibernate(Session session) throws HibernateException, SQLException<br />
              {<br />
                  Query q = session.getNamedQuery(&#8220;LIB_SP&#8221;);<br />
                  q.setLong(&#8220;branchCD&#8221;, branchCode.longValue());<br />
                  q.setLong(&#8220;authorCD&#8221;, authorCode.longValue());<br />
                  return q.list();<br />
              }<br />
          });<br />
      }</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.unidev.com/index.php/2009/03/03/calling-oracle-stored-procedure-from-hibernate/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
	</channel>
</rss>

