Spring and DAO w/ Annotation

Although EJB is moving forward to 3.0, and everyone seems to enjoy the POJO ORM stuff. I do miss some EJB 2.0 features. Particularly, being able to use the ejbSelect and finder methods! With xdoclet, one could easily annotate a method with its corresponding query.


/**
  * @ejb.select query="...."
  */
public abstract Collection ejbSelectWithKey(String key)
  throws FinderException;

Although it seems like named queries are to replace the use of this kind of methods. I still enjoyed the fact that the method is, afterall, a Java method. The Java language can provide name, parameter, and type checkings to this method, where named queries can't.

I could imaging something similar w/ Spring and DAO, and some simple AOP/proxy to achieve the following:


public abstract class PersonDAOHibernate implements PersonDAO
{
  @HQL("from Person p where p.id = :id");
  @InitializeProperties({"accounts", "projects"});
  public abstract Person getById(Long id)
    throws DataAccessException;
  @HQL("from Person p where p.age > 20");
  @Paged(pageSize = 10);
  public abstract Pager selectQualifiedAdults()
    throws DataAccessException;
  @HQL("from Person p where p.name like '%:name%'")
  @Paged(pageSize = 15);
  public abstract Pager selectByNameLike(String name)
    throws DataAccessException;
  @HQL(namedQuery="person.ageGreaterThan")
  @Paged
  public abstract Pager selectByAgeGreaterThanPaged(
    Integer age, int pageSize)
    throws DataAccessException;
  @HQL(namedQuery="person.ageGreaterThan")
  @PagedList
  public abstract List selectByAgeGreaterThanList(
    Integer age, int pageSize, int page)
    throws DataAccessException;
  @HQL(namedQuery="person.ageGreaterThan")
  @PagedList
  public abstract PagedList selectByAgeGreaterThanList(
    Integer age, int pageSize, int page)
    throws DataAccessException;
}

Different implementations can use different annotation for the queries e.g. @SQL, @EQL, etc. Not only can it embed a query, it can also link to named queries.

Without the Open Session In View pattern, it is necessary to load all the data that is required in upper tiers callers. There is a feature in XDoclet for EJB 2.0 to generate value objects that does this pretty well. However, in the recent development of ORM, it seems there is a need to manually initialize the lazily-loaded fields for each DAO methods (if not using Open Session In View). In this example, fields initialization can be explicitly specified using @InitializeProperties.

Lastly, when the query string and parameters are known, it can auto-magically create a Pager object, which can dynamically load a page of items when called.

Updated May 13, 2006 – See here

4 Responses to “Spring and DAO w/ Annotation”

  1. Ray Tsang’s Blog » Blog Archive » Spring and DAO w/ Annotation - It works! Says:

    [...] I finally got some time to implement the idea that I mentioned in previous blog. After struggling for hours with Spring AOP, I realized I needed CGLIB to proxy abstract class! [...]

  2. Ray Tsang’s Blog » Blog Archive » Stripes on Rail Says:

    [...] In this architecture, one is free to implement their own DAO, Manager, or ActionBean. We can make the DAO implementation job easier by introducing Abstract DAO annotation from previous idea and implementation. So in case one really needs to do some custom queries, one can annotate the query to an abstract method, rather than getting into Hibernate details (getHibernateTemplate()… throw exceptions…) [...]

  3. Rodrigo Urubatan's Blog Says:

    Spring-Annotation v1.0.2 just released

    Some additions to this new version:
    * Support for XML Schema Configuration, now you just need to put the tag <sa:annotation-autoload> to enable the annotation module
    * There is no need to create the empty to.properties anymore if all your classe…

  4. Urubatan`s Weblog Says:

    Acabou de ser liberada a versão 1.0.2 do Spring-Annotation

    Algumas novidades desta versão::
    * Suporte a configuração baseada em XML Schema, agora basta adicionar a tag <sa:annotation-autoload> em qualquer arquivo de configuração do spring para habilitar o modulo de anotações.
    * não é mais necess…

Leave a Reply