Spring集成Hibernate由两种形式
1、继续使用Hibernate的映射文件*.hbm.xml
2、使用jpa形式的pojo对象, 去掉*.hbm.xml文件
一、继续使用Hibernate的映射文件*.hbm.xml
此时Spring的配置文件中的SeesionFactory需要使用org.springframework.orm.hibernate.LocalSessionFactoryBean
org.hibernate.dialect.SQLServerDialect
true
true
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.
* 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文件
example.hbm.xml
src/mypackage目录下的example.hbm.xml文件
mypackage/example.hbm.xml
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.
* 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映射文件文件
WEB-INF/mappings/example.hbm.xml
指定classpath下的example.hbm.xml映射文件
classpath:example.hbm.xml
也可以使用*作为通配符
3、mappingDirectoryLocations,指定包含映射文件的文件夹的目录
/**
* Set locations of directories that contain Hibernate mapping resources,
* like "WEB-INF/mappings".
* 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映射文件
WEB-INF/mappings
也可以通过classpath来指出,此处包含classpath路径下的hbm包下的所有*.hbm.xml文件
classpath:hbm/
4、mappingJarLocations ,指定加载的映射文件在jar文件中
/**
* Set locations of jar files that contain Hibernate mapping resources,
* like "WEB-INF/lib/example.hbm.jar".
* 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;
}
例如:
WEB-INF/lib/example.hbm.jar
二、使用jpa形式的pojo对象, 去掉*.hbm.xml文件
此时Spring的配置文件中的SeesionFactory需要使用org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean
AnnotationSessionFactoryBean中查找jpa注解形式的pojo映射对象的属性有:annotatedClasses、packagesToScan
1、annotatedClasses:指定classpath下指定的注解映射实体类的类名
/** * 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; }
com.anyview.entities.ClassTable com.anyview.entities.ClassStudentTable
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.
* 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}).
*/
public void setPackagesToScan(String... packagesToScan) {
this.packagesToScan = packagesToScan;
}