📅  最后修改于: 2021-01-02 15:57:26             🧑  作者: Mango
由于Hibernate可以在不同的环境中运行,因此它需要各种各样的配置参数。这些配置包含为Java类提供不同功能的映射信息。通常,我们在配置文件中提供与数据库相关的映射。 Hibernate有助于以XML文件(例如hibernate.cfg.xml)或属性文件(例如hibernate.properties)提供配置。
Configuration类的实例允许指定属性和到应用程序的映射。此类还构建了一个不变的SessionFactory 。
我们可以通过直接实例化Configuration类实例并在配置文件中指定映射来获取它。如果映射文件存在于类路径中,请使用addResource()方法。
Configuration cfg = new Configuration()
.addResource("employee.hbm.xml")
让我们看一个以XML文件和属性文件提供配置的示例。
基于XML的配置
update
org.hibernate.dialect.Oracle9Dialect
jdbc:oracle:thin:@localhost:1521:xe
system
jtp
oracle.jdbc.driver.OracleDriver
属性文件配置
hibernate.dialect= org.hibernate.dialect.Oracle9Dialect
hibernate.connection.driver_class= oracle.jdbc.driver.OracleDriver
hibernate.connection.url= jdbc:oracle:thin:@localhost:1521:xe
hibernate.connection.username= system
hibernate.connection.password=jtp
hibernate.show_sql=true
hibernate.hbm2ddl=update
Property | Description |
---|---|
hibernate.connection.driver_class | It represents the JDBC driver class. |
hibernate.connection.url | It represents the JDBC URL. |
hibernate.connection.username | It represents the database username. |
hibernate.connection.password | It represents the database password. |
Hibernate.connection.pool_size | It represents the maximum number of connections available in the connection pool. |
Property | Description |
---|---|
hibernate.connection.datasource | It represents datasource JNDI name which is used by Hibernate for database properties. |
hibernate.jndi.url | It is optional. It represents the URL of the JNDI provider. |
hibernate.jndi.class | It is optional. It represents the class of the JNDI InitialContextFactory. |
Property | Description |
---|---|
hibernate.dialect | It represents the type of database used in hibernate to generate SQL statements for a particular relational database. |
hibernate.show_sql | It is used to display the executed SQL statements to console. |
hibernate.format_sql | It is used to print the SQL in the log and console. |
hibernate.default_catalog | It qualifies unqualified table names with the given catalog in generated SQL. |
hibernate.default_schema | It qualifies unqualified table names with the given schema in generated SQL. |
hibernate.session_factory_name | The SessionFactory interface automatically bound to this name in JNDI after it has been created. |
hibernate.default_entity_mode | It sets a default mode for entity representation for all sessions opened from this SessionFactory |
hibernate.order_updates | It orders SQL updates on the basis of the updated primary key. |
hibernate.use_identifier_rollback | If enabled, the generated identifier properties will be reset to default values when objects are deleted. |
hibernate.generate_statistics | If enabled, the Hibernate will collect statistics useful for performance tuning. |
hibernate.use_sql_comments | If enabled, the Hibernate generate comments inside the SQL. It is used to make debugging easier. |
Property | Description |
---|---|
hibernate.cache.provider_class | It represents the classname of a custom CacheProvider. |
hibernate.cache.use_minimal_puts | It is used to optimize the second-level cache. It minimizes writes, at the cost of more frequent reads. |
hibernate.cache.use_query_cache | It is used to enable the query cache. |
hibernate.cache.use_second_level_cache | It is used to disable the second-level cache, which is enabled by default for classes which specify a |
hibernate.cache.query_cache_factory | It represents the classname of a custom QueryCache interface. |
hibernate.cache.region_prefix | It specifies the prefix which is used for second-level cache region names. |
hibernate.cache.use_structured_entries | It facilitates Hibernate to store data in the second-level cache in a more human-friendly format. |
Property | Description |
---|---|
hibernate.transaction.factory_class | It represents the classname of a TransactionFactory which is used with Hibernate Transaction API. |
hibernate.transaction.manager_lookup_class | It represents the classname of a TransactionManagerLookup. It is required when JVM-level caching is enabled. |
hibernate.transaction.flush_before_completion | If it is enabled, the session will be automatically flushed during the before completion phase of the transaction. |
hibernate.transaction.auto_close_session | If it is enabled, the session will be automatically closed during the after completion phase of the transaction. |
Property | Description |
---|---|
hibernate.connection.provider_class | It represents the classname of a custom ConnectionProvider which provides JDBC connections to Hibernate. |
hibernate.connection.isolation | It is used to set the JDBC transaction isolation level. |
hibernate.connection.autocommit | It enables auto-commit for JDBC pooled connections. However, it is not recommended. |
hibernate.connection.release_mode | It specifies when Hibernate should release JDBC connections. |
hibernate.current_session_context_class | It provides a custom strategy for the scoping of the “current” Session. |
hibernate.hbm2ddl.auto | It automatically generates a schema in the database with the creation of SessionFactory. |