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
Thursday, December 27, 2012
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"};
...
SetarrayAsSet = 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
Maven 2
Maven 1
find ~/.maven/ -name '*.jar' | xargs grep classname
Maven 2
find ~/.m2/ -name '*.jar' | xargs grep classnameIf 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
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:
Give coalesce a try and let me know what you think.
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) >= sysdateEssentially this is a between that is null safe.
Give coalesce a try and let me know what you think.
Subscribe to:
Posts (Atom)