1: hibernate, what are?
1: hibernate is a JDBC package is responsible for object persistence, in the middle layer, in the application and database has played a role as a bridge between a mapping tool.
Second: Why is the use of hibernate?
1: the use of hibernate, the application layer can focus on business logic implementation, only need to know how to use the interfaces provided by hibernate, but do not know the specific details of implementation.
2: Packaging a JDBC, you can connect the database to reduce redundant code.
3: the direct use of mapping file, you can easily achieve with different database connections.
Three: hibernate use
1: configuration file
(1): global configuration file: hibernate.cfg.xml: mainly used to achieve the entire application procedure used in the database, as well as the application of object-relational mapping.
Examples:
<? xml version ='1 .0 'encoding =' utf-8 '?>
<! DOCTYPE hibernate-configuration PUBLIC
"- / / Hibernate / Hibernate Configuration DTD 3.0 / / EN" "/ home/soft01/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class"> com.mysql.jdbc.Driver
</ property> specified database driver
<property name="connection.url"> jdbc: mysql: / / 127.0.0.1:3306 / test
</ property> specify a database URL
<property name="connection.username"> root
</ property> link a database user name,
<property name="connection.password"> 831022 </ property> password
<propertyname="dialect"> org.hibernate.dialect.MySQLDialect
</ property> specify the database dialect is, in essence, hibernate API is used to mark a different database interface
<! - Echo all executed SQL to stdout ->
<property name="show_sql"> true </ property>
Used at the console output hibernate generated SQL statement
<property name="format_sql"> true </ property> on the SQL statement to format

<mapping object-relational mapping
resource = "com / allanlxf / hibernate / basic / entity / Student.hbm.xml" />
</ session-factory>
</ hibernate-configuration>
************************************************** *****************
Caution:
◆ any one application of the overall configuration file names are hibernate.cfg.xml, and default on the project's root directory, that is, with the source code at the same level. Each project can have such a global configuration file, if you want to use a different configuration file locations, call the configuration of the constructor has parameters to specify the configuration file.
If it is not prepared to use tools, then this configuration file should not be the same. Classpath on a piece of.
(2): --- Object Relational Mapping documents:
◆ naming rules: className.hbm.xml
◆ ORM details
Class to Table
Identifier to PK
Properties to columns
java type vs database type
◆ the role and application: used to enable the entity class object (class) with the database tables have a one-to-one relationship
<? xml version = "1.0" encoding = "gbk"?>

<! DOCTYPE hibernate-mapping PUBLIC
"- / / Hibernate / Hibernate Mapping DTD 3.0 / / EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="com.allanlxf.hibernate.basic.entity.Student" table="student">
<id name="id" column="id" type="integer">
<generator />
</ id>
<property name="name" column="name" type="string" />
<property name="email" column="email" type="string" />
<property name="birthday" column="birthday" type="date" />
<property name="address" column="address" type="string" />
</ class>
</ hibernate-mapping>
************************************************** *****************
◆ Caution:
(1) to build the table when using a root will usually not related to business logic field as the primary key.
(2) This mapping file can be a category one can also put multiple types of mapping relations go not written in a object-relational mapping file, but recommend the use of the former, because of this process in the development of easy to distinguish between management.
(3) of the document the best types of documents with the entities together.
************************************************** *****************
2: hibernate use interface六步曲
(1) Configuration config = new Configuration ();
/ / According to the location of the default hibernate configuration file, create a configuration object

(2) SessionFactory factory = config.configure (). BuildSessionFactory ();
(3) Session session = factory.openSession ();// applications and data dealing with an object
(4) Transaction trans = session.beginTransaction ();
(5) through the operation of the database session object
session.delete ();
session.save ()
session.update ()
session.load ();
session.find ();
(6) trans.commit ();
(7) turn off resources
************************************************** *****************
◆ the use of Note:
(1) in the creation of SessionFactory object, if re-modify configuration files, then behind the operation does not return, because when creating SessionFactory object has put the original configuration file information is loaded into the cache of the SessionFactory, sessionfactory is a heavyweight object, usually after a Do not destroyed, because the creation and destruction of a SessionFactory object consumes a lot of necessary resources
(2) session is a lightweight object, usually a session object is only used in a transaction applicable to End on the turn off, of course, started when a transaction must first create an object of this.
◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆
Topics II DB primary key in the generation of rules
Study direction: and bearing in mind the distinction between several frequently used method of ID

