Restrictions on table alias in Hibernate Criteria sqlRestriction

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 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.

Given below  is the criteria i had to use to make the sqlRetriction work to meet our requirements.

Criteria c = Session.createCriteria(ParentTable.class, “parent”);
c.createCriteria(“parent.movements”, “movements”)
         .add(Restrictions.sqlRestriction(“this_.movement_initial={alias}.movement_init and this_.movement_number={alias}.movement_nbr “));
 
 
Hibernate generated an alias of this_ on the root criteria table ‘ParentTable’ and i had to use this alias on the sqlRestriction.

Given below is our table structure

ParentTable

movement_id int(9) (unique)
movement_initial varchar2(5)
movement_number int(3)

ChildTable

child_id int(9) (unique)
movement_id int(9) (non unique)
movement_init varchar2(5)
movement_nbr int(3)
ParentTable.hbm.xml
<hibernate-mapping>
    <class name=”com.dj.ParentTable” table=”ParentTable” schema=”XXX”>
        <id name=”movementid” type=”java.lang.Long”>
            <column name=”movement_id” precision=”9″ scale=”0″ />
            <generator>
                <param name=”sequence”>XX_Q1</param>
            </generator>
        </id>

 <property name=”movementInitial” type=”java.lang.String”>
             <column name=”movement_initial” length=”5″ />
 </property>
 <property name=”movementNumber” type=”java.lang.Long”>
             <column name=”movement_number” precision=”6″ scale=”0″ />
        </property>
 <set name=”movements” inverse=”true” cascade=”save-update”>
            <key>
                <column name=”movement_id” precision=”9″ scale=”0″ not-null=”true” />
            </key>
            <one-to-many class=”com.dj.ChildTable”/>
</set>
</class>
</hibernate-mapping>

Some corrections to Hibernate tutorial

Hibernate logo
I was referring to the First application at Hibernate (Jboss) offical documentation. I found some problems when doing the exercise: just want to share some of my findings so others may benefit.

1) dependency version missing: there are already some some discussions on the Hibernate discussion forum, such as this one and that one. I followed the first one: added the version numbers as suggested and the problem went away.

2) jdbc driver can not be found, the fix is to

mvn install:install-file -DgroupId=hsqldb -DartifactId=hsqldb -Dversion=1.8.1.2 -Dpackaging=jar -Dfile=/path/to/your/hsqldb.jar

in my case (mac) this is /Users/major_xu/Downloads/hsqldb/lib/hsqldb.jar

I saw discussion on this one, too, the suggestion is to “add classpath for hsqldb.jar”. But I don’t know how to do it in maven command line.

3) misc: there are a lot other corrections:

Read the rest of this entry »

Oracle PL/SQL Date functions

 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;
return 2/7/2011 7:31:31 AM
 
to_char function:
You can use to_char function to convert a date to a formatted string.
select to_char(sysdate, ‘yyyy/mm/dd’) from dual;
return 2011/02/07
select to_char(sysdate, ‘DDth MON, YYYY’) from dual;
return 07TH FEB, 2011
select to_char(sysdate, ‘YYYY ,DDth MON’) from dual;
return 2011 ,07TH FEB
select to_char(sysdate, ‘MON DDth, YYYY’) from dual;
return FEB 07TH, 2011
select to_char(sysdate, ‘FMMON DDth, YYYY’) from dual;
FEB 7TH, 2011
select to_char(sysdate, ‘FMMon ddth, YYYY’) from dual;
Feb 7th, 2011
select to_char(sysdate, ‘Month DD, YYYY’) from dual;
February  07, 2011
select to_char(sysdate, ‘FMMonth DD, YYYY’) from dual;
February 7, 2011

to_date function:
You can use to_date function to convert a string to a Date.
select to_date(’2011/02/07′, ‘yyyy/mm/dd’) from dual;
return 2/7/2011
select to_date(’070211′,’DDMMYY’) from dual;
return 2/7/2011
 
trunc function:
You can use trunc function to return a date truncated to a specific unit of measure.
 
