Friday, October 26, 2012

Mapping SQL parameters with Spring's BeanPropertySqlParameterSource

If you don't use Spring for your J2EE web application database connectivity, then you should really check it out.

If, instead, you are already using, maybe you don't know a really nice object i discovered some time ago: the BeanPropertySqlParameterSource

The classic way to go in a query (or in an update) is to set the params one by one;
let's take this Person bean as example (id field omitted in this example):

 
 public class Person {         private String name;     private String lastname;     private Date birthdate;     private Language motherTongue;        /* Getters and Setters */        public String getName() {         return name;     }     public void setName(String name) {         this.name = name;     }     public String getLastname() {         return lastname;     }     public void setLastname(String lastname) {         this.lastname = lastname;     }     public Date getBirthdate() {         return birthdate;     }     public void setBirthdate(Date birthdate) {         this.birthdate = birthdate;     }     public Language getMotherTongue() {         return motherTongue;     }     public void setMotherTongue(Language motherTongue) {         this.motherTongue = motherTongue;     } }  

and the Language class referenced in it:

 
class Language {     int id;     int description;         /* Getters and Setters */     public int getId() {         return id;     }     public void setId(int id) {         this.id = id;     }     public int getDescription() {         return description;     }     public void setDescription(int description) {         this.description = description;     }    }  



(with Language values like this: {[1 , English] , [2 , Italian], etc...}


Let's say i've filled all the fields in the Person object, and i want to insert it into a database.
Its instance is named "person". Normally i would go with:

 
String sql = "INSERT INTO person_table " +              " ( name, lastname, birthday, id_language) " +              " VALUES " +              " ( :name, :lastname, :birthdate, :idLanguage) ";        MapSqlParameterSource params = new MapSqlParameterSource(); params.addValue("name", person.getName(), Types.VARCHAR); params.addValue("lastname", person.getLastname(), Types.VARCHAR); params.addValue("birthdate", person.getBirthdate(), Types.DATE); params.addValue("idLanguage", person.getLanguage().getId(), Types.NUMERIC); return namedJdbcTemplate.update(sql, params);  

Imagine a big bean with a lot of fields, with every field passed manually... it's not really a good way to go.

With the BeanPropertySqlParameterSource object instead, IF we are taking all the fields from one object (and his inner objects), we can do like this:

 
String sql = "INSERT INTO person_table " +              " ( name, lastname, birthday, id_language) " +              " VALUES " +              " ( :name, :lastname, :birthdate, :language.id) "; BeanPropertySqlParameterSource params = new BeanPropertySqlParameterSource(person); return namedJdbcTemplate.update(sql, params);  

Not bad uh ? It will retrieve the specified fields (and types) through Reflection, including the fields value  in the inner objects contained in the main bean. As you can note, the notation is OGNL-like, with the getter of the inner object starting with a dot and a lower case letter, stripping "get" from the method name:

:language.id
means
person.getLanguage().getId();

PRO:
  • less code;
  • auto-decoding field Type through Reflections, avoiding user errors;
  • changes on query or bean automatically managed, without the needs of change param settings

CONS:
  • less customization possibilities


Enjoy

Wednesday, September 12, 2012

HTML5 - Change the URL without reloading the page

With HTML5 (compliant browsers), it is now possible to alter the URL in the address bar without reloading the page.
 This can be quite handy in many situations...
to try it quickly, run this script after the page is loaded:

 window.history.pushState("","", "heyIJustChangedMyUrlWithoutPosting");

To learn more, refer to:

A) The original article ;

 B) StackOverflow Post 1 and Post 2 ;

C) The Official W3C documentation .


 HTML5 is revealing its beauty every day a bit more!

Tuesday, September 11, 2012

Art and technology: visit the Sistine Chapel and the Uffizi Gallery in a click

I visited this places for real, years ago, and believe me, they're absolutely stunning.
But, if you are curious about them, and want to take a look without having to come to Italy,
just visit this sites and enjoy ;)


Sistine Chapel - 3D reproduction


More on the Sistine Chapel: http://en.wikipedia.org/wiki/Sistine_Chapel


The Uffizi Gallery - visit everything !!


More on the Uffizi Gallery: http://en.wikipedia.org/wiki/Uffizi