Common Configuration for Hibernate:
1, the core configuration, there are two ways to configure
Configure with the properties file:
-hibernate.cfg.xml
Format: Key=value
Note: The mapping file (database table-entity Class) cannot be loaded in the properties file. Manual encoding must be used to load
XML format File configuration:
-hibernate.properties
Format:<property></property> Tags
In the core configuration:
(1), the required configuration
* 4 basic parameters to connect to a database
Hibernate.connection.driver_class connecting to a database driver
Hibernate.connection.url Connection Database URL
Hibernate.connection.username Database user Name
Hibernate.connection.password Database Password
*hibernate.dialect Operating Database dialect
(2), optional configuration
*hibernate.show_sql true to output SQL statements in the console
*hibernate.format_sql true to format the console output SQL statement
*hibernate.connection.autocommit true whether the transaction is automatically committed
*hibernate.hbm2ddl.auto create/create-drop/update/validate
* Create: Creates a new table each time it executes. (If you have previously had the table, delete the table and recreate it.) The use of general testing.
* Create-drop: Each time the execution, create a new table, the execution of the program after the end of the table, deleted. Used in general testing.
* Update: If there are no tables in the database, create a new table, and if so, use this table directly. You can update the structure of the table .
* Validate: The original table will be used. Verify that the mapping file is consistent with the fields that are configured in the table. Inconsistent error.
(3), mapped configuration
* Load the mapping file in the core configuration file:
* <mappingresource= "Cn/itcast/hibernate3/demo1/customer.hbm.xml"/> * loaded using manual encoding
2. Configuration of mapping files
ORM: Object and relational mappings.
* Configure Java objects and table mappings.
* Configure the mapping of classes to tables:
* Name: The full path of the class:
* Table: Names of tables: (can be omitted. Use the name of the class as the table name.)
<class name= "Cn.itcast.hibernate3.demo1.Order" table= "Orders" >
* Configure normal and field mappings:
<property name= "name" column= "name" type= "string"length= "/>"
Type: Three ways of writing
* Java type: java.lang.String
* Hibernate Type: string
* SQL Type: Cannot use the type attribute directly, need sub-label <column>
* <columnname= "name" sql-type= "varchar"/>
* Configure unique identities with primary key mappings:
* There is only one primary key form in a table:
<id name= "id" column= "id" >
* Build Policy:
* One table corresponds to multiple primary key forms: (Composite primary key:)---understand.
* <composite-id></composite-id>
* Association Relationship:
* The name sql:<queryname> is written in hql statement,<sql-queryname> is written in the SQL statement, after the predefined SQL, use, directly use the name
<queryname= "FindAll" >
From Customer
</query>
<sql-queryname= "Sqlfindall" >
SELECT * FROM Customer </sql-query>