Friday, July 22, 2011

Incorrect Order of SQL execution within a Spring Hibernate Transaction

Recently I faced a strange issue when trying to insert proper data into table ,it was halting with Unique key Constraint Exception.In a transaction I had to read a record and update the Unique Key fields and in the next step insert a record .The record inserted should have the same Unique Key values as the record read from the table.The sequence was a simple read,update and insert a new record.Was baffled when this sequence continuous threw a Unique Key Constraint.When I removed the UK constraint from the table,the records were updated and inserted as required.

On checking on the hibernate log found that the insert was fired before the update .I tried to disable the Spring transaction and then the order of execution was correct.
Looks like the order of SQl execution is not guaranteed within a transaction.To solve this issue one of the following needs to be done after the update and before the insert
1.call hibernateTemplate.flush() or
2.call hibernateTemplate.refresh() or
3.again read the record from table ,this internally calls the flush.

Friday, April 29, 2011

Upgrading to Spring MVC 2.5

I was trying to get familiarised with Spring MVC 2.5.I've used MVC 2.0 in the past and was counting to levarage on the experience.Gettings hands on I realised that 2.5 is a lot different from 2.0.The following are the major differences based on my understanding

Controllers:
Spring 2.5 has introduced Annotation based Controllers.Apart from the native controllers like AbstractController, SimpleFormController or MultiActionController, the DispatcherServlet can now handle any kind of action type.The controllers need not extend or implement MVC related classes or interface.A simple java class can serve as a controller by just adding the @Controller sterotype.

The annotated controllers are identified by specifying the context:component-scan in application context file.This specifies the package of the any annotated components that should be searched.

The methods that specify the mapping should be annotated with the @RequestMapping contruct .This maps the web request and specifies the type of method.The handler method signature are flexible and within a controller each mapping method can have different signature.The mapping methods allows to return nothing (void), a String representing a view, or just a model.By convention, if method does not explicitly return a view, the view name is defined by converting the mapped URL into a view name.

@ModelAttribute:
The methods marked with @ModelAttribute are invoked before every request.This can be used prepopulate the data required by the form.The functionality is similiar to the FormBackingObject in SimpleFormController.A controller can have any number of ModelAttribute methods.In most cases one method to populate the required data should suffice.


Validation and Binding Errors:
In Spring 2.0,injecting the validator bean would handle the Validation.But in 2.5,the validator needs to be injected and should be invoked manually.The BindingResult can be used to store the request data binding errors .If a method uses multiple objects a separate BindingResult instance can be defined for each object

Property Editor:
The Property editor can be registered using the method annotated with @InitBinder contruct.This is similiar to the initBinder() method .

The sample code has a simple form that has option in English/French to enter the data.The source code can be downloaded here.

Saturday, February 26, 2011

Reading data from Hashmap using JSTL

The requirement was to retrieve date based on the key from a HashMap and to display it in the UI.This is a common scenario when using the foreign key data.Initially seemed quite straightforward to create a HashMap to store the key and Values.In the JSP front ,the data can be retrieved either by
(i) looping the contents ( construct ) to identify the match or
(ii) retrieve the data from HashMap using the key( map["key"]).
I preferred the later approach since it was more performance effective.

Map map = new HashMap();
map.put(1, "Monday");
map.put(2, "Tuesday");
map.put(3, "Wednesday");

This was not rendering the data in the page.But when looping the contents the match was found and displayed in the page.

By modifying the key type in HashMap to String the values were retrieved and displayed in the Page.
Map map = new HashMap();
map.put("1", "Monday");
map.put("2", "Tuesday");
map.put("3", "Wednesday");

My inference from this is that EL does not support the Interger wrapper class for HashMap.But I could not find any documentation to confirm this.

Saturday, January 15, 2011

NullPointerException when creating Spring Bean

If the class name of the Bean is missed (for some reason )in the context file,it gives rise to NullPointerException by the factory.The stack trace was like
Caused by: java.lang.NullPointerException
at java.util.concurrent.ConcurrentHashMap.hash(ConcurrentHashMap.java:157)
at java.util.concurrent.ConcurrentHashMap.get(ConcurrentHashMap.java:730)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:186)