Hibernate 配置
hibernate 配置
hibernate 需要知道在哪里找到映射信息,这些映射信息定义了 java 类怎样关联到数据库表。
hibernate 也需要一套相关数据库和其它相关参数的配置设置。所有这些信息通常是作为一个标准的 java 属性文件提供的,文件名称为 hibernate.properties,或者使用xml 文件 hibernate.cfg.xml。
我们将考虑 hibernate.cfg.xml 这个 xml 格式文件,来决定在我的例子里指定需要的 hibernate 应用属性。这个 xml 文件中大多数的属性是不需要修改的。这个文件保存在应用程序的类路径的根目录里。
hibernate 属性
下面是一个重要的属性列表,你可能需要表中的属性来在单独的情况下配置数据库。
| s.n. | 属性和描述 |
|---|---|
| 1 | hibernate.dialect 这个属性使 hibernate 应用为被选择的数据库生成适当的 sql。 |
| 2 | hibernate.connection.driver_class jdbc 驱动程序类。 |
| 3 | hibernate.connection.url 数据库实例的 jdbc url。 |
| 4 | hibernate.connection.username 数据库用户名。 |
| 5 | hibernate.connection.password 数据库密码。 |
| 6 | hibernate.connection.pool_size 限制在 hibernate 应用数据库连接池中连接的数量。 |
| 7 | hibernate.connection.autocommit 允许在 jdbc 连接中使用自动提交模式。 |
如果您正在使用 jndi 和数据库应用程序服务器然后您必须配置以下属性:
| s.n. | 属性和描述 |
|---|---|
| 1 | hibernate.connection.datasource 在应用程序服务器环境中您正在使用的应用程序 jndi 名。 |
| 2 | hibernate.jndi.class jndi 的 initialcontext 类。 |
| 3 | hibernate.jndi.<jndipropertyname> 在 jndi的 initialcontext 类中通过任何你想要的 java 命名和目录接口属性。 |
| 4 | hibernate.jndi.url 为 jndi 提供 url。 |
| 5 | hibernate.connection.username 数据库用户名。 |
| 6 | hibernate.connection.password 数据库密码。 |
hibernate 和 mysql 数据库
mysql 数据库是目前可用的开源数据库系统中最受欢迎的数据库之一。我们要创建 hibernate.cfg.xml 配置文件并将其放置在应用程序的 classpath 的根目录里。你要确保在你的 mysql 数据库中 testdb 数据库是可用的,而且你要有一个用户 test 可用来访问数据库。
xml 配置文件一定要遵守 hibernate 3 configuration dtd,在 http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd. 这个网址中是可以找到的。
<?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>
<property name="hibernate.dialect">
org.hibernate.dialect.mysqldialect
</property>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.driver
</property>
<!-- assume test is the database name -->
<property name="hibernate.connection.url">
jdbc:mysql://localhost/test
</property>
<property name="hibernate.connection.username">
root
</property>
<property name="hibernate.connection.password">
root123
</property>
<!-- list of xml mapping files -->
<mapping resource="employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>
上面的配置文件包含与 hibernate-mapping 文件相关的 <mapping> 标签,我们将在下章看看 hibernate mapping 文件到底是什么并且要知道为什么用它,怎样用它。以下是各种重要数据库同源语属性类型的列表:
| 数据库 | 方言属性 |
|---|---|
| db2 | org.hibernate.dialect.db2dialect |
| hsqldb | org.hibernate.dialect.hsqldialect |
| hypersonicsql | org.hibernate.dialect.hsqldialect |
| informix | org.hibernate.dialect.informixdialect |
| ingres | org.hibernate.dialect.ingresdialect |
| interbase | org.hibernate.dialect.interbasedialect |
| microsoft sql server 2000 | org.hibernate.dialect.sqlserverdialect |
| microsoft sql server 2005 | org.hibernate.dialect.sqlserver2005dialect |
| microsoft sql server 2008 | org.hibernate.dialect.sqlserver2008dialect |
| mysql | org.hibernate.dialect.mysqldialect |
| oracle (any version) | org.hibernate.dialect.oracledialect |
| oracle 11g | org.hibernate.dialect.oracle10gdialect |
| oracle 10g | org.hibernate.dialect.oracle10gdialect |
| oracle 9i | org.hibernate.dialect.oracle9idialect |
| postgresql | org.hibernate.dialect.postgresqldialect |
| progress | org.hibernate.dialect.progressdialect |
| sap db | org.hibernate.dialect.sapdbdialect |
| sybase | org.hibernate.dialect.sybasedialect |
| sybase anywhere | org.hibernate.dialect.sybaseanywheredialec |


