Hibernate数据库对象的创建与导出
2014-03-30 10:43
225 查看
Hibernate 与数据库的关系是ORM关系,对象映射数据库。
那么如何通过对象对数据库进行各种对象的ddl与dml操作呢?
数据库对象操作的〈database-object /〉+ SchemaExport
1、hibernate.cfg.xml
<?xml version="1.0" encoding="GBK"?>
<!-- 指定Hibernate配置文件的DTD信息 -->
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<!-- hibernate- configuration是连接配置文件的根元素 -->
<hibernate-configuration>
<session-factory>
……
<!-- 根据需要自动创建数据库:如果创建表,这里必须为create -->
<property name="hbm2ddl.auto">create</property>
<!-- 显示Hibernate持久化操作所生成的SQL -->
<property name="show_sql">true</property>
<!-- 将SQL脚本进行格式化后再输出 -->
<property name="hibernate.format_sql">true</property>
<!-- 罗列所有的映射文件 -->
<mapping resource="……/lovejk.hbm.xml"/>
</session-factory>
lt;/hibernate-configuration>
2、lovejk.hbm.xml
<?xml version="1.0" encoding="gb2312"?>
<!-- 指定Hiberante3映射文件的DTD信息 -->
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- hibernate-mapping是映射文件的根元素 -->
<hibernate-mapping>
<!-- 使用data-object元素定义数据库对象 -->
<database-object>
<!-- 定义创建数据库对象的语句 -->
<create>create table testjk(name varchar(256));</create>
<!-- 让drop元素为空,不删除任何对象 -->
<drop></drop>
<!-- 指定仅对MySQL数据库有效 -->
<dialect-scope name="org.hibernate.dialect.MySQLDialect"/>
<dialect-scope name="org.hibernate.dialect.MySQLInnoDBDialect"/>
</database-object>
</hibernate-mapping>
3、执行 www.jx-jf.com
public static void main(String[] args) throws Exception
{
//实例化Configuration,这行代码默认加载hibernate.cfg.xml文件
Configuration conf = new Configuration()。configure();
//以Configuration创建SessionFactory
SessionFactory sf = conf.buildSessionFactory();
// //创建SchemaExport对象 www.yzyedu.com
SchemaExport se = new SchemaExport(conf);
// //设置输出格式良好的SQL脚本
se.setFormat(true);
// //设置保存SQL脚本的文件名
se.setOutputFile("d:\\1.sql");
// //输出SQL脚本,并执行SQL脚本
se.create(true, true);
sf.close();
}
那么如何通过对象对数据库进行各种对象的ddl与dml操作呢?
数据库对象操作的〈database-object /〉+ SchemaExport
1、hibernate.cfg.xml
<?xml version="1.0" encoding="GBK"?>
<!-- 指定Hibernate配置文件的DTD信息 -->
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<!-- hibernate- configuration是连接配置文件的根元素 -->
<hibernate-configuration>
<session-factory>
……
<!-- 根据需要自动创建数据库:如果创建表,这里必须为create -->
<property name="hbm2ddl.auto">create</property>
<!-- 显示Hibernate持久化操作所生成的SQL -->
<property name="show_sql">true</property>
<!-- 将SQL脚本进行格式化后再输出 -->
<property name="hibernate.format_sql">true</property>
<!-- 罗列所有的映射文件 -->
<mapping resource="……/lovejk.hbm.xml"/>
</session-factory>
lt;/hibernate-configuration>
2、lovejk.hbm.xml
<?xml version="1.0" encoding="gb2312"?>
<!-- 指定Hiberante3映射文件的DTD信息 -->
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- hibernate-mapping是映射文件的根元素 -->
<hibernate-mapping>
<!-- 使用data-object元素定义数据库对象 -->
<database-object>
<!-- 定义创建数据库对象的语句 -->
<create>create table testjk(name varchar(256));</create>
<!-- 让drop元素为空,不删除任何对象 -->
<drop></drop>
<!-- 指定仅对MySQL数据库有效 -->
<dialect-scope name="org.hibernate.dialect.MySQLDialect"/>
<dialect-scope name="org.hibernate.dialect.MySQLInnoDBDialect"/>
</database-object>
</hibernate-mapping>
3、执行 www.jx-jf.com
public static void main(String[] args) throws Exception
{
//实例化Configuration,这行代码默认加载hibernate.cfg.xml文件
Configuration conf = new Configuration()。configure();
//以Configuration创建SessionFactory
SessionFactory sf = conf.buildSessionFactory();
// //创建SchemaExport对象 www.yzyedu.com
SchemaExport se = new SchemaExport(conf);
// //设置输出格式良好的SQL脚本
se.setFormat(true);
// //设置保存SQL脚本的文件名
se.setOutputFile("d:\\1.sql");
// //输出SQL脚本,并执行SQL脚本
se.create(true, true);
sf.close();
}
相关文章推荐
- Hibernate数据库对象的创建与导出
- 对于SQL Server 对象名 XXX 无效 的多种有效处理方式(hibernate自动创建、数据库移植)
- hibernate创建导出数据库DDL schema
- Hibernate创建对象-关系映射文件和数据库
- ASP EF框架,数据库操作类(上下文类)的实例创建,线程内唯一对象(HttpContext)
- Greenplum+Hadoop学习笔记-14-定义数据库对象之创建与管理模式
- 在VB程序中用ADO对象动态创建数据库和表
- 利用Hibernate储存大对象到达梦数据库
- 添加JBoss插件中的hibernate tools后怎么创建hibernate配置文件和对数据库的表进行反转映射
- Hibernate之 对象/关系数据库映射基础(Basic O/R Mapping)
- sql developer Oracle 数据库 用户对象下表及表结构的导入导出
- 将具有关联关系的两个表通过hibernate从数据库中查出来后转成Json对象时报错解决:采用过滤器
- 关于SQL05中基于对象所在的数据库或架构等创建新的同义词的举例
- oracle创建表空间 用户 数据库导入和导出
- Hibernate的方法获取对象后,对象调用set后会自动更新数据库内容的解决办法
- 实验二 数据库及数据库对象的创建和管理
- Hibernate保存对象时,数据库出现乱码
- Oracle创建用户、授权、导入导出数据库
- JavaWeb学习笔记-Hibernate-05-实体对象与数据库关键词冲突解决方法