Knowledge preparation:
(Reference: Tang sunshine Hibernate3.6 (basic ))
I. Talking about the relationship between javaBean
Existing relationships:
1. Association (one-to-one, one-to-many, multiple-to-one, and many-to-many)
2. Aggregation/combination (final consideration)
3. Inheritance
Therefore, mappings are mainly divided into association ing, inheritance ing, and other mappings.
2. hibernate ing studies the entire process of persistent objects.
Control ing through configuration file
Only two of the three entities can be identified, and the third entity can be obtained;
In this article, we mainly configure --------------- from javaBean + to automatically generate ------------- table ()
Automatic Generation is required. It is best to configure it in hibernate. cfg. xml during development.
Create-drop
True
Train of Thought:
Java entities consist of two parts: Data and relationships. Likewise, tables in the database are the same.
In different systems, the data is stored in a similar way. You only need to solve the relational encoding problem.
However, the relationship is quite different in different systems. hibernate is the solution for ing between javaObject and DBTable,
Then, we understand how the relationship between javaObject and DBTable is stored;
The data in javaObject takes the object as the basic unit,
There are two main types: one-to-one, and objects are connected by the other object as the attribute;
One-to-multiple: Creates a set and a set of objects.
In Db table, the relationship is established through the primary and Foreign keys.
The ing between the two is the problem <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + CiA8L3A + CjxwPgrV/cq90dC + signature + vLS/Signature + signature + csj9oak 52m + signature + CiA8L3A + signature + CjEu1 + 67 + bG + drawing/drawing + 8/Cw + a0 + drawing/MS/wrzPwr2owaKjrMjnsb7A/TwvcD4KPHA + csrxz8ijuw.vcd4kpha + drawing + fill = "brush: java; "> public class User {private int id; private String name; private Date birthday; // required public User () {}// get/set omitted
2) Create the User. hbm. xml file in the com. tjcu. domain directory.
3) In the hibernate. cfg. xml file, add
The above is a basic configuration
One difficulty is the primary key generation method.
{
1. Why is there such a wonderful primary key generator?
In java, the memory address is used to determine objects of the same class, and the table is determined by id.
2. Specific primary key generator
Native: Sequence is used for oracle, and identity (auto-incrementing Primary Key Generation Mechanism) is used for MySQL and SQL Server. native transfers primary key generation to the database, hibernate does not matter (very common ).
Identity: Use the auto-increment fields of SQL Server and MySQL. This method cannot be placed in Oracle. Oracle does not support auto-increment fields. You need to set sequence (which is commonly used in MySQL and SQL Server ).
Sequence: Call the sequence of the underlying database to generate the primary key. You must set the sequence name. Otherwise, hibernate cannot find the sequence.
Uuid: A 128-bit uuid algorithm is used to generate a primary key. uuid is encoded as a 32-bit hexadecimal number string. The occupied space is large (string type ).
Hilo: Use the hilo generation policy. Create an additional table in the database. The default table name is hibernate_unique_key. The default field is integer and the name is next_hi (rarely used ).
Foreign: Use the primary key of another associated object. Normally Joint Use
I have used these Apis. There are other APIs that can be queried based on different requirements. Generally, there is no limit. I sample native.
}
2. Associate Configuration
Single Association: only one-to-one association from Order to Customer is established, that is, the customer attribute is defined only in the Order class. You can also create a one-to-Multiple Association from Customer to Order, that is, define the orders set only in the Customer class.
Double join: establishes multiple-to-one association from Order to Customer, and one-to-Multiple Association from Customer to Order.
1) one-to-many/multiple-to-one
Features-to-one attributes:
* Name: Specifies the name of the persistent class to be mapped.
* Column: Set the foreign key of the table corresponding to the persistence class attribute.
* Class: Set the attribute type of the persistence class.
* Not-null: whether to allow null.
2) one-to-one
Must be differentiated between primary and secondary
For a 1-1 association based on a foreign key, the foreign key can be stored on any side,Add external-to-one at the end of the foreign key to be stored.Element. Add the unique = "true" attribute to the join-to-one element to indicate a 1-1 Association, and use the name attribute to specify the attribute name of the association attribute.
The other end needs to use the one-to-one element. This element uses the property-ref (which can be left blank) attribute to specify fields other than the primary key of the correlated object as the correlated fields.
3) Inheritance
Hibernate supports three inheritance and ing policies:
○ Each specific class is a table (table per concrete class) that maps each object in the domain model to an independent table, that is to say, you do not need to consider the inheritance and polymorphism in the domain model in the relational data model.
○ A table (hierarchy of each class) uses the same table for the Child classes in the inheritance relationship. Therefore, you need to add additional fields to the database table to distinguish child classes.
○ Each sub-class maps each class in a table (table per subclass) domain model to a table, and describes the inheritance relationship between tables through foreign keys in the relational data model. This is equivalent to creating tables in the database according to the structure of the domain model, and establishing the inheritance relationship between tables through foreign keys.
Comparison:
The Component Property indicates that the property of the persistence class is neither a basic data type nor a String, but a component variable. The type of the component property can beCustom class.
The name attribute cannot be mapped directly using property. To map component attributes, Hibernate providesComponentElement.
○ Each component element maps a component property. The component property must specify the type of the property.ClassAttribute is used to determine the component type.