3.4. 可选的配置属性
有大量属性能用来控制 Hibernate 在运行期的行为。它们都是可选的,并拥有适当的默认值。
警告
其中一些属性是"系统级(system-level)的"。系统级属性只能通过java -Dproperty=value
或 hibernate.properties
来设置,而不能用上面描述的其他方法来设置。
表 3.3. Hibernate 配置属性
属性名 | 用途 |
---|---|
hibernate.dialect | 允许 Hibernate 针对特定的关系数据库生成优化的 SQL 的 org.hibernate.dialect.Dialect 的类名。例如: 在大多数情况下,Hibernate 可以根据 JDBC 驱动返回的 |
hibernate.show_sql | 输出所有 SQL 语句到控制台。有一个另外的选择是把 org.hibernate.SQL 这个 log category设为 debug 。例如: |
hibernate.format_sql | 在 log 和 console 中打印出更漂亮的 SQL。 例如: |
hibernate.default_schema | 在生成的 SQL 中,将给定的 schema/tablespace 附加于非全限定名的表名上。 例如: |
hibernate.default_catalog | 在生成的 SQL 中,将给定的 catalog 附加于非全限定名的表名上。 例如: |
hibernate.session_factory_name | org.hibernate.SessionFactory 创建后,将自动使用这个名字绑定到 JNDI 中。例如: |
hibernate.max_fetch_depth | 为单向关联(一对一,多对一)的外连接抓取(outer join fetch)树设置最大深度。值为 0 意味着将关闭默认的外连接抓取。例如: 建议在 |
hibernate.default_batch_fetch_size | 为 Hibernate 关联的批量抓取设置默认数量。 例如:建议的取值为 |
hibernate.default_entity_mode | 为由这个 SessionFactory 打开的所有 Session 指定默认的实体表现模式。取值 |
hibernate.order_updates | 强制 Hibernate 按照被更新数据的主键,为 SQL 更新排序。这么做将减少在高并发系统中事务的死锁。 例如: |
hibernate.generate_statistics | 如果开启,Hibernate 将收集有助于性能调节的统计数据。 例如: |
hibernate.use_identifier_rollback | 如果开启,在对象被删除时生成的标识属性将被重设为默认值。 例如: |
hibernate.use_sql_comments | 如果开启,Hibernate 将在 SQL 中生成有助于调试的注释信息,默认值为 false 。例如: |
表 3.4. Hibernate JDBC 和连接(connection)属性
属性名 | 用途 |
---|---|
hibernate.jdbc.fetch_size | 非零值,指定 JDBC 抓取数量的大小(调用 Statement.setFetchSize() )。 |
hibernate.jdbc.batch_size | 非零值,允许 Hibernate 使用 JDBC2 的批量更新。 例如:建议取 |
hibernate.jdbc.batch_versioned_data | Set this property to true if your JDBC driver returns correct row counts from executeBatch() . It is usually safe to turn this option on. Hibernate will then use batched DML for automatically versioned data. Defaults to false .例如: |
hibernate.jdbc.factory_class | 选择一个自定义的 Batcher 。多数应用程序不需要这个配置属性。例如: |
hibernate.jdbc.use_scrollable_resultset | 允许 Hibernate 使用 JDBC2 的可滚动结果集。只有在使用用户提供的 JDBC 连接时,这个选项才是必要的,否则 Hibernate 会使用连接的元数据。 例如: |
hibernate.jdbc.use_streams_for_binary | 在 JDBC 读写 binary 或 serializable 的类型时使用流(stream)(系统级属性)。例如: |
hibernate.jdbc.use_get_generated_keys | 在数据插入数据库之后,允许使用 JDBC3 PreparedStatement.getGeneratedKeys() 来获取数据库生成的 key(键)。需要 JDBC3+ 驱动和 JRE1.4+,如果你的数据库驱动在使用 Hibernate 的标识生成器时遇到问题,请将此值设为 false。默认情况下将使用连接的元数据来判定驱动的能力。例如: |
hibernate.connection.provider_class | 自定义 ConnectionProvider 的类名,此类用来向 Hibernate 提供 JDBC 连接。例如: |
hibernate.connection.isolation | 设置 JDBC 事务隔离级别。查看 java.sql.Connection 来了解各个值的具体意义,但请注意多数数据库都不支持所有的隔离级别。例如: |
hibernate.connection.autocommit | 允许被缓存的 JDBC 连接开启自动提交(autocommit)(不推荐)。 例如: |
hibernate.connection.release_mode | 指定 Hibernate 在何时释放 JDBC 连接。默认情况下,直到 Session 被显式关闭或被断开连接时,才会释放 JDBC 连接。对于应用程序服务器的 JTA 数据源,你应当使用 after_statement ,这样在每次 JDBC 调用后,都会主动的释放连接。对于非 JTA 的连接,使用 after_transaction 在每个事务结束时释放连接是合理的。auto 将为 JTA 和 CMT 事务策略选择 after_statement ,为JDBC事务策略选择 after_transaction 。例如: This setting only affects |
hibernate.connection.<propertyName> | 把 JDBC 属性 <propertyName> 传递给 DriverManager.getConnection() 。 |
hibernate.jndi.<propertyName> | 把 <propertyName> 属性传递给 JNDI InitialContextFactory 。 |
表 3.5. Hibernate 缓存属性
属性名 | 用途 |
---|---|
hibernate.cache.provider_class | 自定义的 CacheProvider 的类名。例如: |
hibernate.cache.use_minimal_puts | 以频繁的读操作为代价,优化二级缓存来最小化写操作。在 Hibernate3 中,这个设置对的集群缓存非常有用,对集群缓存的实现而言,默认是开启的。 例如: |
hibernate.cache.use_query_cache | 允许查询缓存,个别查询仍然需要被设置为可缓存的。 例如: |
hibernate.cache.use_second_level_cache | 能用来完全禁止使用二级缓存。对那些在类的映射定义中指定 <cache> 的类,会默认开启二级缓存。例如: |
hibernate.cache.query_cache_factory | 自定义实现 QueryCache 接口的类名,默认为内建的 StandardQueryCache 。例如: |
hibernate.cache.region_prefix | 二级缓存区域名的前缀。 例如: |
hibernate.cache.use_structured_entries | 强制 Hibernate 以更人性化的格式将数据存入二级缓存。 例如: |
表 3.6. Hibernate 事务属性
属性名 | 用途 |
---|---|
hibernate.transaction.factory_class | 一个 TransactionFactory 的类名,用于 Hibernate Transaction API(默认为 JDBCTransactionFactory )。例如: |
jta.UserTransaction | 一个 JNDI 名字,被 JTATransactionFactory 用来从应用服务器获取 JTA UserTransaction 。例如: |
hibernate.transaction.manager_lookup_class | 一个 TransactionManagerLookup 的类名 — 当使用 JVM 级缓存,或在 JTA 环境中使用 hilo 生成器的时候需要该类。例如: |
hibernate.transaction.flush_before_completion | If enabled, the session will be automatically flushed during the before completion phase of the transaction. Built-in and automatic session context management is preferred, see 第 2.5 节 “上下文相关的会话(Contextual Session)”. 例如: |
hibernate.transaction.auto_close_session | If enabled, the session will be automatically closed during the after completion phase of the transaction. Built-in and automatic session context management is preferred, see 第 2.5 节 “上下文相关的会话(Contextual Session)”. 例如: |
表 3.7. 其他属性
属性名 | 用途 |
---|---|
hibernate.current_session_context_class | Supply a custom strategy for the scoping of the "current" Session . See 第 2.5 节 “上下文相关的会话(Contextual Session)” for more information about the built-in strategies.例如: |
hibernate.query.factory_class | 选择 HQL 解析器的实现。 例如: |
hibernate.query.substitutions | 将 Hibernate 查询中的符号映射到 SQL 查询中的符号(符号可能是函数名或常量名字)。 例如: |
hibernate.hbm2ddl.auto | 在 SessionFactory 创建时,自动检查数据库结构,或者将数据库 schema 的 DDL 导出到数据库。使用 create-drop 时,在显式关闭 SessionFactory 时,将删除掉数据库 schema。例如: |
hibernate.bytecode.use_reflection_optimizer | Enables the use of bytecode manipulation instead of runtime reflection. This is a System-level property and cannot be set in 例如: |
hibernate.bytecode.provider | Both javassist or cglib can be used as byte manipulation engines; the default is e.g. |
3.4.1. SQL 方言
你应当总是为你的数据库将 hibernate.dialect
属性设置成正确的 org.hibernate.dialect.Dialect
子类。如果你指定一种方言,Hibernate 将为上面列出的一些属性使用合理的默认值,这样你就不用手工指定它们。
表 3.8. Hibernate SQL 方言(hibernate.dialect
)
RDBMS | Dialect |
---|---|
DB2 | org.hibernate.dialect.DB2Dialect |
DB2 AS/400 | org.hibernate.dialect.DB2400Dialect |
DB2 OS390 | org.hibernate.dialect.DB2390Dialect |
PostgreSQL | org.hibernate.dialect.PostgreSQLDialect |
MySQL | org.hibernate.dialect.MySQLDialect |
MySQL with InnoDB | org.hibernate.dialect.MySQLInnoDBDialect |
MySQL with MyISAM | org.hibernate.dialect.MySQLMyISAMDialect |
Oracle(any version) | org.hibernate.dialect.OracleDialect |
Oracle 9i | org.hibernate.dialect.Oracle9iDialect |
Oracle 10g | org.hibernate.dialect.Oracle10gDialect |
Sybase | org.hibernate.dialect.SybaseDialect |
Sybase Anywhere | org.hibernate.dialect.SybaseAnywhereDialect |
Microsoft SQL Server | org.hibernate.dialect.SQLServerDialect |
SAP DB | org.hibernate.dialect.SAPDBDialect |
Informix | org.hibernate.dialect.InformixDialect |
HypersonicSQL | org.hibernate.dialect.HSQLDialect |
Ingres | org.hibernate.dialect.IngresDialect |
Progress | org.hibernate.dialect.ProgressDialect |
Mckoi SQL | org.hibernate.dialect.MckoiDialect |
Interbase | org.hibernate.dialect.InterbaseDialect |
Pointbase | org.hibernate.dialect.PointbaseDialect |
FrontBase | org.hibernate.dialect.FrontbaseDialect |
Firebird | org.hibernate.dialect.FirebirdDialect |
3.4.2. 外连接抓取(Outer Join Fetching)
如果你的数据库支持 ANSI、Oracle 或 Sybase 风格的外连接,外连接抓取通常能通过限制往返数据库次数(更多的工作交由数据库自己来完成)来提高效率。外连接抓取允许在单个 SELECT
SQL 语句中, 通过 many-to-one、one-to-many、many-to-many 和 one-to-one 关联获取连接对象的整个对象图。
将 hibernate.max_fetch_depth
设为 0
能在全局 范围内禁止外连接抓取。设为 1
或更高值能启用 one-to-one 和 many-to-oneouter 关联的外连接抓取,它们通过 fetch="join"
来映射。
See 第 20.1 节 “抓取策略(Fetching strategies)” for more information.
3.4.3. 二进制流(Binary Streams)
Oracle 限制那些通过 JDBC 驱动传输的字节
数组的数目。如果你希望使用二进值(binary)
或 可序列化的(serializable)
类型的大对象,你应该开启 hibernate.jdbc.use_streams_for_binary
属性。这是系统级属性。
3.4.4. 二级缓存与查询缓存
The properties prefixed by hibernate.cache
allow you to use a process or cluster scoped second-level cache system with Hibernate. See the 第 20.2 节 “二级缓存(The Second Level Cache)” for more information.
3.4.5. 查询语言中的替换
你可以使用 hibernate.query.substitutions
在 Hibernate 中定义新的查询符号。例如:
hibernate.query.substitutions true=1, false=0
将导致符号 true
和 false
在生成的 SQL 中被翻译成整数常量。
hibernate.query.substitutions toLowercase=LOWER
将允许你重命名 SQL 中的 LOWER
函数。
3.4.6. Hibernate 的统计(statistics)机制
如果你开启 hibernate.generate_statistics
,那么当你通过 SessionFactory.getStatistics()
调整正在运行的系统时,Hibernate 将导出大量有用的数据。Hibernate 甚至能被配置成通过 JMX 导出这些统计信息。参考 org.hibernate.stats
中接口的 Javadoc,以获得更多信息。