Hibernate Getting Started - Basic Configuration

xiaoxiao2021-03-06  42

Hibernate Getting Started - Basic Configuration

Author: robbin (MSN: robbin_fan AT hotmail DOT com)

Copyright Notice: This article is strictly forbidden, if there is a reprint request, please contact the author.

Hibernate configuration files can have two formats, one is hibernate.properties, the other is hibernate.cfg.xml

The latter is slightly convenient. When adding an HBM mapping file, it can be added directly in hibernate.cfg.xml, and it is not necessary to join in the initialization code like hibernate.properties.

But no matter what, the two configuration items are the same, and the following details:

There is a hibernate.properties template in Hibernate's SRC directory, we don't have to write from the head, modify the template :)

Hibernate.query.substitude.query 1, false 0, Yes 'Y', NO 'N'

This configuration means that when you enter True in Hibernate, Hibernate will turn to 1 inserted into the database. When you enter false in Hibernate, Hibernate will turn into 0 inserted into the database, the following Y, n.

For some databases, such as Oracle, there is no Boolean data type, which is 1 representative true, 0 represents False, so use this configuration directly with true / false directly with True / False.

Hibernate.Dialev Net.sf.hibernate.dialect.mysqldiaLect

Hibernate.Connection.driver_class com.mysql.jdbc.driver

Hibernate.Connection.URL JDBC: MySQL: /// TEST

Hibernate.Connection.username root

Hibernate.connection.password

This is an example of connecting the MySQL database. It is very intuitive, not necessarily explained, all of which are given in the connection parameters of different databases.

Hibernate.Connection.Pool_size 1

Hibernate.Statement_Cache.Size 25

This is the configuration parameters of the connection pool of Hibernate, which will be adopted by default. It is very intuitive and not explained.

Just reminding a little, Hibernate is very a very simple connection pool. If you use Hibernate in the project, it is recommended that you prefer the App Server's connection pool, select the DBCP connection pool of the Hibernate tape. The self-contained connection pool should be selected as the end.

If you use a DBCP connection pool, in addition to configuring DBCP connection pools, you will need to cancel the downlink annotation:

Hibernate.Connection.Provider_class net.sf.hibcpConnectionProvider.dbcpConnectionProvider.provider.connection.provider.connection.

Other connection pools are sympathized.

If you use the App Server connection pool, assume that the App Server connection pool's JNDI name is "MyPool", the configuration should be as follows:

Hibernate.Dialev Net.sf.hibernate.dialect.mysqldiaLect

Hibernate.Connection.DataSource MyPool

Hibernate.connection.provider_class net.sf.hibider.connection.DataSourceConnectionProvider The other parameters do not have to be written, because it is already specified when the App Server is configured.

If you are not using Hibernate in the App Server environment, such as remote client programs, but you want to connect to the Pool of App Server, then you also need to configure JNDI parameters, such as the Hibernate connection to the database connection pool on remote WebLogic:

Hibernate.Dialev Net.sf.hibernate.dialect.mysqldiaLect

Hibernate.Connection.DataSource MyPool

Hibernate.connection.provider_class net.sf.hibernate.connection.DataSourceConnectionProvider

Hibernate.jndi.class WebLogic.jndi.wlinitialContextFactory

Hibernate.jndi.URL T3: // ServerName: 7001 /

Finally, if you need to use Hibernate in EJB or JTA, you need to cancel the downlink:

Hibernate.Transaction.Factory_Class Net.sf.hibernate.Transaction.jtatransActionFactory

Miscellaneous configuration:

Hibernate.show_sql false

Whether hibernate is sent to the SQL of the database is displayed, this is a very very useful function. When you debug hibernate, let Hibernate print SQL statements to help you solve problems quickly.

# hibernate.connection.ission 4

Specifies the isolation level of the database, often different databases have their own isolation levels, and may not be able to change it.

Hibernate.jdbc.Fetch_size 50

Hibernate.jdbc.batch_size 25

These two options are very important! ! ! Will seriously affect Hibernate's crud performance!

C = Create, R = Read, u = update, d = delete

Fetch size is the number of records removed from the database when setting the JDBC's Statement read data.

For example, 10,000 records in query, for Oracle's JDBC driver, will not take 10,000 pieces 1 time, and only take out the number of FETCH SIZEs, and after the record set over, after the completion of these records, then go Database Take the Fetch Size strip data.

Therefore, there is a big saving memory consumption. Of course, the larger the FETCH Size, the less the number of times the database is, the faster the speed; the smaller the fetch size, the more the number of times the database is read, the slower the speed.

This is a bit like usually writing the program to write a hard disk file. Set up a buffer, each time it is written into buffer, after the Buffer is full, write a hard drive, the truth is the same.

The Oracle database JDBC driver default fetch size = 10 is a very conservative setting, according to my test, when Fetch size = 50, performance will increase 1 times, when fetch size = 100, performance can also Continue to increase 20%, Fetch Size continues to increase, and the performance improvement is not significant. So I recommend using Oracle to set Fetch Size to 50.

However, not all databases support Fetch size feature, such as MySQL.

MySQL is like the worst case I said above, he always takes all 10,000 records, and the memory consumption will be very amazing! There is no good way this situation :(

Batch size is a batch size when setting a bulk deletion, bulk update, and batch insertion, a bit equivalent to setting the Buffer buffer size.

The larger the Batch size, the less the number of times the quantity is sent to the database, the faster the speed. One test result I did was when Batch size = 0, using Hibernate to delete 10,000 records for the Oracle database for 25 seconds, when Batch size = 50, delete only 5 seconds! ! !

How big can you improve! Many people do Hibernate and JDBC Insert performance tests will be strange to find that Hibernate is twice as JDBC, that is because Hibernate uses Batch INSERT, and their JDBC does not use Batch.

In my experience, the Oracle database Batch size = 30 is more appropriate, 50 is also good, performance will continue to increase, 50 or more, performance improvement is very weak, but consume more memory more, there is no need.

# hibernate.jdbc.use_scrollable_Resultset True

Set whether you can use the rolling result set of JDBC2.0 specification, which has a certain role in Hibernate's paging display, which is fine because of default.

# hibernate.cglib.use_reflection_optimizer false

Open by default, enable CGLIB reflection optimization. CGLIB is used to dynamically generate PO bytecodes in Hibernate, and open optimization can speed up the speed of the word code structure.

However, when you are in the process of debugging, especially in the application related to Proxy, Lazy Loading, the code is wrong, but the error message is unknown, then you can optimize CGLIB, so Hibernate will output more detailed debugging information Help you debug.

转载请注明原文地址:https://www.9cbs.com/read-51486.html

New Post(0)