Spring整合Hibernate之AnnotationSessionFactoryBean与LocalSessionFactoryBean

spring集成hibernate由两种形式

1、继续使用Hibernate的映射文件*.hbm.xml

2、使用jpa形式的pojo对象, 去掉*.hbm.xml文件

一、继续使用Hibernate的映射文件*.hbm.xml

此时Spring的配置文件中的SeesionFactory需要使用org.springframework.orm.hibernate.LocalSessionFactoryBean

[html] view plain copy

  1. <bean id="sessionFactory"
  2. class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
  3. <property name="dataSource" ref="dataSource" />
  4. <property name="hibernateProperties">
  5. <props>
  6. <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
  7. <prop key="hibernate.show_sql">true</prop>
  8. <prop key="hibernate.format_sql">true</prop>
  9. </props>
  10. </property>
  11. </bean>

LocalSessionFactoryBean可以用如下几个属性来寻找*hbm.xml文件

mappingResources、mappingLocations、mappingDirectoryLocations与mappingJarLocations

1、mappingResources:指定classpath下具体映射文件名

[java] view plain copy

  1. /**
  2. * Set Hibernate mapping resources to be found in the class path,
  3. * like "example.hbm.xml" or "mypackage/example.hbm.xml".
  4. * Analogous to mapping entries in a Hibernate XML config file.
  5. * Alternative to the more generic setMappingLocations method.
  6. * <p>Can be used to add to mappings from a Hibernate XML config file,
  7. * or to specify all mappings locally.
  8. * @see #setMappingLocations
  9. * @see org.hibernate.cfg.Configuration#addResource
  10. */
  11. public void setMappingResources(String... mappingResources) {
  12. this.mappingResources = mappingResources;
  13. }

src根目录下的example.hbm.xml文件

[html] view plain copy

  1. <property name="mappingResources">
  2. <value>example.hbm.xml </value>
  3. </property>

src/mypackage目录下的example.hbm.xml文件

[html] view plain copy

  1. <property name="mappingResources">
  2. <value>mypackage/example.hbm.xml</value>
  3. </property>

2、mappingLocations,可以指定映射文件的路径,可以指定classpath路径下和其他文件夹下的映射文件

[java] view plain copy

  1. /**
  2. * Set locations of Hibernate mapping files, for example as classpath
  3. * resource "classpath:example.hbm.xml". Supports any resource location
  4. * via Spring‘s resource abstraction, for example relative paths like
  5. * "WEB-INF/mappings/example.hbm.xml" when running in an application context.
  6. * <p>Can be used to add to mappings from a Hibernate XML config file,
  7. * or to specify all mappings locally.
  8. * @see org.hibernate.cfg.Configuration#addInputStream
  9. */
  10. public void setMappingLocations(Resource... mappingLocations) {
  11. this.mappingLocations = mappingLocations;
  12. }

指定WEB-INF/mappings目录下的example.hbm.xml映射文件文件

[java] view plain copy

  1. <property name="mappingLocations">
  2. <value>WEB-INF/mappings/example.hbm.xml </value>
  3. </property>

[html] view plain copy

  1. </pre><pre>

指定classpath下的example.hbm.xml映射文件

[html] view plain copy

  1. <property name="mappingLocations">
  2. <value>classpath:example.hbm.xml </value>
  3. </property>

也可以使用*作为通配符

3、mappingDirectoryLocations,指定包含映射文件的文件夹的目录

[java] view plain copy

  1. /**
  2. * Set locations of directories that contain Hibernate mapping resources,
  3. * like "WEB-INF/mappings".
  4. * <p>Can be used to add to mappings from a Hibernate XML config file,
  5. * or to specify all mappings locally.
  6. * @see org.hibernate.cfg.Configuration#addDirectory(java.io.File)
  7. */
  8. public void setMappingDirectoryLocations(Resource... mappingDirectoryLocations) {
  9. this.mappingDirectoryLocations = mappingDirectoryLocations;
  10. }

包含WEB-INF/mappings目录下的所有*hbm.xml映射文件

[html] view plain copy

  1. <property name="mappingDirectoryLocations">
  2. <list>
  3. <value>WEB-INF/mappings</value>
  4. </list>
  5. </property>

也可以通过classpath来指出,此处包含classpath路径下的hbm包下的所有*.hbm.xml文件

[html] view plain copy

  1. <property name="mappingDirectoryLocations">
  2. <list>
  3. <value>classpath:hbm/</value>
  4. </list>
  5. </property>

4、mappingJarLocations ,指定加载的映射文件在jar文件中

[java] view plain copy

  1. /**
  2. * Set locations of jar files that contain Hibernate mapping resources,
  3. * like "WEB-INF/lib/example.hbm.jar".
  4. * <p>Can be used to add to mappings from a Hibernate XML config file,
  5. * or to specify all mappings locally.
  6. * @see org.hibernate.cfg.Configuration#addJar(java.io.File)
  7. */
  8. public void setMappingJarLocations(Resource... mappingJarLocations) {
  9. this.mappingJarLocations = mappingJarLocations;
  10. }

例如:

[java] view plain copy

  1. <property name="mappingDirectoryLocations">
  2. <value>WEB-INF/lib/example.hbm.jar</value>
  3. </property>

