Hibernate-----4, Hibernate configuration file

Source: Internet
Author: User
Tags postgresql org sybase

First, create a configuration object

The only function of a configuration instance is to create a sessionfactory instance; If the mapping definition file is in the class load path, the AddResource () method can be used to add the mapping definition file

3 Ways to configure Hibernate:

using Hibernate.properties as a configuration file (\project\etc\hibernate.properties)

Configuration cfg = new configuration ();//Call the AddResource method multiple times, add the mapping file Cfg.addresource ("Item.hbm.xml"); Cfg.addresource ( "Bid.hbm.xml");

Because the mapping file and persistence class are often one by one corresponding, you can allow hibernate to search the mapping file by adding a persistence class to the Configuration object (in this way, all the mapping files should be placed under the same package path as the persisted class file)

Configuration cfg = new configuration ();//Call the AddClass method multiple times to add a persistent class Cog.addclass ("Lee. Item.class "); Cog.addclass (" Lee. Bid.class ");

using Hibernate.cfg.xml as a configuration file (\project\etc\hibernate.cfg.xml)

Configuration cfg = new configuration ();//load hibernate.cfg.xmlcfg.configure ();

Create a configuration instance directly without using a config file

The configuration object provides the following three methods:

Configuration AddResource (String resourcename): Add a mapping file for a configuration object

Configuration setproperties: Sets a series of properties for a configuration object, which is passed through the Propertiest instance

Configuration SetProperty (String propertyname,string value): Sets a separate property for the configuration object

