If you're interested in functional programming, you might also want to checkout my second blog which i'm actively working on!!

Monday, September 27, 2010

Conditional breakpoints in Eclipse

If you ever have to debug a large for-loop in your code and you only want to inspect an iteration if some condition holds true, you should set a conditional breakpoint.



















Higher order functions in XSLT

I read this very interesting article which almost sounds like using clojures in java but then for xslt. Anyway... when i have the time i'll do a few tests and see if I can directly benefit from the approach mentioned over here.

Behaviour Driven Development

One of the latest hypes is BDD. In Scala there are quite a few BDD libraries available. E.g. Specs.

I was curious to see if there was any library available for Java as well and I found a few:


My personal favourite based on the examples would be EasyB which allows you to write stories with a combination of human understandable language and groovy's expressiveness to test the actual behaviour.

I will shortly try to explain what BDD tries to offer next to good ol' plain interfaces and what some shortcomings of normal interfaces are.

Let us start with discussing following interface:


So this interface should form 'the contract' for the concrete implementation. But what would prevent me from writing following implementation which does nothing from the behaviour we intended.



And for all we know the method could be invoked like addRecipient("Robby Pelssers") which is not exactly a valid email address.

So if we were to add behavioural conditions to our interface, we might be better off. Let us take a look at a revamped version of our interface. The syntax will of course depend on the BDD library, but the idea should be clear.

Sunday, September 26, 2010

Hibernate and Spring - Part 6

Everybody who uses hibernate in a project is bound to run into the famous LazyInitializationException problem. By default, collections are loaded lazy. That's in most cases also what you want. E.g.: If you query Facebook for users with firstname 'John', you don't want to fetch all available data about those users in 1 go. You only want to get their complete names. If you click on the name, you want more detailed information.

You should definitely read Hibernate Fetching Strategies

Below default behaviour (fetch=FetchType.LAZY) which can be omitted.


One way to make sure you never run into lazyinitalization exceptions is to fetch eagerly. But be aware for the penalty on performance and memory consumption. You might be fetching big parts of your database if you use this strategy.


One other way to solve this issue is to add following snippet to the web.xml

Hibernate and Spring - Part 5

So let's take a look at one of our domain object's DAO interface and implementation:
  • JobDao
  • JobDaoImpl






As you can see the only thing we still had to implement was the method getEntityClass(). I also added a method to find a job based on its workflowname and itemname and used my convenience methods.

It calls the findListByProperties on the DaoImpl which dynamically creates a HQL string and calls the findByNamedParam(String query, String[] parameters, Object[] values) on our hibernate template.

The querystring we end up with looks like

Friday, September 24, 2010

Hibernate and Spring - Part 4

Ok.. So now that we do have our Domain Objects annotated and ready to be used, we need to write our Service or DAO layer which provides following methods for our domain objects:
  • find/query
  • delete
  • insertOrUpdate
So we already have defined our SessionFactory and HibernateTemplate. Time to make good use of it. First we define a generic interface and next a generic implementation.

One remark: Many methods are just one-1-one mapped to the typical hibernate template methods but I added some extra convenience methods like
  • findUniqueByProperty(String propertyName, Object value)
  • findUniqueByProperties(String[] propertyNames, Object[]values)
  • findListByProperty(String propertyName, Object value)
  • findListByProperties(String[] propertyNames, Object[]values)





Hibernate and Spring - Part 3

Now let's take a look at 2 of my DomainObject's: Job and JobEvent.
The relationships between both domainobjects are simple:
  • A job has 1 to many JobEvents
  • A JobEvent belongs to 1 Job.





Hibernate and Spring - Part 2

I think it's always good practice to extend some generic superclass for your Domain Objects. Even if they don't inherit anything at first, you might decide otherwise later on and it becomes very easy to add common methods and variables. For my use case I decided that every Domain Object had to be uniquely identifiable.

Hibernate and Spring - Part 1

I have an application which locally runs against a mysql database but for QA and PROD we're using Oracle 10G. It's quite easy to setup a generic application context by injecting the correct property values. I'm using Cocoon-Spring-Configurator do load the correct properties once the application Context is initialized.

Below my configuration:



Some example property values for Mysql:


Some example property values for Oracle:

Wednesday, September 22, 2010

Unwanted behaviour with Numeric converters in JSF

It seems that you have to be carefull as what to expect from the standard JSF converters. Let us take a look at following jsf snippet which should convert the value entered into a java.lang.Long.



It does a good job if the user enters something that is not convertible to a long.












However, when you enter nothing and leave the Job ID field blank, JSF does a not so nice job of converting the value to 0 (zero) while you were expecting a null value.

Check out following articles as well as how-to solve this:

http://balusc.blogspot.com/2008/07/dao-tutorial-use-in-jsf.html#EmptyToNullConverter
http://tomcat.apache.org/tomcat-6.0-doc/config/systemprops.html#Expression_Language


I tested with the solution from BalusC's blog but a custom converter didn't even help solving the issue on tomcat6.


So the option left is to add following startup parameter to tomcat's launch configuration and use the standard jsf converter as mentioned on top of this article.