[html] view plain copy

  1. </pre><p></p><p><span style="line-height:30px"><span style="line-height:21px"><span style="font-family: FangSong_GB2312; font-size: 18px;"></span></span></span></p><p></p><p>二、使用jpa形式的pojo对象, 去掉*.hbm.xml文件</p><p>此时Spring的配置文件中的SeesionFactory需要使用<span style="color:rgb(255,0,0);">org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean</span></p><p>AnnotationSessionFactoryBean中查找jpa注解形式的pojo映射对象的属性有:annotatedClasses、packagesToScan</p><p>1、annotatedClasses:指定classpath下指定的注解映射实体类的类名</p><p><pre name="code" class="java">/**
  2. * Specify annotated classes, for which mappings will be read from
  3. * class-level annotation metadata.
  4. * @see org.hibernate.cfg.AnnotationConfiguration#addAnnotatedClass(Class)
  5. */
  6. public void setAnnotatedClasses(Class<?>... annotatedClasses) {
  7. this.annotatedClasses = annotatedClasses;
  8. }

[html] view plain copy

  1. <property name="annotatedClasses"> <list> <value>com.anyview.entities.ClassTable</value> <value>com.anyview.entities.ClassStudentTable</value> </list> </property>

2、 packagesToScan指定映射文件的包名

[java] view plain copy

  1. /**
  2. * Specify packages to search using Spring-based scanning for entity classes in
  3. * the classpath. This is an alternative to listing annotated classes explicitly.
  4. * <p>Default is none. Specify packages to search for autodetection of your entity
  5. * classes in the classpath. This is analogous to Spring‘s component-scan feature
  6. * ({@link org.springframework.context.annotation.ClassPathBeanDefinitionScanner}).
  7. */
  8. ublic void setPackagesToScan(String... packagesToScan) {
  9. this.packagesToScan = packagesToScan;

[html] view plain copy

    1. <property name="packagesToScan" value="com/anyview/entities/"></property></strong></span>
时间: 2024-08-25 14:56:49

Spring整合Hibernate之AnnotationSessionFactoryBean与LocalSessionFactoryBean的相关文章

Spring整合hibernate(1)之基础整合

Spring整合hibernate3之基础整合 Spring集成hibernate3和4有一定的区别,目前基本都在使用3,所以此处内容以3为基础: 1.导入hibernate的包和Spring的包 1.1.导入Spring的依赖包 1.2.导入Log4j的依赖包:log4j-1.2.16.jar 1.3.导入dbcp的依赖包:commons-dbcp-1.4.jar.commons-pool-1.5.6.jar 1.4.导入hibernate3的依赖包 hibernate全部版本地址:http:

Spring框架学习(4)spring整合hibernate

内容源自:spring整合hibernate    spring整合注解形式的hibernate 这里和上一部分学习一样用了模板模式, 将hibernate开发流程封装在ORM层提供的模板类HibernateTemplate中,通过在DAO中对模板类的使用,实现对传统hibernate开发流程的代替. 一.先来看看Hibernate的传统开发流程: 1) 配置SessionFactory对象 hibernate.cfg.xml <session-factory> a 数据源 driver_cl

Spring整合Hibernate中自动建表

Spring整合Hibernate中自动建表 博客分类: JavaEE Java代码   <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource" /> <

3、Spring整合Hibernate

经过前面的两节分析:1.Hibernate之生成SessionFactory源码追踪 和 2.Spring的LocalSessionFactoryBean创建过程源码分析 .我们可以得到这样一个结论,spring的LocalSessionFactoryBean具体是调用Hibernate的Configuration中configure(...)方法来读取并解析xxx.cfg.xml文件的,同样也会得到一个原生态的org.hibernate.cfg.Configuration 和 org.hibe

Spring学习(五)spring整合hibernate

上一篇博客中讲到spring dao层对jdbc的封装,用到了模板模式的设计思想 .这篇我们来看看spring中的orm层对hibernate的封装,也就是所谓的spring整合 hibernate.这里同样用了模板模式, 将hibernate开发流程封装在ORM层提供的模板类HibernateTemplate中,通过在DAO中对模板类的使用,实现对传统hibernate开发流程的代替. 一.先来看看Hibernate的传统开发流程: 1) 配置SessionFactory对象 hibernat

Spring 整合 Hibernate

Spring 整合 Hibernate •Spring 支持大多数流行的 ORM 框架, 包括 Hibernate JDO, TopLink, Ibatis 和 JPA. •Spring 对这些 ORM 框架的支持是一致的, 因此可以把和 Hibernate 整合技术应用到其他 ORM 框架上. •Spring 2.0 同时支持 Hibernate 2.x 和 3.x. 但 Spring 2.5 只支持 Hibernate 3.1 或更高版本 1.Spring 整合 Hibernate 整合什么

Spring 整合hibernate和mybatis的 applicationContext.xml的配置

Spring整合hibernate的applicationContext.xml配置 1.注解方式的实现 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" x

Spring整合Hibernate详细步骤

阅读目录 一.概述 二.整合步骤 回到顶部 一.概述 Spring整合Hibernate有什么好处? 1.由IOC容器来管理Hibernate的SessionFactory 2.让Hibernate使用上Spring的声明式事务 回到顶部 二.整合步骤 整合前准备: 持久化类: @Entity public class Book { private Integer id; private String bookName; private String isbn; private int pric

尚硅谷Spring整合Hibernate基于xml配置

描述:这是一个最简单网上书城demo. 下载地址:http://download.csdn.net/detail/u013488580/8370899 1. Spring 整合 Hibernate 整合什么 ? 1). 有 IOC 容器来管理 Hibernate 的 SessionFactory 2). 让 Hibernate 使用上 Spring 的声明式事务 2. 整合步骤: 1). 加入 hibernate ①. jar 包 ②. 添加 hibernate 的配置文件: hibernate