Get beginning of the quarter
select trunc(sysdate, ‘Q’) from dual;
return 1/1/2011
Get first day of the Month
select trunc(sysdate, ‘MONTH’) from dual;
return 2/1/2011
Get start day of the week or get first work day of the week
select trunc(sysdate,’DD’) from dual;
return 2/7/2011
Get time in 24 hours
select trunc(sysdate,’HH24′) from dual;
return 2/7/2011 7:00:00 AM
Get time in 12 hours
select trunc(sysdate,’HH12′) from dual;
return 2/7/2011 7:00:00 AM

add_months function:
You can use add_months function to add or subtract n months from a given date.
select add_months(sysdate,2) from dual;
return 4/7/2011 7:15:49 AM
select add_months(sysdate,-4) from dual;
return 10/7/2010 7:16:47 AM
 
last_day function:
You can use last_day function to return last day of the current month.
select last_day(sysdate) from dual;
return 2/28/2011 7:23:49 AM

next_day function:
You can use next_day function to get the next week day greater than     the given date.
Today is 02/07/2011 Monday.
 
select next_day(sysdate,’MONDAY’) from dual;
return 2/14/2011 7:26:36 AM – Returns next Monday
select next_day(sysdate,’TUESDAY’) from dual;
2/8/2011 7:28:23 AM – Returns next Tuesday
select next_day(sysdate,’WEDNESDAY’) from dual;
2/9/2011 7:28:23 AM – Returns next Wednesday
select next_day(sysdate,’THURSDAY’) from dual;
2/10/2011 7:28:23 AM – Returns next Thursday
select next_day(sysdate,’FRIDAY’) from dual;
2/11/2011 7:28:23 AM – Returns next Friday
 
current_date function:
You can use current_date function to return current date and time in the current time zone.
select current_date from dual;
return 2/7/2011 7:19:07 AM
 
current_timestamp function:
You can use current_timestamp function to return current date and and time with the local time zone.
select current_timestamp from dual;
return 07-FEB-11 07.21.20.097093 AM -06:00;

ORA-30926: unable to get a stable set of rows in the source tables

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  = src.author_id;

This mergae statement might return error ‘ORA-30926: unable to get a stable set of rows in the source tables’ , if there is duplicates in the ‘USING’ query. For example, duplicate book_id retuned by the ‘src’ query in the given example. This can be fixed by using a DISTINCT in the query

