Hibernate (3) structure-configuration file-object ing and configuration file,

Source: Internet
Author: User

Hibernate (3) structure-configuration file-object ing and configuration file,
I. Architecture

  • SessionFactory: A thread-safe and immutable cache snapshot of the compiled ing file of a single database. Session factory. It is possible to hold an optional data cache which can be saved at the process or group level and can be reused in transactions.
  • Session, Session: a single thread, short-lived object, representing a conversation between the application and the persistence layer. It encapsulates a JDDBC connection, which is also the Transaction factory and stores a cache of objects that must be persisted. It is used to traverse objects or to search for objects by identifiers.
  • Persistent Object and its Collection: short-lived single-thread Object, including persistence status and commercial functions. The flexible JavaBeans/POJOs. (Plain Old Java Objects, simple and clean Java Objects) is sometimes called Plain Ordinary Java Objects, indicating a data set. The only difference is that they now belong to a Session. Once the Session is closed, they will all be disassociated from the Session. They can be freely used at any program layer, for example: directly act as the DTO (data transmission object) and data transmission object that are transmitted to the presentation layer.
  • Temporary Object (Transient Object) and its Collection: Currently, there are no instances from a Session persistence class. It may be a temporary object that has just been instantiated by the program and has not been persisted yet, or is instantiated by a closed Session.
  • Transaction, Transaction: (optional) A single-thread short-lived object, which is used by the application to represent atomic operations of a batch of jobs. It is the abstraction of underlying JDBC, JTA, or CORBA transactions. JTA is the Java Transaction API. JTA allows applications to execute Distributed Transaction Processing-to access and update data on two or more network computer resources. The JTA support of the JDBC driver greatly enhances data access. common Object Request Broker Architecture, Common Object Request proxy Architecture ). A Seesion may span multiple Transaction transactions in some cases.
  • ConnectionProvider: (optional) Factory and pool for JDBC connection. It is abstracted from the underlying DateSource or DriverManager. The corresponding program is invisible, but can be extended and implemented by development.
  • TransactionFactory: (optional) instance factory of the transaction. Invisible to applications.It can be expanded and implemented by developers.
Ii. configuration file -- -XML format

The default configuration file is hibernate. cfg. xml.

During Hibernater initialization, the file is automatically searched in CLASSPATH and the configuration information is read to prepare for subsequent database operations.

The configuration file should be deployed in CLASSPATH,For Web applications, the configuration file should be placed under \ WEB-INF \ classes, directory

Configuration File Content:

<? Xml version = '1. 0' encoding = 'utf-8'?> <! DOCTYPE hibernate-configuration PUBLIC "-// Hibernate/Hibernate Configuration DTD 3.0 // EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <! -- Generated by MyEclipse Hibernate Tools. --> Iii. Object ing

In Hibernate, object ing maps object classes to tables in the database to implement one-to-one correspondence between attributes in the object class and fields in the database table.

Ing is created according to the definition of persistence class (entity class), rather than the definition of the table.

You can manually write the configuration file or use a tool to generate it. For example, when MyEclipse generates an object class, it can automatically generate a configuration file.

Configuration File Name: Class Name. hbm. xml

<? Xml version = '1. 0' encoding = 'utf-8'?> <! -- Here is different from the hibernate. cfg. xml configuration file. --> <! DOCTYPE hibernate-mapping PUBLIC "-// Hibernate/Hibernate Configuration DTD 3.0 // EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <! -- Here is different from the hibernate. cfg. xml configuration file. --> 

4. Optional Hibernate configurations 4.1 and Hibernate configuration items

(1) After hibernate. dialect specifies a dialect, Hibernate can automatically generate optimized SQL statements based on the lower-layer database. The value is the inheritance class of org. hibenate. dialect. Dialect. In most cases, Hibernate

It can be determined based on the metadata returned by the lower-layer JDBC.

(2) hibernate. show_ SQL prints all SQL statements to the console. You can set the log policies of the org. hibernate. SQL class to DEBUG level to achieve the same effect. Valid value: true | false.

