HIbernate association mapping

Hibernate association mapping relationship between
tables and tables
In software development, the most common relationship between classes and classes is the association relationship, and the association is directional.
One-to-one: There must be a unique identifier One
-to-many : Add a foreign key to the multi-party to point to the primary key of one party Many-to
-many: Create a third table with at least two fields in the middle table, which are used as foreign keys to point to both sides of the many-to-many
Primary key.

Establishing a one-to-many bidirectional relationship
When a class is associated with a class, it is easy to navigate from one object to another, or
to navigate to a group of objects through a collection.
Configure one-to-many association
1. Create two entity classes respectively.
Hibernate requires that when defining a collection class attribute in a persistent class, the attribute must be declared as an interface
type . For example, java.util.Set can specify a generic java.util.Set<table Name>
Multi-party entity class needs to add a collection of entity classes of one party
2 Create the mapping relationship between entity class and table
separately Add foreign key to one party to point to the primary key of one party
Because there is no field directly corresponding to the set property in the multi-party table, you cannot use the <property>
element to To map collection properties, use the <set> element
<set name="cout">
    <key column="uc"/>
    <one-to-many class="entity.Cour"/>
</set>

The <set> element also contains two sub-elements:
1. <key> element: the column attribute sets the foreign key to the table of the associated persistent class object,
2. <ont-to-many> element: the class attribute sets with the associated persistence class.
Hibernate obtains the following information according to the above mapping code:
1. The collection attribute of the multi-class <set> element table name is java.util.Set collection type
2. The <one-to-many> sub-element table name collection stores a set of Collection object
3. The <key> sub-element indicates that one table refers to the multi-party table through foreign keys. 3. Add
the mapping relationship to the core configuration file . Cascades) 1.cascades attribute Elements used to map the association relationship between persistent classes, <set>, <many-to-one> and <one-to-one> all have a cascade attribute, which is used to specify how to operate and Other objects associated with the current object Soliton delete operation cascade="delete-orphan" When the cascade attribute of the <set> element of customer.hbm.xml is set to all-delete-orphan, Hibernate will process the customer object as follows: 1. When saving or updating a customer object, cascade saving or updating all associated order objects,   equivalent to save-update.











2. When deleting the customer object, delete all order objects in cascade, which is equivalent to delete.
3. Delete all order objects that are no longer associated with the customer object.
When there is a parent-child relationship on both sides of the association, the cascade attribute of the parent can be set to all-delete-orphan.


2. The inverse attribute of the <set> element The
term "inverse" is literally translated as "inversion". In Hibernate, the inverse attribute is formulated The direction in the association relationship is set
through the inverse attribute to set which side of the bidirectional association maintains the relationship between the table and the table. The inverse=false is the active side, and the inverse=true is the passive side, and the active side is responsible for maintaining the relationship.
Set inverse=true on the one side, and the relationship is managed by the collection side.
Configure inverse=”true”: configure at that end. Then that end gives up the right to maintain foreign keys.
In general, the one side gives up. To

establish a many-to-many relationship, an intermediate table
must be created, and the intermediate table consists of two The primary key of a multi-party table is composed
of collection attributes at both ends of the
two-way relationship. The intermediate table must be used in the bidirectional association. The key sub-element should be added to the
collection attribute to map the foreign key column, and the many-to-many sub- element
should be added to the collection element.
Both sides of the association need to specify the table name of the join table and the column name of the foreign key column. The value of the table element of the set of the two set elements must be specified and must be the same. The cascade of the many-to
-many relationship is the same as the one-to-many
one . , created entity classes for students and courses, and associated mapping files
(many-to-many, one party must voluntarily give up the right to maintain foreign keys, inverse="true")
2. Add the mapping file to the core configuration file 
3. Add two students and two courses to the test, and students choose
the relevant course Hibernate Retrieval method
Hibernate's retrieval method.doc
Select * from user where uname=? group by sex having age>30 order by age desc When Hibernate queries multiple objects, it immediately queries and
loads
the object associated with it. This query strategy
It is called immediate loading. Immediate loading has two major disadvantages:
1. The number of select statements is too large, which requires frequent access to the database, which will affect the query performance
. 2. When the application only needs to access multiple objects, but does not need to access one object , loading one -sided
  objects is completely more than an operation, and these more than one-sided objects waste a lot of space.
In order to solve the above problems, Hibernate provides a lazy loading strategy, which
is used to set the lazy attribute of the loading strategy. The optional values ​​of the lazy attribute in the
class- level <class> element are: true (lazy loading) and false (immediate loading)
<class> The default value of the lazy attribute in the element is true
. The optional values ​​of the lazy attribute in the <set> element are: true (lazy loading) and false (immediate loading) and extra (
enhanced lazy loading); <set the default value of the lazy attribute in the element is true
The optional values ​​of the lazy attribute in the <mant-to-one> element at the many-to-one association level are: proxy (lazy loading), no-proxy (no proxy
lazy and false (immediate loading); <many-to The default value of the lazy attribute in the -one> element is proxy

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326952722&siteId=291194637