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;
  public abstract Pager selectByAgeGreaterThanPaged(
    Integer age, int pageSize)
    throws DataAccessException;
  public abstract List selectByAgeGreaterThanList(
    Integer age, int pageSize, int page)
    throws DataAccessException;
  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

Spring and DAO w/ Annotation

3 thoughts on “Spring and DAO w/ Annotation

  1. […] 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…) […]

  2. 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…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s