Spring中使用Hibernate3的Annotation

来源:互联网 发布:战舰世界大和转炮数据 编辑:程序博客网 时间:2024/05/17 05:17
Hibernate3 Annotation的SessionFactory创建需要有两个地方与用mapping resource的方式不同:

Configuration需要使用新的org.hibernate.cfg.AnnotationConfiguration
需要使用新的mappingClass元素而不是使用mappingResource
org.springframework.orm.hibernate3.LocalSessionFactoryBean 对此的支持是configurationClass属性及configLocation属性,也就是分别设置configurationClass和hibernate.cfg.xml的位置(mappingClass在配置文件中设置)。

设置的例子如下:

  <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.LocalSessionFactoryBean">
   <property name="dataSource">
     <ref bean="dataSource"/>
   </property>
   <property name="configLocation">
       <value>classpath:hibernate.cfg.xml</value>
   </property>
</bean>


org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean是专门针对这个问题的解决办法,只需要使用这个类做SessionFactory,则只需要直接设置annotatedClasses属性即可。设置的例子如下:

  <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
   <property name="dataSource">
     <ref bean="dataSource"/>
   </property>
   <property name="annotatedClasses">
     <list>
       <value>test.package.Foo</value>
       <value>test.package.Bar</value>
     </list>
   </property>
</bean>