Thursday, April 28, 2011

Calling Postgres function with integer[] input from Hibernate

Situation: Running Hibernate, Postgres. Have function defined in Postgres taking integer[] as input. Have defined in a Hibernate mapping file which calls the function using a variable parameter ":ids". Want to call this , passing in a List. How to do it?

Answer: Create a class named HibernateArrayLongType which extends UserType. Define the two core methods in this class as follows:

public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
throws HibernateException, SQLException {
Array value = rs.getArray(names[0]);
if (value != null) {
return value.getArray();
}
else
return null;
}

public void nullSafeSet(PreparedStatement statement, Object value, int index)
throws HibernateException, SQLException {

if (value == null) {
statement.setNull(index, Types.ARRAY);
} else {
List list = (List)value;
Long[] array = new Long[list.size()];
for (int i = 0; i < list.size(); i++) {
array[i] = list.get(i);
}
// the "integer" type name may be specific to Postgres
Array sqlArray = statement.getConnection().createArrayOf("integer", array);
statement.setArray(index, sqlArray);
}

}


In a class which extends HibernateDaoSupport, add this code:

List ids = new ArrayList();
Query query = getSession().getNamedQuery("queryName");
query.setParameter("ids", ids, Hibernate.custom(HibernateArrayLongType.class));
List result = (List)query.list();

Monday, January 10, 2011

Tip for testing database function performance

Suppose you've got a database function that's slow. You want to determine where the bottlenecks are, so you decide to pepper your function with RAISE NOTICE statements to output the current time at different points within the function. You try "RAISE NOTICE current_timestamp" and note that every time this statement is executed, the same timestamp is output. It's as if current_timestamp has been pre-set to be the moment in time when the function began executing. What to do? The solution is to use "RAISE NOTICE timeofday()" - this will reflect the actual time, not the time the function began executing.

(Please note that this applies to Postgres. I don't know if this post translates precisely for other DBMS's.)

Postgres performance considerations

A great article about Postgres performance considerations that the guys from thoughtbot have been so kind to share - read and enjoy!

Monday, December 27, 2010

Preserve the history while re-adding a file to Subversion

To re-add a file previously deleted in Subversion execute this command...

svn copy --revision [rev number] [path to file in repository] [path on local file system to copy to]

... then commit the file. This will retain the history of the file.

Here's an example:

(1) svn copy --revision 20471 http://tools.symposium.org/svn/smp/dev/config/classes/velocity/donor/feedback.vm config/classes/velocity/donor/feedback.vm

(2) svn commit -m "resurrected feedback.vm from rev 20471" config/classes/velocity/donor/feedback.vm

Monday, August 30, 2010

Type conversion: Spring 2 vs. Spring 3

To my dismay, there is no generalized String <-> Object conversion mechanism in Spring 2.0 unless you're using Webflow. Spring 3, however, includes it for non-Webflow situations.

Tuesday, July 20, 2010

Unlimited strong passwords in a flash

Ever needed to generate a bunch of strong passwords in a flash? Look no further than this amazing tool!