29 January 2008

HashMap JDBC Template Problem
How TO?

Josuha Bloch implements famous Data Structure Map for Java.
Lets a bit revise the issue.

HashMap works as below
You have a load factor.
And a number of entries for a table.
When load factor loads then new number of entries created for new objects.

Lets say your table has 16 entries for default and load factor 0.75
if 16*3/4 of your table is full. Then new 16 empty entry will be created for your HashMaps new key Objects.

But before loading 75% of your table if keys collide.
Then Existing HashMap key will point to new key.

Open your Fovorite IDE and Run the Code below.

HashMap map = new HashMap();
map.put("status", "1");
map.put("startDate", "2");
map.put("endDate", "3");
map.put("msisdn", "4");
System.out.println("size : " + map.size());

Everything will go fine.
And put a break point to the line of System.out.println("size : " + map.size());
and run again.
When debugger stops execution..
Inspect Map value. You will see that Altough you have 4 keys you will see
3 Objects. Since keys endDate and status collide. (You can also swap sequence of them.)



Until Here All expected.
But;

While Using Spring Framework You Should Consider on Collision of HashMap.
Since When you extend StoredProcedure Class inorder to use JDBC Template
You require to use statement below.

Map resultMap = super.execute(inputs);

here inputs object is the HashMap which owns collide obejct inside the HashMap.
Then JDBC Template will bomb. And You will see SQL Exception.

Then Your innocent PLSQL Procedure or function will be suspcious.
Because it will give you an error smthg like this.

You are trying to call a procedure with 3 parameters , invalid number of parameters or invalid types used...

Please take care on this. :)

My Title is composed of Google search vocab. therefore
Don't care so much :)

No comments: