Oct 29
Quick Tip: Using Spring and Hibernate Annotations
Hibernate Annotations provides a complete implementation of EJB 3 / JPA-compatible annotations. It is possible to use Hibernate annotations with the native Hibernate API. You use Hibernate annotated classes in the same way that you would use XML mapping files. This is a great strategy if you’d like to prepare for a transition to JPA, but aren’t quite ready due to some of the known deficiencies in the API.
It is also possible to tie the annotated classes (and Hibernate) together with Spring’s transaction management. Unfortunately, the Spring documentation on ORM using Hibernate doesn’t cover how to do this. I’ll show you how, below.
To use annotated classes instead of XML mapping files, simply reference Hibernate like so in your Spring XML configuration file (note this requires Spring 2.0+) (line breaks denoted with »):
<bean id="sessionFactory" class="<strong>org.springframework.orm.hibernate3. »
annotation.AnnotationSessionFactoryBean</strong>">
<property name="dataSource" ref="dataSource"></property>
<property name="annotatedClasses">
<list>
<value>com.foo.Employee</value>
...
In describing this, I assume that you’ve already got your basic Spring and Hibernate configuration working (there are plenty of great examples available if you don’t).
And that’s basically it – all the classes listed in “annotatedClasses” need to have JPA-compatible annotations (@Entity or @MappedSuperclass). This allows you to avoid heavy XML mappings and take one step closer to JPA / Java EE 5.



January 23rd, 2009 at 10:11 am
<bean id=”sessionFactory” class=”org.springframework.orm.hibernate3. » annotation.AnnotationSessionFactoryBean“> <property name=”dataSource” ref=”dataSource”></property> <property name=”annotatedClasses”> <list> <value>com.foo.Employee</value>