Spring整合Hibernate之AnnotationSessionFactoryBean与LocalSessionFactoryBean
1、继续使用Hibernate的映射文件*.hbm.xml
2、使用jpa形式的pojo对象, 去掉*.hbm.xml文件
一、继续使用Hibernate的映射文件*.hbm.xml
此时Spring的配置文件中的SeesionFactory需要使用org.springframework.orm.hibernate.LocalSessionFactoryBean
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <property name="hibernateProperties">
- <props>
- <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
- <prop key="hibernate.show_sql">true</prop>
- <prop key="hibernate.format_sql">true</prop>
- </props>
- </property>
- </bean>
LocalSessionFactoryBean可以用如下几个属性来寻找*hbm.xml文件
mappingResources、mappingLocations、mappingDirectoryLocations与mappingJarLocations
1、mappingResources:指定classpath下具体映射文件名
- /**
- * Set Hibernate mapping resources to be found in the class path,
- * like "example.hbm.xml" or "mypackage/example.hbm.xml".
- * Analogous to mapping entries in a Hibernate XML config file.
- * Alternative to the more generic setMappingLocations method.
- * <p>Can be used to add to mappings from a Hibernate XML config file,
- * or to specify all mappings locally.
- * @see #setMappingLocations
- * @see org.hibernate.cfg.Configuration#addResource
- */
- public void setMappingResources(String... mappingResources) {
- this.mappingResources = mappingResources;
- }
src根目录下的example.hbm.xml文件
- <property name="mappingResources">
- <value>example.hbm.xml </value>
- </property>
src/mypackage目录下的example.hbm.xml文件
- <property name="mappingResources">
- <value>mypackage/example.hbm.xml</value>
- </property>
2、mappingLocations,可以指定映射文件的路径,可以指定classpath路径下和其他文件夹下的映射文件
- /**
- * Set locations of Hibernate mapping files, for example as classpath
- * resource "classpath:example.hbm.xml". Supports any resource location
- * via Spring's resource abstraction, for example relative paths like
- * "WEB-INF/mappings/example.hbm.xml" when running in an application context.
- * <p>Can be used to add to mappings from a Hibernate XML config file,
- * or to specify all mappings locally.
- * @see org.hibernate.cfg.Configuration#addInputStream
- */
- public void setMappingLocations(Resource... mappingLocations) {
- this.mappingLocations = mappingLocations;
- }
指定WEB-INF/mappings目录下的example.hbm.xml映射文件文件
- <property name="mappingLocations">
- <value>WEB-INF/mappings/example.hbm.xml </value>
- </property>
- </pre><pre>
指定classpath下的example.hbm.xml映射文件
- <property name="mappingLocations">
- <value>classpath:example.hbm.xml </value>
- </property>
也可以使用*作为通配符
3、mappingDirectoryLocations,指定包含映射文件的文件夹的目录
- /**
- * Set locations of directories that contain Hibernate mapping resources,
- * like "WEB-INF/mappings".
- * <p>Can be used to add to mappings from a Hibernate XML config file,
- * or to specify all mappings locally.
- * @see org.hibernate.cfg.Configuration#addDirectory(java.io.File)
- */
- public void setMappingDirectoryLocations(Resource... mappingDirectoryLocations) {
- this.mappingDirectoryLocations = mappingDirectoryLocations;
- }
包含WEB-INF/mappings目录下的所有*hbm.xml映射文件
- <property name="mappingDirectoryLocations">
- <list>
- <value>WEB-INF/mappings</value>
- </list>
- </property>
也可以通过classpath来指出,此处包含classpath路径下的hbm包下的所有*.hbm.xml文件
- <property name="mappingDirectoryLocations">
- <list>
- <value>classpath:hbm/</value>
- </list>
- </property>
4、mappingJarLocations ,指定加载的映射文件在jar文件中
- /**
- * Set locations of jar files that contain Hibernate mapping resources,
- * like "WEB-INF/lib/example.hbm.jar".
- * <p>Can be used to add to mappings from a Hibernate XML config file,
- * or to specify all mappings locally.
- * @see org.hibernate.cfg.Configuration#addJar(java.io.File)
- */
- public void setMappingJarLocations(Resource... mappingJarLocations) {
- this.mappingJarLocations = mappingJarLocations;
- }
例如:
- <property name="mappingDirectoryLocations">
- <value>WEB-INF/lib/example.hbm.jar</value>
- </property>
- </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">/**
- * Specify annotated classes, for which mappings will be read from
- * class-level annotation metadata.
- * @see org.hibernate.cfg.AnnotationConfiguration#addAnnotatedClass(Class)
- */
- public void setAnnotatedClasses(Class<?>... annotatedClasses) {
- this.annotatedClasses = annotatedClasses;
- }
- <property name="annotatedClasses"> <list> <value>com.anyview.entities.ClassTable</value> <value>com.anyview.entities.ClassStudentTable</value> </list> </property>
2、 packagesToScan指定映射文件的包名
- /**
- * Specify packages to search using Spring-based scanning for entity classes in
- * the classpath. This is an alternative to listing annotated classes explicitly.
- * <p>Default is none. Specify packages to search for autodetection of your entity
- * classes in the classpath. This is analogous to Spring's component-scan feature
- * ({@link org.springframework.context.annotation.ClassPathBeanDefinitionScanner}).
- */
- ublic void setPackagesToScan(String... packagesToScan) {
- this.packagesToScan = packagesToScan;
- <property name="packagesToScan" value="com/anyview/entities/"></property></strong></span>
Spring整合Hibernate之AnnotationSessionFactoryBean与LocalSessionFactoryBean的更多相关文章
- Spring框架学习(4)spring整合hibernate
内容源自:spring整合hibernate spring整合注解形式的hibernate 这里和上一部分学习一样用了模板模式, 将hibernate开发流程封装在ORM层提供的模板类Hiber ...
- Spring整合Hibernate报错:annotatedClasses is not writable or has an invalid setter method
Spring 整合Hibernate时报错: org.springframework.beans.factory.BeanCreationException: Error creating bean ...
- 【Java EE 学习 53】【Spring学习第五天】【Spring整合Hibernate】【Spring整合Hibernate、Struts2】【问题:整合hibernate之后事务不能回滚】
一.Spring整合Hibernate 1.如果一个DAO 类继承了HibernateDaoSupport,只需要在spring配置文件中注入SessionFactory就可以了:如果一个DAO类没有 ...
- spring整合hibernate的详细步骤
Spring整合hibernate需要整合些什么? 由IOC容器来生成hibernate的sessionFactory. 让hibernate使用spring的声明式事务 整合步骤: 加入hibern ...
- spring整合hibernate
spring整合hibernate包括三部分:hibernate的配置.hibernate核心对象交给spring管理.事务由AOP控制 好处: 由java代码进行配置,摆脱硬编码,连接数据库等信息更 ...
- Spring 整合 Hibernate
Spring 整合 Hibernate •Spring 支持大多数流行的 ORM 框架, 包括 Hibernate JDO, TopLink, Ibatis 和 JPA. •Spring 对这些 OR ...
- 使用Spring整合Hibernate,并实现对数据表的增、删、改、查的功能
1.1 问题 使用Spring整合Hibernate,并实现资费表的增.删.改.查. 1.2 方案 Spring整合Hibernate的步骤: 1.3 步骤 实现此案例需要按照如下步骤进行. 采用的环 ...
- Spring整合Hibernate详细步骤
阅读目录 一.概述 二.整合步骤 回到顶部 一.概述 Spring整合Hibernate有什么好处? 1.由IOC容器来管理Hibernate的SessionFactory 2.让Hibernate使 ...
- SSH整合之spring整合hibernate
SSH整合要导入的jar包: MySQL中创建数据库 create database ssh_db; ssh_db 一.spring整合hibernate带有配置文件hibernate.cfg.xml ...
随机推荐
- scala的tcp通信
client: object ActorClient extends App { import actors.Actor, actors.remote.Node, actors.remote.Remo ...
- android 入门-本地化语言
打包安装到手机上,改变手机系统语言,你在看看是不是改变了.哇.真的可以.
- [Unity3d插件]EasyTouch的初步使用
对于移动平台上的RPG类的游戏,我们常用虚拟摇杆来控制人物角色的行走和一些行为,相信我们对它并不陌生,之前尝试了EasyTouch2.5,发现并没有最新版的3.1好用,2.5版本的对于自适应没有做的很 ...
- [unity3d插件]2dtoolkit系列一 创建精灵
从今天开始要做一个2d游戏,由于之前都是做cocos2dx的,然后接触了一段时间的unity3d,都是做3D方面的东西,得知要做2d游戏还是有点开心的,或许因为不想丢失之前的2d游戏的一些思想,然后接 ...
- 【项目经验】--EasyUI DataGrid之右键菜单
前两天验收项目,老总提了一个不是需求的需求,为什么这么说呢?因为我们的管理不到位!话说当天,我们UI系统下发了一个总文件,上面写着"各个系统找一个没有添加UI的模块去添加最新版本UI进行测试 ...
- 在MySQL中存储大文件
我们的目标:把一首mp3保存到MySQL数据库中! 由于MySQL默认当存入的数据太大时会抛异常,所以应在my.ini中添加如下配置!max_allowed_packet=10485760,这样,可以 ...
- Attribute在.net编程中的应用
Attribute FYI Link: Attribute在.net编程中的应用(一) Attribute在.net编程中的应用(二) Attribute在.net编程中的应用(三) Attribut ...
- Uva 10976 Fractions Again?!
直接暴力 没技巧 y应该从k+1开始循环,因为不然y-k<0的时候 你相当于(x*y) % (负数) 了. #include <iostream> using namespace s ...
- spring boot 打包成jar 包在发布到服务器上
http://blog.csdn.net/sai739295732/article/details/49444447
- 并查集(涂色问题) HDOJ 4056 Draw a Mess
题目传送门 题意:给出一个200 * 50000的像素点矩阵,执行50000次操作,每次把一个矩形/圆形/菱形/三角形内的像素点涂成指定颜色,问最后每种颜色的数量. 分析:乍一看,很像用线段树成段更新 ...