First: the embodiment of the mapping file:
<id name="id" column="id" type="integer">
<generator />
</ id>
oracle: sequence, seqhilo, hilo, increment, native
mysql: identity, increment, hilo, native
1: Assigned: specified by the user's own business logic of a relevant, if not generator, then the system default specified by the user, this relatively small, preferably not the primary key associated with the business logic
2: Native: more versatile, Mysql.sqlserver: identity
Oracle.db2: sequence
Disadvantage: Oracle of all of the tables are the use of a sequence
3: Identity:
(1): the use of rules:
The underlying database to generate labeled Fu,
(2): a precondition: the underlying database to support the growth of the field types automatically
4: Sequence:
(1) Usage:
A table for each of the ID to create a separate sequence, then the sequence obtained from the automatic increases in the identifier. Assignment to the primary key. Once created you can put it as a class object, the access to current or next value student_id_seq.curval / student_id_seq.nextval
(2) prerequisites: Supporting sequence database. oracle support, mysql does not support
Create sequence student_id_seq by 2 start with 1
Create table student (id number primary key, name varchar2 (20));
Insert into student values (student_id_seq.curval, "xuzheng");
5: Hilo:
According to high / low algorithm to generate the ID, hibernate table based on specific field as a high value, in the default selection hibernate_unique_hi
6: increment:
hibernate in order to automatically generate growth indicators for each increment of 1
◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆
Three topics: relationship
Study direction: the focus of understanding the reality of abstract relationship,

1: one-on-one relationships:
1: The first implementation is the primary key association, in essence, a table for selecting the primary key have to rely on a primary key.
◆ only been the object of reference exist, and to create reference objects
Examples:
Table 1: create table car_pk (
id number (10,0) not null,
name varchar2 (15),
primary key (id)
);
Table II: create table engine_pk (
id number (10,0) not null,
model varchar2 (20),
primary key (id)
);
alter table engine_pk
add constraint fk_engine_car_pk
foreign key (id)
references car_pk (id);
Was the light side configuration file
<hibernate-mapping package=" o2o.entity">
<class name="Car" table="car_pk">
<id name="id" column="id" type="integer">
<generator />
</ id>
<property name="name" column="name" type="string" length="15" not-null="false"/>
(Own-to - people)
<one-to-one name="engine" cascade="all"/>
</ class>
</ hibernate-mapping>
========================
Table II configuration file
<hibernate-mapping package=" o2o.entity">
<class name="Engine" table="engine_pk">
<id name="id" column="id" type="integer">
<generator> / / the id reference to the method used foreign
<param name="property"> car </ param> / / by the light of those
</ generator>
</ id>
<property name="model" column="model" type="string" length="20"/>
<one-to-one name="car" constrained="true" foreign-key="fk_engine_car_pk"/>
</ class>

2: The second implementation is a foreign key association
Examples:
Table 1: create table car_fk (
id number (10,0) not null,
name varchar2 (15) not null,
primary key (id)
);

Table II: create table engine_fk (
id number (10,0) not null,
carid number (10,0) unique,
primary key (id)
);
alter table engine_fk
add constraint fk_engine_car_fk
foreign key (carid)
references car_fk (id);
Was the light side configuration file
<hibernate-mapping package=" o2o.entity">
<class name="Car" table="car_fk">
<id name="id" column="id" type="integer">
<generator />
</ id>
<property name="name" column="name" type="string" length="15" not-null="true"/>
<one-to-one name="engine" property-ref="car" cascade="save-update"/>
/ / casecade referenced on the side. Save or update the current marked object, the object associated cascade change. The default is none.
</ class>
</ hibernate-mapping>

The light-side configuration file
<hibernate-mapping package=" o2o.entity">
<class name="Engine" table="engine_fk">
<id name="id" column="id" type="integer">
<generator/>
</ id>
<many-to-one name="car" column="carid" unique="true" foreign-key ="fk_engine_car_fk"/>
</ class>
</ hibernate-mapping>
II: one-to-many:
1: Use the rules:
(1) to build a unilateral table only to retain their own property
(2) to build that many side table, plus some order_id as its foreign key
(3) one-to-many, two-way link, the party must specify inverse = "true", means that multi-party has a master control, multi-party correlation only associated with a mirror. That is, hibernate detects a multi-party and have changed the time, only in accordance with the multi-object state changes to update the database simultaneously.
2: correlation direction
(1) one-way association: in a single party do not need to set up a collection of multi-store property.
(2) two-way association: in a single party has a required property set to store many of the party
3: Examples of two-way link
create table ec_order (
id number (10,0) not null,
primary key (id)
);
create table ec_item (
id number (10,0) not null,
orderid number (10,0) not null,
primary key (id)
);
alter table ec_item
add constraint fk_item_order
foreign key (orderid)
references ec_order (id);
Side configuration file:
<hibernate-mapping package=" o2m.entity">
<class name="Order" table="ec_order">
<id name="id" column="id" type="integer">
<generator />
</ id>
<set name="items" cascade="all-delete-orphan" inverse="true">
<key column="orderid" />
<one-to-many />
</ set>
</ class>
</ hibernate-mapping>
Multi-configuration files
<hibernate-mapping package=" o2m.entity">
<class name="Item" table="ec_item">
<id name="id" column="id" type="integer">
<generator />
</ id>
<many-to-one name="order" column="orderid" not-null="true" foreign-key="fk_item_order"/>
</ class>
</ hibernate-mapping>
Three: many-to-many relationships:
Only two-way many-to-many association.
Many-to-many object references are set
Necessary to use the third table to maintain the many-to-many relationship.
Writing configuration file when the many-to-many tags to be set within the tag, <key column =""> always and the type of the corresponding primary key id
<column=""> always and the associated class that corresponds to the primary key id
Such as student at the configuration document:
<set name="courses" table="ENROLLMENTS" inverse="true" cascade="save-update">
<key column="sid"/>
<many-to-many column="cid">
</ many-to-many>
</ set>
Note: the cascade of two configuration files are set to save-updat.
Add a relationship when the attention of:
In the many-to-many relationship, both sides have control over (that is, both sides did not set inverse = true) when the two sides should put cascade relations reflected in the database go, the situation triggered by the implementation of the two are exactly the same sql statement, in violation of the database primary key unique constraint,
☆ code below:
Applications:
Stu.getCourses (). Add (course);
Course.getStudents (). Add (stu);
There are two solutions:
In the above statement can choose an implementation
At a party to the configuration file inverse = "true", then let the party have not been provided to operate.
create table students (
sid integer primary key,
name varchar2 (32) not null,
sex varchar2 (8),
birthday date,
edu varchar2 (64),
cardno varchar2 (16) not null unique
);

