Thursday, July 24, 2014

Hibernate appends LIMIT in every query automatically

Once in a morning I woke up with a very special issue in my web application and by afternoon it almost made me to pull out my hair.
I am using Spring hibernateDAOSupport at DAO layer of my application and am using hibernateTemplate with detachedcriteria to fetch records from my DB. This time I was assigned an intermittent issue where in the query API in DAO was not able to fetch correct number of records from my DB.
I always like challenges when it comes to, and I always need a couple of coffee mugs to get in DEBUG mode.

While debugging I jumped right away on the hibernate generated SQL where I was able to trace my issue. The issue was hibernate was appending "LIMIT ?" at end of my query even though I do not intended to do this.



    DetachedCriteria detachedCriteria = DetachedCriteria.forClass(ABC.class);
    detachedCriteria.add(Restrictions.in("col1", phone));
    detachedCriteria.add(Restrictions.isNotNull("col2"));
    list = getHibernateTemplate().findByCriteria(detachedCriteria);


Do you see any setMaXResults statement? Well I also don't as like you. Then why the ??? hibernate appending "LIMIT ?" these two words as add-on and making me smash my head on wall.

Wooooooooo but I stopped there and thought Hibernate Spring can not be blamed for this it should be something which I am doing. and now I at least know where am I going wrong. So lets find solution.

Solution:

Well after certain Google try I found some links, blogs people discussing about this. Now here are some work around for the same.

1. A quick hack is to reset maxResults to null or 0 when you use setMaxResults to fetch limited records from DB

2. Another is bit wordier but a nice approach where you need not to rest max results everytime


    protected List getWrappedStuff() {
    List results = new ArrayList();
    HibernateTemplate hibernateTemplate = getHibernateTemplate();
    hibernateTemplate.setMaxResults(10);

    StringBuilder hsql = new StringBuilder("select * from table1");

    List queryResults = hibernateTemplate.find(hsql.toString());
    for (Object[] result : queryResults) {
        results.add(new myobject((String)result[0], (Long)result[1]));
    }

    return results; 
}
The question really is, is it a nice practice to use HibernateTemplate and how can we use this

All spring templates (hibernate, jdbc, rest, jpa etc.) have the same pros and cons:

Pro: They perform common setup routines for you, let you skip the boilerplate and concentrate on the logic you want.

Con: you are coupling your application tightly to the spring framework. For this reason, Spring recommends that HibernateTemplate no longer be used.

Specifically, what HibernateTemplate did for you was to automatically open and close sessions and commit or rollback transactions after your code executed. However, all of this can be achieved in an aspect-oriented way using Spring's Declarative Transaction Management.

So time for you to think.

Monday, July 21, 2014

Spring MVC serving images from physical file system

Spring MVC serving images from physical file system

Very offen people are in quest of a good solution to store and potray user images viz. personal images, signatures etc and like many people I found myself as one looking out for this kinda solution googling, binging all around internet with almost no notion.

Well, but "Ever tried. Ever failed. No matter. Try Again. Fail again. Fail better."
One nice morning I was able to find a solution for this using Java Spring.

Spring gives you a very nice way to handle how you would display images which are stored somewhere outside your web server/servlet container sandbox.

This tutorial will explain how you should configure your Spring MVC application if you want to serve resources that reside outside the webapp directory, or even outside the whole project archive. In short, how to access files from anywhere in the file system with your static resources.
You need to configure access to your static resource by putting the following line in the {servlet-context.xml} file:



Serving resources from outside the context root
Now, this is accomplished by specifying an absolute path to the resource folder in the location attribute of the resources tag:



NOTE: don't forget to put the trailing slash at the end of the absolute path. It won't work if you miss that.
Serving resources from outside the context root - Windows path



Hope this will spare you some sleepless hours, because it took me a while to figure it out myself.

After context file configuration you should be able to serve images using a simple image tag.
viz.

Good Luck

Friday, July 18, 2014

Resolving JAR hell The Hack

Resolving JAR hell The Hack

I never knew about how Java class loader works. Frankly speaking just read this line while interview preparation.

When explored I came to know how it really works, how bootstrap/system classpath makes a difference, how web app lib directory creates a significant difference and all.




Actually there is a way to solve this and this is using your custom class loader but well sometimes you run out of time when you have a crunched time and a critical delivery ahead and that's where I was standing.


I had a situation to load "XSSFWorkbook" in my web application to read data from excel sheet and had two JARS "poi-ooxml-3.9.jar" and "org.eclipse.birt.runtime_4.3.1.v20130918-1142.jar" with same class name and package structure. I simply remaned the later so in its alphabetical order comes next to the earlier one and it did a trick for me. Just wanted to share the same.

Hope this will help somebody from some sleepless nights and crunched situation


Well and curious to mention that this is my very first post so apologize for incorrect English comprehension and excited to see you all sharing knowledge.

Good Luck