Spring and DAO w/ Annotation – It works!

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!

It took me a while to learn a bit about Spring's AOP framework. After hours of struggling with words such as Attribute, Definition, Config, Advisor, Source, ObjectSourceDefinition, I finally realized that those were not the problems.

Apparently Spring AOP can only proxy a real instantiated object, i.e. non-abstract class. But the idea is to use the abstract class and let someone else implement it using information provided with @HQL annotation.

So I switched to CGLIB, setting super class to the abstract class, and intercept calls to abstract methods that are annotated.

This is what I get so far….

DAO Code:


public abstract class PersonDAOHibernate
  extends HibernateDaoSupport
  implements PersonDAO
{
  @HQL("from Person p where p.name = :arg1")
  public abstract List<Person> findByName(String name);
  @HQL("from Person")
  public abstract List<Person> findAll();
}

Because the method argument names are not accessible in compiled code, I had the resolve to naming by argument index numbers (starting from 1). I think I'll also add argument/parameter level annotations that can specify parameter names:


@HQL("from Person p where p.name = :name");
public abstract List findByName(@NamedParam("name") String name);

Right now it recognizes return types of List and of the domain class for select/find operations. For updates, it recognizes return types of void and integer (for returning counts)

The Spring configuration currently looks like the following:


<bean id="personDao"
  class="com.nooton.spring.dao.hibernate3.annotation
    .AnnotationHibernateDAOProxyFactoryBean">
  <property name="targetClass"
    value="dao.hibernate.PersonDAOHibernate"/>
  <property name="sessionFactory"
    ref="sessionFactory"/>
</bean>
Spring and DAO w/ Annotation – It works!

One thought on “Spring and DAO w/ Annotation – It works!

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

Leave a reply to Ray Tsang’s Blog » Blog Archive » Stripes on Rail Cancel reply