(3) When hibernate. format_ SQL formatting SQL statements are printed on the console or written into log files. Valid value: true | false.

(4) When hibernate. default_schema generates an SQL statement, it adds the tablespace or database schema given by the ing file before the table name ). The value is SCHEMA_NAME.

(5) When hibernate. default_catalog generates an SQL statement, add the catalog file before the table name. The value is CATALOG_NAME.

(6) After hibernate. session_factory_name Hibernate creates the org. hibernate. SessionFactory instance, it will automatically bind the instance to the name in the JNDI. Set the value to jndi/sf.

(7) hibernate. max_fetch_depth sets the maximum depth of external connections to a single table. 0 is used to block the default external connection settings. We recommend that you set it to 0 to 3.

(8) hibernate. default_batch_fetch_size sets the scale of the Hibernate batch joint query. It is strongly recommended. Recommended values: 4, 8, and 16.

(9) hibernate. default_entity_mode default object expression mode. All sessions opened through SessionFactory. Value: dynamic-map, dom4j, and pojo.

(10) hibernate. order_updates forces Hibernate to sort SQL updates by the primary key value of the updated item. In this way, the transaction deadlock can be reduced in high concurrency. Valid value: true | false.

(11) If hibernate. generate_statistics is set to true, Hibernate will adjust the performance and collect statistics. Valid value: true | false.

(12) If hibernate. use_identifier_rollback is set to true, the generated identifier property will be reset to the default value. After the object is deleted. Valid value: true | false.

(13) If hibernate. use_ SQL _comments is set to true, Hiberante generates comments for SQL, which is more conducive to debugging. The default value is false. Valid value: true | false.

4.2. Hibernate JDBC and connection attributes

(1) hibernate. jdbc. fetch_size specifies the JDBC query scale. Call (Statement. setFetchSize ()).
(2) hibernate. jdbc. batch_size specifies how Hibernate uses JDBC2 for batch update. Value Range: 5 to 30.
(3) If hibernate. jdbc. batch_versioned_data sets this attribute to true, JDBC returns the correct number of rows after executeBatch is executed. This parameter is usually safe. Hibernate will automatically
The data is decoded using batch DML. The default value is false. Valid value: true | false.
(4) choose a custom org. hibernate. jdbc. Batcher for hibernate. jdbc. factory_class. This attribute is not required for all applications. Value, which defines the Class Name of the factory.
(5) hibernate. jdbc. use_scrollable_resultset Hibernate uses a rolling record set of JDBC2. You need to set this parameter when using the JDBC connection provided by the user. Otherwise, Hibernate uses the connection
Connect to MetaData. Valid value: true | false.
(6) When hibernate. jdbc. use_streams_for_binary reads or writes binary data or serializes data from JDBC or to JDBC, the stream is used. System-level data. Set true | false.
(7) after hibernate. jdbc. use_get_generated_keys is inserted, you can use the value in JDBC3 PreparedStatement. getGeneratedKeys () to retrieve the locally generated key value. JDBC3 + and
JRE1.4 +. If your driver is faulty after Hibernate identifier generator is used, set it to false. By default, you can connect to MetaData. Valid value: true | false.
(8) hibernate. connection. provider_class implements the class name of org. hibernate. connection. ConnectionProvider interface, which provides a connection for Hibernate.
(9) hibernate. connection. isolation sets the JDBC transaction isolation level. Check the constant value defined by java. SQL. Connection. Note that most databases do not support all isolation levels, some additional and non-standard isolation levels. Optional values: 1, 2, 4, and 8.
(10) automatic submission of hibernate. connection. autocommit JDBC shared connection. (Not recommended), true | false.
(11) hibernate. connection. release_mode specifies when Hibernate should release the JDBC connection. By default, JDBC always exists and is explicitly closed or disconnected only when the Session is closed.
Connection time. For the JTA data source on the application server, you should use after_statement to force release the JDBC connection after each JDBC request ends. For non-JTA data sources, it is usually released after each transaction ends.
JDBC connection is meaningful. When this value is set to auto, the after_statement mode is selected for JTA and CMT transaction policies. In the JDBC transaction policy, select after_transaction. Value: auto (default ),
On_close, after_statment, after_transaction. Note: This setting only affects sessions opened through SessionFactory. openSession. For
Session obtained through SessionFactory. getCurrentSession. The configuration of the CurrentSessionContext implementation class is used to control the connection release mode of these sessions.
(12) hibernate. connection. Pass these attributes to DriverManager. getConnection.
(13) hibernate. jndi. Pass these attributes to the JNDI InitialContextFactory.