create table courses (
cid integer primary key,
name varchar2 (32) not null unique,
desc varchar2 (256)
);
create table enrollments (
cid integer references courses (cid),
sid integer references students (sid),
constraint enrollments_pk primary key (cid, sid)
);
<hibernate-mapping package="hbn.many2many">
<class name="Student" table="STUDENTS">
<id name="sid" unsaved-value="null">
<generator>
</ generator>
</ id>
<property name="name"/>
<property name="sex"/>
<property name="birthday"/>
<property name="edu"/>
<property name="cardno" unique="true" not-null="true"/>
<set name="courses" table="ENROLLMENTS" inverse="true" cascade="save-update">
<key column="sid"/>
<many-to-many column="cid"> </ many-to-many>
</ set>
</ class>

<class name="Course" table="COURSES">
<id name="cid" unsaved-value="null">
<generator>
</ generator>
</ id>
<property name="name" unique="true" not-null="true"> </ property>
<property name="desc"> </ property>
<set name="students" table="ENROLLMENTS" cascade="save-update">
<key column="cid"> </ key>
<many-to-many column="sid"> </ many-to-many>
</ set>
</ class>
</ hibernate-mapping>
======================
Iv: combinatorial mapping
Elements contained in Component Mapping is another category (Table)
Two classes to build a table, the main property of a number of correspondence tables are a number of field combinations
create table account (
id integer primary key,
name varchar2 (32) not null,
balance double not null,
street varchar2 (32),
city varchar2 (32),
province varchar2 (32),
zip varchar2 (16)
);================================================ ================================================== =
<hibernate-mapping package="hbn.valuetype.component">
<class name="Account" table="ACCOUNT">
<id name="id" unsaved-value="null">
<generator>
<param name="table"> pk_table </ param>
<param name="column"> id_value </ param>
</ generator>
</ id>
<property name="name"> </ property>
<property name="balance"> </ property>
<component name="addr">
<property name="street"> </ property>
<property name="city"> </ property>
<property name="province"/>
<property name="zip"/>
</ component>
</ class>

</ hibernate-mapping>
================================================== =================================================
Friday: inheritance mapping
table-per-concreate-class an application to build a table
That is, the parent category separately in the sub-category, rather than build a separate table
-------- Do not need to many inquiries, the application of state mapping, operating only on a single table
Disadvantage: the time to amend the parent category, and its concrete subclass corresponding to the table field should also be changed;
Do not support polymorphism, a multi-role, it is very difficult to maintain data integrity.
Advantages: easy to use, because a specific category exists only one table.
◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆
table-per-class-hierarchy-level Writing the whole table
-------- Required many states and sub-class property with less time to apply
Disadvantage: there will be a large number of null fields and sub-class of property should not have restrictions on non-empty.
Advantages: use only one form, a single sql query
◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆ ◆
table-per-subclass of a class to write a table, including the parent
-------- Required many states and many sub-class property is called when the application
Disadvantages: There are too many data tables, each table corresponds to a class (in the many-to-many relationships was also a need for an extra table to preserve the relationship);
Query time to multi-table query, read the data longer. Solution is to organize the data in the database to a different disk surface (assuming the head of each operation are independent). This mode of reading and writing performance issues will be a certain ease.
Advantages: very good to follow the idea of object-oriented, multi-state, required only at the right table has a role in the field can be required. To amend the succession of the parent tree class and subclass are easy, because only need to modify a class or a table.

Four topics: Small knowledge points

1: lazy lazy = "true"
Nature: When the call student.load () method, would not immediately put the full contents of the database is loaded into memory, only to initialize the OID property, when the first examples of the use of the time, will put all the contents of loaded into memory. Improve efficiency
2: Insert = false: At the table to insert the time, ignore this field, default value = true
3: update = false: to amend the time lost sight of the field, default value = true.
Source: http://bbs.tarena.com.cn/viewthread.php?tid=133157