Hibernate 配置详解(12) 其实我也不想用这么土的名字
hibernate.hbm2ddl.import_files
这个配置用于在hibernate根据映射文件执行DDL之前,如果我们自己设置了要事先运行的SQL文件,hibernate就会先执行这些SQL文件。比如,在classpath下面任意创建一个SQL文件:ddl.sql,然后添加:
create table CC(id bigint(19) primary key auto_increment,name varchar(255));
接着配置:
#也可以写成/ddl.sql
hibernate.hbm2ddl.import_files=ddl.sql
然后设置
#设置为hibernate.hbm2ddl.auto=create-drop也行
hibernate.hbm2ddl.auto=create
关于hibernate.hbm2ddl.auto请看这个文章:
http://blog.csdn.net/stefwu/article/details/10538385
再启动hibernate,就能看到在创建Hibernate管理的表之前,就已经创建好了CC这个表。
这个配置原理很简单了,说说注意的几个点:
1,这个配置是可以添加多个导入文件的,文件之间只需要使用逗号分隔即可;注意就是这些文件的执行是有顺序的,配置在前面的文件肯定先执行了;
2,这个配置如果不写,那么,相当于默认配置了一个/import.sql;
3,只有在hibernate.hbm2ddl.auto=create或者create-drop的时候才会先执行SQL文件,可以看下面代码:
在SessionFactoryImpl的构造方法里面:
if ( settings.isAutoCreateSchema() ) {
new SchemaExport( serviceRegistry, cfg )
.setImportSqlCommandExtractor( serviceRegistry.getService( ImportSqlCommandExtractor.class ) )
.create( false, true );
}
if ( settings.isAutoUpdateSchema() ) {
new SchemaUpdate( serviceRegistry, cfg ).execute( false, true );
}
if ( settings.isAutoValidateSchema() ) {
new SchemaValidator( serviceRegistry, cfg ).validate();
}
if ( settings.isAutoDropSchema() ) {
schemaExport = new SchemaExport( serviceRegistry, cfg )
.setImportSqlCommandExtractor( serviceRegistry.getService( ImportSqlCommandExtractor.class ) );
}
4,文件寻找的路径是先寻找classpath,再找Environment类路径,再找加载Hibernate.jar的classLoad路径,可以看ConfigHelper里面的:
public static InputStream getResourceAsStream(String resource) {
String stripped = resource.startsWith("/") ?
resource.substring(1) : resource;
InputStream stream = null;
ClassLoader classLoader = ClassLoaderHelper.getContextClassLoader();
if (classLoader!=null) {
stream = classLoader.getResourceAsStream( stripped );
}
if ( stream == null ) {
stream = Environment.class.getResourceAsStream( resource );
}
if ( stream == null ) {
stream = Environment.class.getClassLoader().getResourceAsStream( stripped );
}
if ( stream == null ) {
throw new HibernateException( resource + " not found" );
}
return stream;
}
5,默认情况下,放在SQL文件中的SQL语句,只能是一条SQL一行,不能格式化的。
再说说看代码的心情,其实有的时候看别人的代码可以发现其实还是会有一些小瑕疵可以博得一笑,有兴趣的可以看看hibernate.hbm2ddl.import_files这个配置的加载过程。还有时候可以看到别人代码上面的注释,有的也是挺有爱的。
Hibernate 配置详解(12) 其实我也不想用这么土的名字的更多相关文章
- Hibernate 配置详解(12) 补充
hibernate.hbm2ddl.import_files_sql_extractor 这个配置项用于补充这篇文章: http://blog.csdn.net/stefwu/article/deta ...
- Hibernate 配置详解(9)
hibernate.cache.use_structured_entries Hibernate文档上介绍,该属性是用于把对象以一种更易读的方式放到二级缓存中,这样,在对二级缓存进行监控的时候就更容易 ...
- Hibernate 配置详解(2)
6) hibernate.session_factory_name: 配置一个JNDI名称,通过Configuration对象创建的SessionFactory会绑定到JNDI下该名称中.一般名字格式 ...
- Hibernate 配置详解(5)
9) hibernate.batch_fetch_style: 该配置是hibernate4.2.0新添加的,使用这个设置可以配置hibernate在做batch-fetch的时候,生成SQL的策略. ...
- Hibernate 配置详解(8)
hibernate.generate_statistics 这个配置大家应该都很熟悉,用于开启Hibernate统计信息,便于对Hibernate相关性能调试提供数据依据.在开发过程当中,可以把这个选 ...
- Hibernate 配置详解(7)
hibernate.order_updates: Hibernate文档中提到,该配置用于在刷新一级缓存,提交UPDATE的时候,按照每类对象的主键顺序排序后再提交,可以在高并发情况下减少事务死锁的可 ...
- Hibernate 配置详解(11)
hibernate.session_factory_name_is_jndi 配置hibernate.cfg.xml中SessionFactory的name属性是否作为JNDI名称绑定.默认是true ...
- hibernate二级缓存ehcache hibernate配置详解
<!-----------------hibernate二级缓存ehcache------------------------->hibernate配置 <prop key=&quo ...
- Hibernate配置详解
<!--标准的XML文件的起始行,version='1.0'表明XML的版本,encoding='gb2312'表明XML文件的编码方式--> <?xml version='1.0' ...
随机推荐
- Javascript中的attribute和property分析
attribute和property这两个单词,都有属性的意思,attribute有属性.特质的意思,property则有性质,性能的意思. 首先需要明确的是,在规范中,读取和设置attribute的 ...
- CSS知识点摘记
CSS层叠样式表cascading style sheets 将网页中的样式单独分离出来,完全由CSS控制,增强样式复用性和扩展性. 格式:选择器{属性名:属性值:属性名:属性值:……} CSS与HT ...
- 美国地质调研局USGS
https://lta.cr.usgs.gov/get_data/ http://www.usgs.gov/
- C语言入门(1)——C语言概述
1.程序与编程语言 我们使用计算机离不开程序,程序告诉计算机应该如何运行.程序(Program)是一个精确说明如何进行计算的指令序列.这里的计算可以是数学运算,比如通过一些数学公式求解,也可以是符号运 ...
- poj2390
#include <stdio.h> #include <stdlib.h> int main() { int r,m,y,i; scanf("%d %d %d&qu ...
- [虚拟化/云] kvm的架构分析
预备知识 1. 客户机物理页框到宿主机虚拟地址转换 http://blog.csdn.net/zhuriyuxiao/article/details/8968781 http://www.tuicoo ...
- express手工实现session原理
var express = require('express'); var cookieParser = require('cookie-parser'); var bodyParser = requ ...
- lodash中_.set的用法
_.set(object, path, value) # Ⓢ Ⓣ Ⓝ 设置对象的路径上的属性值.如果路径不存在,则创建它. 参数 1.object (Object): 待扩大的对象. 2.path ( ...
- 新浪微博布局学习——妙用TabHost
前言 为了更好的开发Android应用程序,除了熟练掌握基本的UI组件和API外,还需要掌握一些技巧,而这些技巧可以通过阅读一些代码来提高,本系列将与大家分享一些新浪微博布局方面的收获,欢迎交流! 声 ...
- 利用CSS3的transform 3D制作的立方体旋转效果
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...