4.3 Hibernate Cache attributes

(1) CacheProvider class name customized by hibernate. cache. provider_class.
(2) hibernate. cache. use_minimal_puts spend more read operations to optimize the minimum write operations for the second-level cache. This operation is very useful for cluster cache. In Hibernate3
This function is enabled by default. Valid value: true | false.
(3) Enable query cache for hibernate. cache. use_query_cache. You must enable query cache for some queries. Valid value: true | false.
(4) hibernate. cache. use_second_level_cache may have been used to completely blocking the second-level cache. It is enabled by default, and the CacheProvider implementation class is specified. Valid value: true | false.
(5) hibernate. cache. query_cache_factory implements the Class Name of the QueryCache interface. The default value is the built-in StandardQueryCache.

(6) hibernate. cache. region_prefix uses the prefix of the second-level cache region name.
(7) hibernate. cache. use_structured_entries forces Hibernate to use a more user-friendly format when storing data to the second-level cache. Valid value: true | false.

4.4,Hibernate transaction attributes

(1) hibernate. transaction. transaction_class uses the Class Name of the TransactionFactory of the Hibernate transaction APIs. The default value is JDBCTransactionFactory.

(2) jta. UserTransaction JTATransactionFactory uses the JNDI name to get JTA UserTransaction from the application server. Valid value: jndi/ut.

(3) hibernate. transaction. manager_lookup_class is the class name of the TransactionManagerLookup implementation class. This value is required when JVM-level cache is enabled or hilo generator is used in the JTA environment.

(4) If hibernate. transaction. flush_before_completion is set to true, the session will automatically push the value of the cache area to the database before the transaction Completes. Built-in and automatic session context management are preferred. Valid value: true | false.

(5) If hibernate. transaction. auto_close_session is set to true, the session will be automatically closed after the transaction is completed. Built-in and automatic session context management are preferred. Fetch

Value: true | false.

4.5 Miscellaneous properties mixed attributes

(1) hibernate. current_session_context_class provides a policy for the current session lifecycle. Value: jta, thread, managed, and custom class name.

(2) hibernate. query. factory_class: select the parsing implementation class of HSQL. Valid values: org. hibernate. hsql. ast. ASTQueryTranslatorFactory and

Org. hibernate. hsql. classic. ClassicQueryTranslatorFactory.

(3) mappings between tags in hibernate. query. subsitutions Hibernate and those in SQL. For example, it can be a function name or a common string name. Value, such as hsqLiteral = SQL _LITERAL,

HqlFunction = SQLFUN.

(4) hibernate. hbm2ddl. auto when SessionFactory is created, it automatically verifies or outputs the mode DDL (also the table creation statement) to the database. That is to say, the database table structure is automatically verified or deleted first.

In addition to re-establishing, or updating the data table structure in the original table structure. When the value is set to create-drop, the table structure in the database will be deleted when SessionFactory is cleared. Values: validate, create, update,

Create-drop.

(5) hibernate. cglib. use_reflection_optimizer uses CGLIB to replace the runtime reflection (System Level Attribute ). Reflection is very useful when there is a fault. Note that Hibernate always needs CGLIB,

Even after you disable optimization. You cannot set this attribute in the hibernate. cfg. xml file.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.