[Repost] Hibernate Getting Started - Basic Configuration

xiaoxiao2021-03-06  47

Hibernate profiles can have two formats, one is hibernate.properties, and 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.substitudes True 1, False 0, Yes 'Y', NO 'N' This configuration means that when you enter True in Hibernate, Hibernate will turn into 0 insert the database. When you enter false in Hibernate, Hibernate will transform Insert the database, the same, the same, the same. 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. Code: hibernate.dialect net.sf.hibernate.dialect.MySQLDialect hibernate.connection.driver_class org.gjt.mm.mysql.Driver 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, and all the connection parameter templates of different databases are given. Code: hibernate.connection.pool_size 1 hibernate.Statement_cache.size 25 This is the configuration parameters of the Hibernate comes with the connection pool, 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 the DBCP connection pool, you need to cancel the downlink: Code: hibernate.connection.provider_class net.sf.hibernate.Connection.dbcpConnectionProvider Other connection pools.

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: code: hibernate.diaract.MysqldiaLect Hibernate.Connection.DataSource MyPool Hibernate. Connection.Provider_class net.sf.hibernate.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 a remote client program, but you want to connect pools in the App Server database, then you need to configure JNDI parameters, such as Hibernate Connect to remote WebLogic database connection pool: code : hibernate.dialect 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: Code: hibernate.tatage.Factory.Trassaction.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. Code: # hibernate.connection.ission 4 Specify the isolation level of the database, often different databases have their own quarantine levels, not necessarily Hibernate settings, so you don't have to take it. Code: hibernate.jdbc.fetch_size 50 hibernate.jdbc.batch_size 25 These two options are very important! ! ! It will seriously affect Hibernate's crud performance! Code: 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.

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

New Post(0)