Download Hibernate , open the address www.hibernate.org , click Hibernate ORM -> Downloads to download version 4.3.11 , to use Hibernate , you need to copy the required directory and optional\c3p0 under Lib to the WEB-INF\lib directory of the site, where The required directory is the required library for Hibernate ; the optional\c3p0 directory is the required library for the C3P0 data source; the data source driver is also copied to the WEB-INF\lib directory. If it is a MySQL database, it can be downloaded from the www.mysql.com site.
-
Hibernate configuration file, you can use the *.properties property file, you can also use the XML file configuration, the following uses the XML file configuration:
-
The configuration file structure is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name=" property name "> property value </property>
... multiple property configurations
<mapping class=" The class name of the persistent class "/>
... class names of multiple persistent classes
</session-factory>
</hibernate-configuration>
-
property description
- C3P0 attribute name connection.driver_class : Specifies the driver used to connect to the database, MySQL example com.mysql.jdbc.Driver
- C3P0 attribute name connection.url : specify the connection URL of the database , MySQL example jdbc:mysql:// database server address / database name
- C3P0 attribute name connection.username : database login user name
- C3P0 attribute name connection.password : database login user password
- C3P0 property name dialect : Specifies the dialect of the database, MySQL example org.hibernate.dialect.MySQLInnoDBDialect
- C3P0 property name hibernate.c3p0.max_size : the maximum number of connections in the connection pool
- C3P0 attribute name hibernate.c3p0.min_size : the minimum number of connections in the connection pool
- C3P0 property name hibernate.c3p0.timeout : Specify the connection timeout period in the connection pool
- C3P0 property name hibernate.c3p0.max_statements : Specify the maximum number of Statements cached by the connection pool
- C3P0 property name hibernate.c3p0.idle_test_period :
- C3P0 property name hibernate.c3p0.validate :
- Property name show_sql : Displays the SQL generated by Hibernate persistence
- Property name hibernate.format_sql : Format the SQL script and then output it
- Property name hibernate.use_sql_comments : Whether to add comments to Hibernate -generated SQL statements that are helpful for debugging
- Property name hibernate.jdbc.batch_size : Specifies the size of the JDBC2 batch update, integer value
-
Attribute name hbm2ddl.auto : operate on table structure
- Value create : every time hibernate is loaded , the last generated table will be deleted, and then a new table will be regenerated according to your model class, even if there is no change twice;
- The value of create-drop : a table is generated according to the model class every time hibernate is loaded , but as soon as the sessionFactory is closed , the table is automatically deleted;
- Value update : When hibernate is loaded for the first time , the table structure will be automatically established according to the model class (provided that the database is established first), and the table structure will be automatically updated according to the model class when hibernate is loaded later , even if the table structure has changed, but the rows in the table Still there will not delete the previous row. It should be noted that when deployed to the server, the table structure will not be established immediately, it will not be established until the application is run for the first time;
- Value validate : every time hibernate is loaded , the database table structure is verified to be created, it will only be compared with the tables in the database, no new tables will be created, but new values will be inserted
-
Property name hibernate.current_session_context_class : Set the underlying implementation type of SessionFactory.getCurrentSession()
- Value thread : Implemented using org.hibernate.context.ThreadLocalSessionContext to track and delimit the context-related Session through the currently executing thread
- Value jta : Implemented using org.hibernate.context.JTASessionContext to track and delimit context-related Sessions according to JTA
- Value managed : org.hibernate.context.ManagedSessionContext implementation, tracking and defining the context-related Session through the currently executing thread , but the program needs to use the static method of the entire class to bind and unbind the Session instance, it will not be automatically opened , flush or close
-
mapping description
- Attribute class : The class name of the persistent class
- Attribute resource : mapping file of persistent class
-