Wednesday, June 30, 2010

Hibernate's AnnotationSessionFactoryBean

These days im a bit busy with creating a new framework to be used for development of enterprise projects. I was using the hibernate template to deal with all database related activity and used the HibernateDAOSupport class as well. When configuring it at first i found it to be very cumbersome because you have to specify each and every annotated class within its configuration as such;


<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.mypackage.domain.Person</value>
</list>
</property>

</bean>


First i thought i found use xDoclet and generate this XML through the build process. But as I Googled up on the same i found that there was another way to do it in just one line which would scna all your sub packages. You just have to give your base package name and that its. Following is the re factored AnnotationSessionFactoryBean configuration;


<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<list>
<value>com.mypackage.**.*</value>
</list>
</property>

</bean>


Thats it. Now you can put your domain classes anywhere within your project and this will scan all your entities.