Friday, December 25, 2009

301 Moved Permanently!

We have moved! Please visit us at:

Sunday, March 9, 2008

Oracle Hierarchical Query in Hibernate

When you need then there is no replacement of Oracle hierarchical queries. At the same time we would like Hibernate to do the hard work of mapping result set to entities. Here is an example of Hibernate Native Query which would fetch the organization chart from famous scott.emp table.

Session s = (Session) em.getDelegate();
String orgChartQuery = "select {emp.*} from EMP {emp} \n" +
"start with emp.mgr is null \n" +
"connect by prior emp.empno = emp.mgr\n" +
"order siblings by ename";
List list = s.createSQLQuery(orgChartQuery)
.addEntity("emp", Employee.class)

Now suppose we want to render this in a tree control with breadcurms. We can use the following query.

Session s = (Session) em.getDelegate();
String orgChartQuery = "select sys_connect_by_path(emp.empno, '/') breadcrum,\n"+
"level, {emp.*} from EMP {emp} \n" +
"start with emp.mgr is null \n" +
"connect by prior emp.empno = emp.mgr\n" +
"order siblings by ename";
List list = s.createSQLQuery(orgChartQuery)
.addScalar("breadcrum", Hibernate.STRING)
.addScalar("level", Hibernate.INTEGER)
.addEntity("emp", Employee.class)
for (Object[] objects : list) {
String breadcrum = (String) objects[0];
Integer level = (Integer) objects[1];
Employee emp = (Employee) objects[2];
System.out.println(breadcrum +" "+ level + " "+emp.getName()
+ " "+emp.getDepartment().getName());

Breadcrums willl give you path to the object from the root and level gives the 1 based level in the tree.

Friday, January 11, 2008

Handling Date with Google Gears

Suppose you execute the following DML statement in Google Gears:
"insert or replace into mytable "+
"(id, modified_on) values (?, ?)",
[null, new Date()]

This seemingly works but you are in for a surprise because Date is stored as Date.toString(). SQLite ducktying brain can't infer that it's a date datum. If you ran select query you will get string back.
Sat Jan 12 2008 02:34:58 GMT-0800 ยป
(Pacific Standard Time)

The downside of storing date as text that you can't use any SQLite date time functions in your queries. Due to manifest typing, SQLite happily stores text in a date column. We would like to do better. We would like to store JavaScript Date as native SQLite Date without any loss of information. SQLite itself stores date in Julian Day representation while JavaScript cannonical date representation is number of milliseconds since 1 January 1970 00:00:00 UTC. So, we need to convert JavaScript Date to either Julian Day or text as understood by SQLite. Considering the ease of implementation, efficiency and lossless representation, YYYY-MM-DDTHH:MM:SS.FFF date format seems to fit the bill. This is ISO8601 format. However after some digging in SQLite code, it turns out that if it SQLite also works even if we don't zero pad fields. So this simple JavaScript method with minor departure from ISO8601 format does the job well.
dateToSQLiteFormat = function(date) {
return date.getUTCFullYear()+
"-"+date.getUTCMonth() + 1+

So far so good. How about converting SQLite dates to JavaScript Date object. This function does that trick.
dateSqlFragment = function(column, alias) {
return " ((strftime('%s', "+column+") -
strftime('%S', "+column+"))*1000 +
strftime('%f', "+column+")*1000) "+
(alias "");

var rs = db.execute(
"select id, "+dateSqlFragment("modified_on") +
" from mytable");
Date date = new Date(rs.field(1));

Thursday, May 3, 2007

Smart Copy Plugin for IntelliJ

IntelliJ always seems to do The Right Thing. It comes with code-aware features such as Smart Line Joins, and Smart Line Split. However, there is not Smart Copy to complement Smart Paste. Besides symmetry, there are good reasons to have Smart Copy. Supppose, you have a SQL query in Java Code and you want to run it in SQLPlus to see an execution plan. Another example could be embedded XML as compile time constant. When you copy a query embedded in a Java class, perhaps you are interested in its value without without the quotes and plus characters clinging to the copied text.
If you are a surprised user, you are not alone. Smart Copy is a plugin to copy the literal values. Smart Copy action maps to Ctrl+Alt+Shift+C by default. It copies the value of compile time constants and String literals to the system clipboard. It also copies the selected literals. Entire expression is copied if noting is selected. If it cannot do smart copy, it will fallback to regular copy action. Certainly a candidate for your Ctrl+C mapping.

Labels: ,

Saturday, April 28, 2007

Maven debug output

Imagine you are frustrated by one of the maven plugin and you want to turn the debug output on. Google debugging a maven plugin or maven debug output and you may still be out of luck. Here is the brute force way of doing it.
Open MAVEN_HOME/core/plexus-container-default-1.0-alpha-9.jar and edit org/codehaus/plexus/plexus-bootstrap.xml to set threshold to debug.


Tuesday, January 16, 2007

java.util.Scanner oops!

Java 5 java.util.Scanner provides a better replacement for StringTokenizer. And, the hope was that we should be able to write,

Scanner lines = new Scanner(new File("/etc/passwd"));
for(String line : lines) {

Unfortunately, this doesn't work because someone mistyped, and it implements Iterator instead of Iterable.