Thursday, December 27, 2012

New Hibernate Section on site

I have added a new section on the site for Hibernate tips and tricks. Hopefully you will find it helpful and educational.

It can be found Hibernate Tips and Tricks

Wednesday, September 26, 2012

Convert Java array to a Set easily

How to convert an array to a java.util.Set easily

Have you ever wondered why there is no Arrays.asSet()? Do you loop over your array to add each element to a Set to make it a set? Well there is a much easier way. Its also why there is no Arrays.asSet().
String[] strArray = new String[] {"a", "b", "c"};
...
Set arrayAsSet = new HashSet(Arrays.asList(strArray));
Simple as that.

Find a jar

Find what jar a class belongs to in your local maven repo

Have you ever had to figure out what jar a class file belongs to? Here is a quick way to search in your local maven repo to find out what jar it is in:
Maven 1
find ~/.maven/ -name '*.jar' | xargs grep classname

Maven 2
find ~/.m2/ -name '*.jar' | xargs grep classname
If you are on windows and can not use find, your best bet is to install cygwin. It is a great free utility for windows that lets you use linux commands in windows. With a little work it will even let you run X-windows stuff. 

Tuesday, September 25, 2012

Coalesce as a replacement for NVL

If you are trying to keep your SQL or evan your HQL database agnostic, which is generally a good thing. One way to do this is to not use NVL but use the more generally available (although not as optimized) Coalesce function. 

Coalesce takes two arguments and processes them from left to right and then returns the first non null value it finds. For instance if you wrote
coalesce(null, "smurf")
you would get "smurf" as the return.

A really good place to use coalesce is in Date processing. Say I'm looking for a recipe that has a nullable live_date and a nullable experation_date to see if the recipe is currently active. I would write:
select * from recipe r where coalesce(r.live_date, sysdate) <= sysdate and coalesce(r.expiration_date, sysdate) >= sysdate
Essentially this is a between that is null safe.

Give coalesce a try and let me know what you think.

Monday, September 24, 2012

Welcome to my new blog

Since people have been complaining about the format I am using on my Google site I am going to try moving everything to this blog format. Let me know what you think.