In Hibernate, there are mainly two configuration files when we use:
- Core configuration file - hibernate.cfg.xml (mainly describes Hibernate related configuration)
- Mapping configuration file - xxx.hbm.xml
core configuration file
Hibernate's core configuration file, hibernate.cfg.xml, is mainly used to describe Hibernate-related configuration. For Hibernate's core configuration file it has two ways:
- hibernate.cfg.xml
- hibernate.properties
We use hibernate.cfg.xml more in development because it has stronger configuration capabilities and is easy to modify.
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Set database driver-->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- Set database URL -->
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/activitidata</property>
<!-- database username-->
<property name="hibernate.connection.username">root</property>
<!-- database password-->
<property name="hibernate.connection.password">root</property>
<!-- Specify the dialect of the corresponding database. In order to better adapt to various relational databases, hibernate specifies a dialect for each database -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Print sql statement for easy debugging -->
<property name="show_sql">true</property>
<!-- Format sql statement, typesetting-->
<property name="foramt_sql">true</property>
<!-- Specify the way hibernate generates database tables: create creates a new table each time, update uses the original table-->
<property name="hbm2ddl.auto">create</property>
<!-- You need to open this configuration when using session.getCurrentSession-->
<property name="hibernate.current_session_context_class">true</property>
<!-- List xml mapping files -->
<mapping resource="hibernate_demo/pojo/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
The content of the above configuration file can be divided into three parts:
Load database related information
<!-- Set database driver-->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- Set database URL -->
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/activitidata</property>
<!-- database username-->
<property name="hibernate.connection.username">root</property>
<!-- database password-->
<property name="hibernate.connection.password">root</property>
<!-- Specify the dialect of the corresponding database. In order to better adapt to various relational databases, hibernate specifies a dialect for each database -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
2. Hibernate related configuration
<!-- Print sql statement for easy debugging -->
<property name="show_sql">true</property>
<!-- Format sql statement, typesetting-->
<property name="foramt_sql">true</property>
<!-- Specify the way hibernate generates database tables: create creates a new table each time, update uses the original table-->
<property name="hbm2ddl.auto">create</property>
<!-- 使用session.getCurrentSession时需要打开该配置 -->
<property name="hibernate.current_session_context_class">true</property>
3.加载映射配置文件
<!-- 列出xml的映射文件 -->
<mapping resource="hibernate_demo/pojo/User.hbm.xml"/>
重点:
hbm2ddl.auto
- create-drop 每次都会创建一个新的表,执行完成后就删除。一般在测试中使用。
create 每次都会创建一个新的表,但不删除。一般也是在测试中使用。
update
如果数据库中有表,不创建,没有表则创建;如果映射不匹配,会自动更新表结构。validate
只会使用存在的表,并且会对映射关系进行校验。
映射配置文件
映射配置文件的名称是类名.hbm.xml
,它一般放置在实体类所在的包下。这个配置文件的主要作用是建立表与类之间的映射关系。
<?xml version="1.0" encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="hibernate_demo.pojo">
<class name="User">
<id name="id" type="int" column="ID_">
<generator class="native" />
</id>
<property name="date" type="timestamp" column="EVENT_DATE" />
<property name="title" type="java.lang.String" column="TITLE" />
</class>
</hibernate-mapping>
你需要以格式 <classname>.hbm.xml
保存映射文件。我们保存映射文件在 Employee.hbm.xml 中。让我们来详细地看一下在映射文件中使用的一些标签:
- 映射文件是一个以
<hibernate-mapping>
为根元素的 XML 文件,里面包含所有<class>
标签。 <class>
标签是用来定义从一个 Java 类到数据库表的特定映射。Java 的类名使用 name 属性来表示,数据库表明用 table 属性来表示。<meta>
标签是一个可选元素,可以被用来修饰类。<id>
标签将类中独一无二的 ID 属性与数据库表中的主键关联起来。id 元素中的 name 属性引用类的性质,column 属性引用数据库表的列。type 属性保存 Hibernate 映射的类型,这个类型会将从 Java 转换成 SQL 数据类型。- 在 id 元素中的
<generator>
标签用来自动生成主键值。设置 generator 标签中的 class 属性可以设置 native 使 Hibernate 可以使用 identity, sequence 或 hilo 算法根据底层数据库的情况来创建主键。 <property>
标签用来将 Java 类的属性与数据库表的列匹配。标签中 name 属性引用的是类的性质,column 属性引用的是数据库表的列。type 属性保存 Hibernate 映射的类型,这个类型会将从 Java 转换成 SQL 数据类型。
1.统一声明包名,这样在<class>
中就不需要写类的全名。
<hibernate-mapping package="cn.itheima.domain">
<class name="Customer" table="t_customer" catalog="hibernateTest">
....
</class>
</hibernate-mapping>
2.关于<class>
标签配置的详细介绍:
- name属性:类的全名称。
- table属性:映射到数据库里面的那个表的名称,可以省略,这时表的名称就与类名一致。
- catalog属性:数据库名称,可以省略,如果省略,则参考核心配置文件中url路径中的库名称。
3.关于<id>
标签配置的详细介绍:
<hibernate-mapping package="cn.itheima.domain">
<class name="Customer" table="t_customer" catalog="hibernateTest">
<!-- class下必须要有一个id的子元素 -->
<!-- id是用于描述主键的 -->
<id name="id" column="id" type="int"> <!-- java数据类型 -->
<!-- 主键生成策略 -->
<generator class="native"></generator>
</id>
......
</class>
</hibernate-mapping>
首先该标签必须存在。<id>
是用于建立类中的属性与表中的主键映射。
- name:类中的属性名称
- column:表中的主键名称,column也可以省略,这时列名就与类中属性名称一致
- length:字段长度,如果length忽略不写,且你的表是自动创建这种方案,那么length的默认长度是255
- type属性:指定类型
<generator>
它主要是描述主键生成策略,这里就不做篇幅来介绍了,请看后面的文章。
4.关于<property>
标签
它是描述类中属性与表中非主键字段的映射关系。
映射类型
当你准备一个 Hibernate 映射文件时,我们已经看到你把 Java 数据类型映射到了 RDBMS 数据格式。在映射文件中已经声明被使用的 types 不是 Java 数据类型;它们也不是 SQL 数据库类型。这种类型被称为 Hibernate 映射类型,可以从 Java 翻译成 SQL,反之亦然。