Hibernate-配置详解(一)

Hibernate JDBC 属性

参数名必选
属性名用途
hibernate.connection.driver_classJDBC driver class
hibernate.connection.urlJDBC URL
hibernate.connection.usernamedatabase user
hibernate.connection.password数据库用户密码
hibernate.connection.pool_sizemaximum number of pooled connections


这是一个使用 C3P0 的 hibernate.properties 样例文件:

hibernate.connection.driver_class = org.postgresql.Driver
hibernate.connection.url = jdbc:postgresql://localhost/mydatabase
hibernate.connection.username = myuser
hibernate.connection.password = secret
hibernate.c3p0.min_size=5
hibernate.c3p0.max_size=20
hibernate.c3p0.timeout=1800
hibernate.c3p0.max_statements=50
hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect

Hibernate 数据源属性

参数名必选
属性名用途
hibernate.connection.datasource数据源 JNDI 名字
hibernate.jndi.urlJNDI 提供者的 URL(可选)
hibernate.jndi.classJNDI InitialContextFactory 类(可选)
hibernate.connection.username数据库用户(可选)
hibernate.connection.password数据库密码(可选)


这是一个使用应用程序服务器提供的 JNDI 数据源的 hibernate.properties 样例文件:

hibernate.connection.datasource = java:/comp/env/jdbc/test
hibernate.transaction.factory_class = \
    org.hibernate.transaction.JTATransactionFactory
hibernate.transaction.manager_lookup_class = \
    org.hibernate.transaction.JBossTransactionManagerLookup
hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect

Hibernate 配置属性

参数名必选
属性名用途
hibernate.dialect允许 Hibernate 针对特定的关系数据库生成优化的 SQL 的 org.hibernate.dialect.Dialect 的类名。

例如:full.classname.of.Dialect

在大多数情况下,Hibernate 可以根据 JDBC 驱动返回的 JDBC metadata 选择正确的 org.hibernate.dialect.Dialect 实现。
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_nameorg.hibernate.SessionFactory 创建后,将自动使用这个名字绑定到 JNDI 中。

例如:jndi/composite/name
hibernate.max_fetch_depth为单向关联(一对一,多对一)的外连接抓取(outer join fetch)树设置最大深度。值为 0 意味着将关闭默认的外连接抓取。

例如: 建议在 0 到 3 之间取值
hibernate.default_batch_fetch_size为 Hibernate 关联的批量抓取设置默认数量。

例如:建议的取值为 4,8,和 16
hibernate.default_entity_mode为由这个 SessionFactory 打开的所有 Session 指定默认的实体表现模式。取值dynamic-map,dom4j,pojo
hibernate.order_updates强制 Hibernate 按照被更新数据的主键,为 SQL 更新排序。这么做将减少在高并发系统中事务的死锁。
hibernate.generate_statistics如果开启,Hibernate 将收集有助于性能调节的统计数据。
hibernate.use_identifier_rollback如果开启,在对象被删除时生成的标识属性将被重设为默认值。
hibernate.use_sql_comments如果开启,Hibernate 将在 SQL 中生成有助于调试的注释信息,默认值为 false。

Hibernate JDBC 和连接(connection)属性

参数名必选
属性名用途
hibernate.jdbc.fetch_size非零值,指定 JDBC 抓取数量的大小(调用 Statement.setFetchSize())。
hibernate.jdbc.batch_size非零值,允许 Hibernate 使用 JDBC2 的批量更新。 例如:建议取 5 到 30 之间的值
hibernate.jdbc.batch_versioned_dataSet 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 连接

例如:classname.of.ConnectionProvider
hibernate.connection.isolation设置 JDBC 事务隔离级别。查看 java.sql.Connection 来了解各个值的具体意义,但请注意多数数据库都不支持所有的隔离级别。

例如:1, 2, 4, 8
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。

例如:auto (默认)、on_close 、after_transaction、after_statement
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值