<?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/category/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>Restrictions on table alias in Hibernate Criteria sqlRestriction</title>
		<link>http://blog.unidev.com/index.php/2011/06/07/restrictions-on-table-alias-in-hibernate-criteria-sqlrestriction-2/</link>
		<comments>http://blog.unidev.com/index.php/2011/06/07/restrictions-on-table-alias-in-hibernate-criteria-sqlrestriction-2/#comments</comments>
		<pubDate>Tue, 07 Jun 2011 21:07:00 +0000</pubDate>
		<dc:creator>Dhanya James</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Oracle]]></category>
		<category><![CDATA[Hiberate sqlRestriction on alias]]></category>
		<category><![CDATA[hibernate criteria join]]></category>
		<category><![CDATA[hibernate criteria sqlRestriction]]></category>
		<category><![CDATA[hibernate join on fields other than primary key]]></category>

		<guid isPermaLink="false">http://blog.unidev.com/?p=329</guid>
		<description><![CDATA[Hibernate documentation on SQLRestriction on Crietria ******************* sqlRestriction public static Criterion sqlRestriction(String sql) Apply a constraint expressed in SQL. Any occurrences of {alias} will be replaced by the table alias. Parameters: sql Returns: Criterion ******************* The catch here is that the only {alias} sqlRestriction understands is the alias name of the table on which the [...]]]></description>
			<content:encoded><![CDATA[<p>Hibernate documentation on SQLRestriction on Crietria</p>
<p>*******************<br />
sqlRestriction public static Criterion sqlRestriction(String sql)<br />
Apply a constraint expressed in SQL.<br />
Any occurrences of {alias} will be replaced by the table alias.<br />
Parameters: sql<br />
Returns: Criterion<br />
*******************</p>
<p>The catch here is that the only {alias} sqlRestriction understands is the alias name of the table on which the criteria call is constructed. If there is more criterias added  , sqlRestriction does not understand the alias of any other tables joined.  In order to get around this problem in our project, i had to look at the SQL generated by hibernate to find the table alias and use this in the SQL passed to sqlRestriction.</p>
<p>Given below  is the criteria i had to use to make the sqlRetriction work to meet our requirements.</p>
<p>Criteria c = Session.createCriteria(ParentTable.class, &#8220;parent&#8221;);<br />
c.createCriteria(&#8220;parent.movements&#8221;, &#8220;movements&#8221;)<br />
         .add(Restrictions.sqlRestriction(&#8220;this_.movement_initial={alias}.movement_init and this_.movement_number={alias}.movement_nbr &#8220;));<br />
 <br />
 <br />
Hibernate generated an alias of this_ on the root criteria table &#8216;ParentTable&#8217; and i had to use this alias on the sqlRestriction.</p>
<p>Given below is our table structure</p>
<p>ParentTable</p>
<p>movement_id int(9) (unique)<br />
movement_initial varchar2(5)<br />
movement_number int(3)</p>
<p>ChildTable</p>
<p>child_id int(9) (unique)<br />
movement_id int(9) (non unique)<br />
movement_init varchar2(5)<br />
movement_nbr int(3)<br />
ParentTable.hbm.xml<br />
&lt;hibernate-mapping&gt;<br />
    &lt;class name=&#8221;com.dj.ParentTable&#8221; table=&#8221;ParentTable&#8221; schema=&#8221;XXX&#8221;&gt;<br />
        &lt;id name=&#8221;movementid&#8221; type=&#8221;java.lang.Long&#8221;&gt;<br />
            &lt;column name=&#8221;movement_id&#8221; precision=&#8221;9&#8243; scale=&#8221;0&#8243; /&gt;<br />
            &lt;generator&gt;<br />
                &lt;param name=&#8221;sequence&#8221;&gt;XX_Q1&lt;/param&gt;<br />
            &lt;/generator&gt;<br />
        &lt;/id&gt;</p>
<p> &lt;property name=&#8221;movementInitial&#8221; type=&#8221;java.lang.String&#8221;&gt;<br />
             &lt;column name=&#8221;movement_initial&#8221; length=&#8221;5&#8243; /&gt;<br />
 &lt;/property&gt;<br />
 &lt;property name=&#8221;movementNumber&#8221; type=&#8221;java.lang.Long&#8221;&gt;<br />
             &lt;column name=&#8221;movement_number&#8221; precision=&#8221;6&#8243; scale=&#8221;0&#8243; /&gt;<br />
        &lt;/property&gt;<br />
 &lt;set name=&#8221;movements&#8221; inverse=&#8221;true&#8221; cascade=&#8221;save-update&#8221;&gt;<br />
            &lt;key&gt;<br />
                &lt;column name=&#8221;movement_id&#8221; precision=&#8221;9&#8243; scale=&#8221;0&#8243; not-null=&#8221;true&#8221; /&gt;<br />
            &lt;/key&gt;<br />
            &lt;one-to-many class=&#8221;com.dj.ChildTable&#8221;/&gt;<br />
&lt;/set&gt;<br />
&lt;/class&gt;<br />
&lt;/hibernate-mapping&gt;</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.unidev.com/index.php/2011/06/07/restrictions-on-table-alias-in-hibernate-criteria-sqlrestriction-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Oracle PL/SQL Date functions</title>
		<link>http://blog.unidev.com/index.php/2011/02/07/oracle-plsql-date-functions/</link>
		<comments>http://blog.unidev.com/index.php/2011/02/07/oracle-plsql-date-functions/#comments</comments>
		<pubDate>Mon, 07 Feb 2011 15:07:06 +0000</pubDate>
		<dc:creator>Dhanya James</dc:creator>
				<category><![CDATA[General Technology]]></category>
		<category><![CDATA[Oracle]]></category>

		<guid isPermaLink="false">http://blog.unidev.com/?p=294</guid>
		<description><![CDATA[ All examples given here selects from DUAL table and DUAL table is a special one row table present by default in all Oracle database installations. All examples have been executed based on sysdate and these SQLs were run on 2/7/2011. sysdate function: You can use sysdate function to get current date and time . select sysdate from dual; [...]]]></description>
			<content:encoded><![CDATA[<p> All examples given here selects from DUAL table and DUAL table is a special one row table present by default in all Oracle database installations. All examples have been executed based on sysdate and these SQLs were run on 2/7/2011.</p>
<p><strong>sysdate function:<br />
</strong>You can use sysdate function to get current date and time .<br />
select sysdate from dual;<br />
return 2/7/2011 7:31:31 AM<br />
 <br />
<strong>to_char function:<br />
</strong>You can use to_char function to convert a date to a formatted string.<br />
select to_char(sysdate, &#8216;yyyy/mm/dd&#8217;) from dual;<br />
return 2011/02/07<br />
select to_char(sysdate, &#8216;DDth MON, YYYY&#8217;) from dual;<br />
return 07TH FEB, 2011<br />
select to_char(sysdate, &#8216;YYYY ,DDth MON&#8217;) from dual;<br />
return 2011 ,07TH FEB<br />
select to_char(sysdate, &#8216;MON DDth, YYYY&#8217;) from dual;<br />
return FEB 07TH, 2011<br />
select to_char(sysdate, &#8216;FMMON DDth, YYYY&#8217;) from dual;<br />
FEB 7TH, 2011<br />
select to_char(sysdate, &#8216;FMMon ddth, YYYY&#8217;) from dual;<br />
Feb 7th, 2011<br />
select to_char(sysdate, &#8216;Month DD, YYYY&#8217;) from dual;<br />
February  07, 2011<br />
select to_char(sysdate, &#8216;FMMonth DD, YYYY&#8217;) from dual;<br />
February 7, 2011</p>
<p><strong>to_date function:<br />
</strong>You can use to_date function to convert a string to a Date.<br />
select to_date(&#8217;2011/02/07&#8242;, &#8216;yyyy/mm/dd&#8217;) from dual;<br />
return 2/7/2011<br />
select to_date(&#8217;070211&#8242;,&#8217;DDMMYY&#8217;) from dual;<br />
return 2/7/2011<br />
 <br />
<strong>trunc function:<br />
</strong>You can use trunc function to return a date truncated to a specific unit of measure.<br />
 <br />
Get beginning of the quarter<br />
select trunc(sysdate, &#8216;Q&#8217;) from dual;<br />
return 1/1/2011<br />
Get first day of the Month<br />
select trunc(sysdate, &#8216;MONTH&#8217;) from dual;<br />
return 2/1/2011<br />
Get start day of the week or get first work day of the week<br />
select trunc(sysdate,&#8217;DD&#8217;) from dual;<br />
return 2/7/2011<br />
Get time in 24 hours<br />
select trunc(sysdate,&#8217;HH24&#8242;) from dual;<br />
return 2/7/2011 7:00:00 AM<br />
Get time in 12 hours<br />
select trunc(sysdate,&#8217;HH12&#8242;) from dual;<br />
return 2/7/2011 7:00:00 AM</p>
<p><strong>add_months function:<br />
</strong>You can use add_months function to add or subtract n months from a given date.<br />
select add_months(sysdate,2) from dual;<br />
return 4/7/2011 7:15:49 AM<br />
select add_months(sysdate,-4) from dual;<br />
return 10/7/2010 7:16:47 AM<br />
 <br />
<strong>last_day function:<br />
</strong>You can use last_day function to return last day of the current month.<br />
select last_day(sysdate) from dual;<br />
return 2/28/2011 7:23:49 AM</p>
<p><strong>next_day function:<br />
</strong>You can use next_day function to get the next week day greater than     the given date.<br />
Today is 02/07/2011 Monday.<br />
 <br />
select next_day(sysdate,&#8217;MONDAY&#8217;) from dual;<br />
return 2/14/2011 7:26:36 AM &#8211; Returns next Monday<br />
select next_day(sysdate,&#8217;TUESDAY&#8217;) from dual;<br />
2/8/2011 7:28:23 AM &#8211; Returns next Tuesday<br />
select next_day(sysdate,&#8217;WEDNESDAY&#8217;) from dual;<br />
2/9/2011 7:28:23 AM &#8211; Returns next Wednesday<br />
select next_day(sysdate,&#8217;THURSDAY&#8217;) from dual;<br />
2/10/2011 7:28:23 AM &#8211; Returns next Thursday<br />
select next_day(sysdate,&#8217;FRIDAY&#8217;) from dual;<br />
2/11/2011 7:28:23 AM &#8211; Returns next Friday<br />
 <br />
<strong>current_date function:<br />
</strong>You can use current_date function to return current date and time in the current time zone.<br />
select current_date from dual;<br />
return 2/7/2011 7:19:07 AM<br />
 <br />
<strong>current_timestamp function:<br />
</strong>You can use current_timestamp function to return current date and and time with the local time zone.<br />
select current_timestamp from dual;<br />
return 07-FEB-11 07.21.20.097093 AM -06:00;</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.unidev.com/index.php/2011/02/07/oracle-plsql-date-functions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ORA-30926: unable to get a stable set of rows in the source tables</title>
		<link>http://blog.unidev.com/index.php/2010/12/21/ora-30926-unable-to-get-a-stable-set-of-rows-in-the-source-tables/</link>
		<comments>http://blog.unidev.com/index.php/2010/12/21/ora-30926-unable-to-get-a-stable-set-of-rows-in-the-source-tables/#comments</comments>
		<pubDate>Tue, 21 Dec 2010 16:40:37 +0000</pubDate>
		<dc:creator>Dhanya James</dc:creator>
				<category><![CDATA[General Technology]]></category>
		<category><![CDATA[Oracle]]></category>

		<guid isPermaLink="false">http://blog.unidev.com/?p=282</guid>
		<description><![CDATA[Merge statement can be used if you are trying to use transformed data fetched from a set of tables to update another table. MERGE INTO table_3 c USING (SELECT a.book_id, a.author_id          FROM table_1 a, table_2 b         WHERE a.author_id = b.author_id) src ON (c.book_id = src.book_id) WHEN MATCHED THEN   UPDATE SET c.author_id  = [...]]]></description>
			<content:encoded><![CDATA[<p>Merge statement can be used if you are trying to use transformed data fetched from a set of tables to update another table.</p>
<p>MERGE INTO table_3 c<br />
USING (SELECT a.book_id, a.author_id<br />
         FROM table_1 a, table_2 b<br />
        WHERE a.author_id = b.author_id) src<br />
ON (c.book_id = src.book_id)<br />
WHEN MATCHED THEN<br />
  UPDATE SET c.author_id  = src.author_id;</p>
<p>This mergae statement might return error &#8216;ORA-30926: unable to get a stable set of rows in the source tables&#8217; , if there is duplicates in the &#8216;USING&#8217; query. For example, duplicate book_id retuned by the &#8216;src&#8217; query in the given example. This can be fixed by using a DISTINCT in the query</p>
<p>MERGE INTO table_3 c<br />
USING (SELECT DISTINCT a.book_id, a.author_id<br />
         FROM table_1 a, table_2 b<br />
        WHERE a.author_id = b.author_id) src<br />
ON (c.book_id = src.book_id)<br />
WHEN MATCHED THEN<br />
  UPDATE SET c.author_id  = src.author_id;</p>
<p>-Dhanya James</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.unidev.com/index.php/2010/12/21/ora-30926-unable-to-get-a-stable-set-of-rows-in-the-source-tables/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<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>

