Hibernate
Detailed configuration file:
Mapping file: Naming: Class name. hbm.xml
<?xml version= "1.0"?>
<! DOCTYPE hibernate-mapping Public
"-//hibernate/hibernate Mapping DTD 3.0//en"
"Http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" >
Package: Packages for objects to be mapped (optional, if not specified, all classes of this file must specify a full path)
Auto-import default is True, the package name is automatically imported when writing HQL
If you specify false, then write hql must write the full name of the class;
such as: Session.createquery ("from Cn.itcast.c_hbm_config. Employee "). List ();
<class name= "Employee" table= "Employee" >
Class maps An object (typically, an object writes a mapping file, which is a class node)
Name specifies the type of object to be mapped
Table specifies the tables for the object;
If no table name is specified, the default is the same as the object name
<!--primary key, map---
<id name= "EmpId" column= "id" >
<!--
The generation strategy of the primary key
Identity self-growth (MySQL, DB2)
Sequence self-growth (sequence), the self-growth in Oracle is implemented as a sequential method
Native self-growth "will choose identity or sequence based on how the underlying database grows from the ground up"
If it's a MySQL database, the self-growth approach is identity
If it is an Oracle database, use the sequence sequence to achieve self-growth
Increment self-growth (there will be problems with concurrent access, generally in the server cluster environment.) )
Assigned specifies the primary key generation policy to manually specify a value for the primary key
UUID Specifies the unique value that the UUID randomly generates
Foreign (foreign key way, one-to-one speaking)
-
<generator class= "native"/>
</id>
<!--non-primary key, map---
<property name= "EmpName" column= "EmpName" ></property>
<!--
Normal field mappings
Property
Name Specifies the property name of the object
column specifies the field name of the table that corresponds to the object property, and the default is consistent with the object property if not written.
length specifies the duration of the character, which defaults to 255
Type specifies the types of fields for the mapping table, if you do not specify a type that matches the property
Java type: Full name must be written
hibernate Type: Direct write type, all lowercase
-
<property name= "workdate" column= "Workdate" ></property>
<!--Composite PRIMARY key mapping--
<composite-id name="Keys">
<key-property name="UserName" type="string"></key-property>
<key-property name="Address" type="string"></key-property>
</composite-id>
<property name="age" type="int"></property>
</class>
Master profile: Name: Hibernate.cfg.xml can also be a different name, but the load path must be written in the load phase
<! DOCTYPE hibernate-configuration Public
"-//hibernate/hibernate Configuration DTD 3.0//en"
"Http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd" >
<session-factory>
<!--database Connection configuration--
<property name= "Hibernate.connection.driver_class" >com.mysql.jdbc.Driver</property>
<property name= "Hibernate.connection.url" >jdbc:mysql:///hib_demo</property>
<property name= "Hibernate.connection.username" >root</property>
<property name= "Hibernate.connection.password" >root</property>
Database version
<property name= "Hibernate.dialect" >org.hibernate.dialect.MySQL5Dialect</property>
Whether to display SQL statements in the console
<property name= "Hibernate.show_sql" >true</property>
<!--load all mappings--
<mapping resource= "Cn/itcast/a_hello/employee.hbm.xml"/>
Automatically create tables
#hibernate. Hbm2ddl.auto Create-drop executes the creation table each time the sessionfactory is created;
When calling Sesisonfactory's Close method, delete the table!
#hibernate. Hbm2ddl.auto create to re-build the table every time, and delete and create the table if it already exists
#hibernate. Hbm2ddl.auto Update If the table does not exist, create a table that does not exist;
#hibernate. Hbm2ddl.auto validate (when generating the environment) performs validation: When the contents of the mapping file are not the same as the database table structure, an error occurs!
</session-factory>
#hibernate. Dialect Org.hibernate.dialect.MySQLDialect
#hibernate. Dialect Org.hibernate.dialect.MySQLInnoDBDialect
#hibernate. Dialect Org.hibernate.dialect.MySQLMyISAMDialect
#hibernate. Connection.driver_class Com.mysql.jdbc.Driver
#hibernate. Connection.url jdbc:mysql:///test
#hibernate. Connection.username Gavin
#hibernate. Connection.password
Hibernate configuration file