Archive for the ‘AOP/IoC’ Category

Spring binding many-to-many

Sunday, March 4th, 2007

Wow. This was a lot more complicated than I thought. After several days of research and questions I finally discovered how to make this work. Its all about custom property editors. Please note that many-to-many relationships are not the best thing to use in a production environment. They dont scale well and they are not easy to manage. I later on replaced with Solr, my new favorite implementation.

In this example, many ‘things’ have many ‘tags’ and inversely many ‘tags’ have many ‘things’.

protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
   binder.registerCustomEditor(Set.class, “tags”, new CustomCollectionEditor(Set.class) {
     protected Object convertElement(Object element) {
       long id = NumberUtils.parseNumber((String) element, Long.class).longValue();
       Tag tag = tagMgr.getTag(element.toString());

       return new Tag(id, tag.getName);
     }
   });
}

Spring pagination support for Hiberate

Thursday, May 18th, 2006

As I have said before I love Spring and Hibernate; however, there is no Spring support for Hibernate’s pagination API. Why am I am not sure. Maybe they are supporting it now but heres what I did. First I made and abstract object I called ‘Page’. I also have a Solr subclass of this object but I will cover that later.


public abstract class Page {

    public List results;
    private int pageNumber;
    private int resultsPerPage;
    private int totalCount;
    private String queryString;
    private Integer totalPages = null;

    /*
        Getters and setters
    */

}


public class PageHibernate extends Page implements HibernateCallback {

    public PageHibernate() { }

    public PageHibernate(String queryString, int page, int resultsPerPage) {
        this.setPageNumber(page);
        this.setResultsPerPage(resultsPerPage);
        this.setQueryString(queryString);
    }


    public final Object doInHibernate(Session session)
            throws HibernateException {

        Query query = session.createQuery(this.getQueryString());
        query.setFirstResult((getPageNumber() - 1) * getResultsPerPage());
        query.setMaxResults(getResultsPerPage());

        return this.results = query.list();
    }
}


Since Spring abstracts your Hibernate interaction with HibernateDaoSupport you dont have to write straight criteria calls to Hibernate if you dont want to. Spring does create simple implementation to allow to write straight HQL and many other functions but not pagination. Rather than tackle this problem at this angle I decided to make the abstract class ‘Page’ that you see above for all my data stores. Anyway, here’s how easy it is to then hit the database with this object properly constructed and have it returned ready for use.


public Page getPageOfThings(int page, int resultsPerPage, String orderBy) {

    PageHibernate newPage = new PageHibernate(
            ”FROM Thing”, page, resultsPerPage);

    getHibernateTemplate().executeFind(newPage);

    List count = getHibernateTemplate().find(”SELECT count(*) FROM Thing”);

    newPage.setTotalCount(Integer.parseInt(count.get(0).toString()));

    return newPage;

}

Spring and Hibernate

Sunday, March 12th, 2006

Spring and Hibernate make Java web application development so much easier I barely remember what I did without them. Despite the steep learning curve, Hibernate especially so, they work so well together thanks to Spring. Spring is probably the most “open” open source framework out there for Java. It can and will play nice with tons of other frameworks like Tiles, SiteMesh, Velocity, JSF, as well as backend technologies like iBatis, Hibernate, ACEGI, and countless others.

I have begun to like them so much at the office that I am using it a home in all of my side projects. This blog will hopefully shed some light on some of these technologies to hopefully give back. Blogs like this are especially important because although these frameworks aren’t relatively new, there can be some holes in the documentation. Two books that helped me through my trouble were Matt Raible’s book “Spring Live!” and “Hibernate in Action”