Hibernate relationship Mapping

Source: Internet
Author: User

This article for Bo Master original, allow reprint, but please declare the original address: http://www.coselding.cn/blog/8/8-140.html

1. Relationship Mapping:
A One-to-one: Primary Key association: PRIMARY Key Association
One-way: foreign Key Association
Bidirectional: FOREIGN key association or intermediate table
Federated PRIMARY Key:
@OneToOne: One-to-one correlation, annotations on the Get method referenced by the other class
@JoinColumn: Set the associated foreign key name, Name property
@PrimaryKeyJoinColumn: Setting a primary Key association
@JoinColumns Setting the Federated primary key
XML using <many-to-one/> tags, add unique attributes to
B One-to-many: one party adds a set of multiparty sets
Annotations: @OneToMany and @joincolumn specify foreign key names for multiple parties
Xml:<set><one-to-many/></set>
C Many-to-one: in multi-party plus foreign key
NOTES: @ManyToOne
Xml:<many-to-one/>
Note: A one-to-many, multi-pair relationship, to the multi-party maintenance relationship, set in @onetomany Mappedby
property, XML in <set></set> set inverse= "false"
D Many-to-many:
Unidirectional:
Note: @manytomany on the Maintenance collection, @JoinTable Specify the intermediate table name and column name,
Joincolumns, Joincolumn, Inversejoincolumn
Xml:<set><many-to-many/></set>
Bidirectional:
Note: Set @manytomany (mappedby= "") on a one-way basis on the other side of the collection
XML: Also set <set><many-to-many/></set> label on the other side
E. Component mapping: A bean that is part of a table, not as an entity, such as a student ID as part of a student entity;
Component references in entities add @embedded
Using <component/> tags in XML
Note: (1) bidirectional correlation Mappedby attribute must be set, XML with <one-to-one/> property-ref attribute
Indicates that the relationship is dominated by the other party;
(2) Two-way relationship pay attention to the danger that the stack overflow of the entity infinite Loop call may occur during the call;
2, the relationship between the additions and deletions to change the search:
(1) Association relationship SET Cascade Cascade operation, only affect the deletion and modification;
(2) If the associated object is a party, the default is directly taken out, EAGER;
(3) If the associated object is a multi-party, the default is lazy loading, lazy;
(4) Fetch set query read cascade operation, only affect the query operation, default lazy;
(5) Two-way correlation relationship some operations can result in repetitive operations, note that setting up lazy loading avoids redundant operations;
(6) Delete: First query and then delete, prevent cascade Delete-Destroy the memory entity's Association relationship or HQL;
3. Set Mapping:
(1) General mode: Set;
(2) List: Add annotations and set, list is to be able to sort, @OrderBy specify sorting parameters;
(3) Map:key is a non-repeating sub-segment (most primary key), @MapKey; value is a collection element entity, which is more suitable for querying;
4. Inheritance mapping:
(1) A few attribute inheritance relationships: single-table, a table stores all classes, plus a type tag seat segment;
(2) Each class is a table: Table-per-class, can not store the inheritance polymorphism relationship, inherit polymorphic call trouble, need to maintain the uniqueness of each entity primary key;
(3) According to the inheritance relation, the parent class has the table, the subclass also has the table, but the common attribute in the parent table: Joined, the parent table and the child table to set the primary key Association, maintains the primary key uniqueness, the query must join the table, the new subclass constructs the new table;
Note: A. @Inheritance: Note The inheritance map, specify the inheritance mapping policy, for the parent class;
B. @DiscriminatorColumn: Indicates the type tag bit of the class for the parent class;
C. @DiscriminatorValue ("tag bit value"): Class type tag bit value;
5. Tree-like mapping:
(1) Object design: ID, Father node reference, child node collection, other node content;
(2) Table design: ID, parent_id, other node content column;
(3) Set @entity, @Id, primary key generation policy, @ManyToOne (set the Foreign key column "parent_id"), @OneToMany (mappedby= "parent");
(4) Set the modified cascade, lazy or eager specific analysis to choose;
(5) Set up the parent-child relationship between the objects, deposit the database;
(6) Query using recursive method;

This article for Bo Master original, allow reprint, but please declare the original address: http://www.coselding.cn/blog/8/8-140.html

Hibernate relationship Mapping

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.