MERGE INTO table_3 c
USING (SELECT DISTINCT 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  = src.author_id;

-Dhanya James

PHP – problem trying to connect with Informix on IIS

I was working on a project which needs PHP talk to Informix database on Windows platform. After installed IBM Informix Client_SDK, included PHP extension php_ifx.dll, and use following command to connect to Informix:

$sync_link_id = ifx_connect($database, $username, $password);

I got following error:

[SQLSTATE=IX 000 SQLCODE=-25560]

If you check Informix document from IBM, you know Environment variable INFORMIXSERVER must be set. We know how to set up environment variable on Unix and Apache, but the question is how to set it up for IIS?

People may first think this could be environment variable on Windows as well. But this is wrong. Informix client is using Registry KEY on Windows platform. Here is the path on a 64bit machine.

HKEY_LOCAL_MACHINE\Software\Wow6432Node\Informix\Environment\

Second, you should have your default server setup under KEY:

HKEY_LOCAL_MACHINE\Software\Wow6432Node\Informix\SqlHosts\

At here, you need to provide HOST, PROTOCOL and SERVICE, so php_ifx knows how to connect to your Informix server.

After set them up, PHP should be able to talk to Informix.

Redirect to Error page in Wicket

I was looking for a solution to redirect to an error page in wicket and found this class , RestartResponseAtInterceptPageException , causes wicket to interrupt current request processing and immediately redirect to an intercept page.

For example,
throw new RestartResponseAtInterceptPageException(new ErrorPage(“User does not have permission to view this screen”));

If you need to redirect to an intercept page without interrupting the current request processing , call RedirectToInteceptPage(Page) instead.

-Dhanya

Change location of Wicket’s ajax debug link

I needed to move Wicket’s ajax debug link from the bottom right to the bottom left because it was covering some links.  The script below is how I accomplished it (uses jQuery).  The link uses absolute positioning so you can customize this to move it anywhere on the page.

    <script type="text/javascript">
        $(document).ready(function() {
            //
            // Moves the debug window to the left instead of the right.
            //
            // Needs to use setTimeout so that the wicket-ajax-debug.js
            // script has a chance to create the link
            //
            if(wicketAjaxDebugEnable)
            {
                setTimeout(function() {
                    $("#wicketDebugLink").css('left', '10px');
                }, 1000);
            }
        });
    </script>

Unidev Makes the List of St. Louis’ Best Employers

 

Employees Credit Unidev with Best Company Culture in St. Louis Post Dispatch Contest

 

During the month of February, the St. Louis Post-Dispatch and STLtoday.com invited employees of St. Louis area businesses to vote for their company to make the St. Louis Top Employer’s list. Readers were given the opportunity to vote their employer in one of the five categories: Best company culture, best commitment to diversity, best perks, best co-workers/team, and best benefits.

Out of 94,000 businesses in the St. Louis area, 72 companies were given the honor by its employees. Unidev was nominated for “Best Company Culture”, and was acknowledged in the Sunday February 28, issue of the St. Louis Post Dispatch on the front page of the ‘Jobs’ section. In addition, Unidev was in the top five vote getters in the contest.

Here is what some of the employees had to say about the company culture at Unidev:

“Unidev is truly a great place to work. The work environment promotes open communication, creativity and leadership among employees. Everyone is encouraged to contribute and new ideas are welcomed. Management and employees are treated the same, and everyone is part of the bigger picture.”

“The company culture at Unidev may be part of the reason a person joins the company, but it is a big part of the reason people stay with the company. The culture at Unidev is the perfect balance between a down-to-earth, family-oriented atmosphere and a strong, results-driven work environment. Due to Unidev’s commitment to corporate social responsibility and good, transparent governance, great customer service, a strong, long-term brand, and passionate employees and customers seems to naturally happen on its own. Especially in these tough times, it is amazing to be a part of a company who is constantly growing, adding new members from different expertise and really listens to and cares about the happiness and well-being of each individual employee and customer. I couldn’t imagine working anywhere else.”

“Unidev is a great place to work! It is a very laid back atmosphere to work in yet everyone has a very strong work ethic and works together to complete project and make the customers happy.”

 Thanks to all of the employees at Unidev and The Net Impact for making Unified Development Inc. a great company to work for!

Unidev – 20 years and Counting

On January 1, 1990, Unidev was founded. Of course it was a lot different then. Initially we (the two of us) did custom development mostly in Clipper. About 6 months after we started, things started to pick up and we added another programmer. This was all on premises programming mostly for smaller companies. We saw steady growth for the next year. In 1991, we got a pretty good sized project for a Fortune 1000 company. We were just in the right place at the right time on this one. There just were not that many shops specializing in PC development at that time. Through the 90s, the field consulting business grew nicely. Around 2001 with the end of Y2K, the .com bust and 9/11, business really slowed down. We shifted to doing more in house projects and have picked up a number of very strong customers with long term contracts. That business continues to show solid growth for us today. We’ll continue to adapt to the needs of our customers and I hope to be here another 20 years.

Unidev Seeks Implementation Partner for Global Web CMS

        Looking to Microsoft Partner Network to Expand Opportunities in New Markets

Unidevannounces its search for Microsoft Partners in North America, Asia and Europe to implement their global web content management (CMS) system, Auctori™. As a 16 year Certified Microsoft Partner, we are looking to leverage the Microsoft Partner Channel Builder to deepen our services and solutions to more complex business opportunities, to provide a broader range of client services and to create business relationships with other partners whose skills complement our own.

“The Microsoft Partner Channel Builder is one of the key sales enablement benefits of our Microsoft relationship, and is an excellent way to combine our expertise and resources. We are looking forward to connecting with industry partners to form new alliances and deliver the end-to-end solutions that address customers’ business needs,” said Greg Alexander, CEO, Unidev. “This tool, with its two-way model, is making Microsoft Partner communications much easier.”

Auctori, a .Net SaaS CMS Platform, was developed by Unidev for multi-national companies, and currently supports a number of European, North American and Asian sites. This system supports multi-lingual site management and site deployment. Auctori was built from the ground up in conjunction with a team of search engine optimization (SEO) specialist to implement best SEO practices. The next release of Auctori is scheduled for the first quarter of 2010, and will feature European and Asian hosting in addition to its current North American hosting, with all sites and platforms managed from a single console.

For further information on Unidev or Auctori, contact Steve Thomas at 636-532-4424 or steve.thomas@unidev.com.

 

Read the full press release