public class newsmanager{public static void Main (string[] args) throws exception{Configuration conf = new Config Uration (). addclass (Org.app.News.class). SetProperty ("Hibernate.connection.url", "jdbc:mysql://localhost/ Hibernate "). SetProperty (" Hibernate.connection.username "," root "). SetProperty (" Hibernate.connection.password "," 123 "). SetProperty (" Hibernate.connection.c3p0.max_size "," a "). SetProperty (" Hibernate.connection.c3p0.min_size ", "1"). SetProperty ("Hibernate.connection.c3p0.timeout", "the") SetProperty ("Hibernate.connection.c3p0.max_ Statements "," (")"). SetProperty ("Hibernate.connection.c3p0.idle_test_period", "the"). SetProperty (" Hibernate.connection.c3p0.acquire_increment "," 2 "). SetProperty (" Hibernate.connection.c3p0.validate "," true "). SetProperty ("Hibernate.connection.dialect", "Org.hibernate.dialect.MySQLInnoDBDialect"). SetProperty ("              Hibernate.hbm2ddl.auto "," create "); Sessionfactory SF = Conf.buildsessioNfactory ();             Session sess = Sf.opensession ();             Transaction tx = Sess.begintransaction ();             News n = new News ();             N.settitle ("title");             N.setcontent ("content");             Sess.save (n);             Ct.commit ();     Sess.close (); }}


Second, JDBC Connection properties

The name and semantics of all hibernate attributes are defined in Org.hibernate.cfg.Environment

<!-Connecting the database--><!-setting the driver--><property name= "Connection.driver_class" of the Connection database > oracle.jdbc.driver.oracledriver</property><!-Setting the URL of the required connection database service--><property name= "Connection.url" >jdbc:oracle:thin: @localhost: 1521:orcl</property><!-The user name of the connection database--><property name= " Connection.username ">system</property><!-Connection Database password--><property name=" Connection.password "> 123</property><!-the maximum number of connections to the connection pool using the C3PO connection pool--><!--<property name= "Hibernate.c3po.max_size" >20 </property><!-Connection Pool Minimum number of connections--><property name= "Hibernate.c3po.min_size" >1</property><!- Connection Pool Connection Timeout length--><property name= "Hibernate.c3po.timeout" >5000</property> <!-- Connection pool Maximum cache number of statement objects--><property name= "Hibernate.c3po.max_statements" >100</property>< Property Name= "Hibernate.c3po.idle_test_period" >3000</property><property name= " Hibernate.c3po.acquire_increment ">2</property><property Name= "Hibernate.c3po.validate" >true</property> 


Third, the database dialect

Tell hibernate which database will be used at the bottom of the application

<propertyname= "dialect" >org.hibernate.dialect.MySQLInnoDBDialect</property>

DB2 Org.hibernate.dialect.DB2Dialect

DB2 as/400 Org.hibernate.dialect.DB2400Dialect

DB2 OS3090 Org.hibernate.dialect.DB2390Dialect

PostgreSQL Org.hibernate.dialect.PostgreSQLDialect

MySQL Org.hibernate.dialect.MySQLDialect

MySQL with InnoDB Org.hibernate.dialect.MySQLInnoDBDialect

MySQL with MyISAM Org.hibernate.dialect.MySQLMyISAMDialect

Oracle (any version) Org.hibernate.dialect.OracleDialect

Oracle 9i Org.hibernate.dialect.Oracle9iDialect

Oracle 10g Org.hibernate.dialect.Oracle10gDialect

Sybase Org.hibernate.dialect.SybaseDialect

Sybase Anywhere Org.hibernate.dialect.SybaseAnywhereDialect

Mircriosoft SQL Server Org.hibernate.dialect.SQLServerDialect

SAP DB Org.hibernate.dialect.SAPDBDialect

Informix Org.hibernate.dialect.InformixDialect

Hypersonicsql Org.hibernate.dialect.HSQLDialect

Ingres Org.hibernate.dialect.IngresDialect

Progress Org.hibernate.dialect.PregressDialect

Mckoi SQL Org.hibernate.dialect.MckoiDialect

Interbase Org.hibernate.dialect.InterbaseDialect

Pointbase Org.hibernate.dialect.PointbaseDialect

Frontbase Org.hibernate.dialect.FrontbaseDialect

Firebird Org.hibernate.dialect.FirebirdDialect

   

Iv. Link Properties for jndi data sources

Hibernate can use Jndi (javanaming diretory interface,java named directory interface) data source configuration if you do not need hibernate to manage the data source, but directly access the container management data source;

The following are the main configuration properties of the connection Jndi data source:

Hibernate.connection.datasource: Specifies the data source Jndi name Hibernate.jndi.url: Specifies the URL of the Jndi provider (optional), If the code for Jndi persistent access is in an app, you do not need to specify the attribute Hibernate.jndi.class: Specifies the implementation class for Jndi initialcontextfactory (optional). If the code for Jndi persistent access is in an app, you do not need to specify the attribute Hibernate.connection.username: Specifies the user name of the connection database (optional) Hibernate.connection.password: Specify the password to connect to the database (optional)

Configure hibernate; Connect the configuration fragment of the data source in Tomcat:

<property name= "Connection.datasource" >java:comp/env/jdbc/dstest</property><property name= " Dialect ">org.hibernate.dialect.MySQLDialect</property>

V. Properties of Hibernate things

Hibenate not only provides local things support, but also allows the use of container-managed global Things


Hibernate.transaction.factory_class: Specifies the type of object factory used by hibernate (the attribute value must be a direct or indirect subclass of Transactionfactory)

Jta. UserTransaction: The property value is a jndi name. Hibernate will use Jtatransactionfactory to get JTA from the application server UserTransaction

Hibernate.transaction.manager_lookup_class: The attribute value should be a Transactionmanagerlookup class name when using a JVM-level cache, or when using the Hilo Generator policy in a JTA environment , this class is required

Hibernate.transaction.flush_before_completion: Specifies whether the session automatically flushes data to the underlying database after the thing is done

Hibernate.transaction.auto_close_session: Specifies whether to automatically close the session after the end of the thing

六、二级 Cache related Properties

Hibernate's sessionfactory can hold an optional level two cache, which improves hibernate's persistent access performance by using this level two cache


Hibernate.cache.provider_class: Set class name for level two cache Cacheprovider

Hibernate.cache.use_minimal_puts: Optimizes level Two cache for minimal write operations at the expense of frequent read operations; This setting is useful for cluster caching and is enabled by default for cluster cache implementations

Hibernate.cache.use_query_cache: Sets whether the query cache is allowed, and the individual queries still need to display the settings to be cacheable

Hibernate.cache.use_second_level_cache: Used to set whether level two caching is enabled, this property completely disables the use of level two caches, and the two-level cache is turned on by default for persistent classes that have <cache.../> specified in the mapping file

Hibernate.cache.query_cache_factory: Set the class name of the query cache factory (default Standardquerycache), the query cache factory must implement the Querycache interface;

Hibernate.cache.region_prefix: Set prefix for level two buffer name

Hibernate.cache.use_structured_entries: Sets whether hibernate is forced to store data in a two-level cache in a more readable format

Seven, outer join fetch properties

Outer join fetching can limit the number of SQL statements to improve efficiency, and this outer join crawl fetches data from multiple data tables at once by using outer join in a single SELECT statement

Outer join fetching is allowed in a single SELECT statement through <many-to-one.../>, <one-to-many.../>, <many-to-many.../>, and <one-to-one.../> To get the entire object graph of the connection object.

Setting the hibernate.max.fetch_depth to 0 will prohibit the outer join crawl at the global scope, set to 1 or higher can enable the N-1 or 1-1 of the external connection crawl, in addition, the mapping file should also be specified through the fetch= "join" to specify this outer join crawl

Viii. Other Common configuration properties

Hibernate.show_sql: Whether to output hibernate-generated SQL statements (true, false) in the console

Hibernate.format_sql: Whether to turn SQL statements into well-formed SQL (true, false)

Hibernate.use_sql_comments: Whether to add comments (true, false) to help debug in hibernate-generated SQL statements

Hibernate.jdbc.fetch.size: Specifies the size (integer) of the number of JDBC fetches, which essentially calls the Statement.setfetchesize () method

Hibernate.jdbc.batch.size: Specifies the size (integer) of the batch update for hibernate using JDBC2, 5-30 is recommended

Hibernate.connection.autocommit: Setting whether to submit automatically (usually not recommended)

Hibernate.hbm2ddl.auto: Sets whether the database table is automatically created based on the mapping file when creating Sessionfactory, Create-drop: Displays the data table that was just built when the sessionfactory is turned off; Update, create, Create-drop)



Hibernate-----4, Hibernate configuration file

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.