JPA Association Annotation

Source: Internet
Author: User
Join annotation example table:

Customer table

Create Table customer {

Id int (20) not null auto_increment,

Name varchar (100 ),

Address_id int (20 ),

Primary Key (ID)

}

 

Address Table

 

 

 

 


Create Table address {

Id int (20) not null auto_increment,

City varchar (100 ),

Detail varchar (100 ),

Primary Key (ID)

}

 

 

 

 

 

 


 

Associate a specified column with @ joincolumn

The field that stores the relationship between the table and the table in the annotation table. It must be marked on the object.

Attribute:

Name indicates the name of the field corresponding to the table

Unique indicates whether the field is unique. The default value is false.

Nullable indicates whether the field can be null

Insertable indicates whether this field needs to be inserted when data is inserted using the insert script.

Updatable indicates whether to update this field when updating the update script.

Columndefinition indicates the SQL statement created by this field when the table is created. It is generally used to generate table definitions using entity.

Table indicates the fields in the ing table of the ing table. The name of the base table by default.

Referencedcoumnname indicates the field name in the joined table. The default value is the gradually field name of the associated table.

One-to-multiple @ onetoone

Attribute:

Targetentity indicates the object type associated with the default value. The default value is the object class currently labeled. Therefore, this knowledge can be omitted.

Cascade indicates the baseline style type of the object associated with this object. No operation is performed by default.

Fetch indicates the Loading Method of the object. The default value is immediate loading of the eager, or delayed loading of lazy.

Optional indicates whether the correlated entity can have a null value. The default value is true. If the value is false, the @ joincolumn annotation must be used together.

Yby two-way Association attribute, marked in the entity that does not save the link

 

Example:

 

@ Entity <br/> @ table (name = "customer") <br/> Public Class Customer implements Java. io. servializable {<br/> @ ID <br/> @ generatevalue (Strategy = generationtype. auto) <br/> private integer ID; <br/> @ onetoone (cascade = {cascadetype. all}) <br/> @ joincolumn (name = "address_id") <br/> private address; <br/> .... <br/>}< br/> @ entity <br/> @ table (name = "Address") <br/> Public Class address implements Java. io. servializable {<br/> @ ID <br/> @ generatevalue (Strategy = generationtype. auto) <br/> private integer ID; <br/>... <br/>} 

Because the address_id field of the customer is associated with the primary key ID of the address table, you do not need to specify referencedcolumnname = "ID" in the joincolumn annotation of the customer object"

 

Bidirectional Association

The one-to-one association is only one-way association, that is, the address object can only be obtained through the customer object, but not through the address object to get the customer object.

To obtain a customer object through an address object, you must add a reference to the customer object in the address object class.

Add a customer object to address.

@ Onetoone (mappedby = "Address") <br/> private customer; 

 

One-to-multiple @ onetoworkflow

The attribute of @ onetoone is the same as that of @ onetoone.

Note that one-to-Multiple object sets are stored in the Collection class.

You can either specify the set type, for example, private list <address> addresses.

Either specify the targeteneity attribute

 

Many-to-one @ manytoone

@ Manytoone has the same attribute as @ onetoone.

 

Example:

Add the customer_id field to the address table for example. Remove the address_id field from the customer table.

 

Unidirectional Association:

@ Entity <br/> @ table (name = "customer") <br/> Public Class Customer implements Java. io. serializable {<br/>... <br/> @ onetoetype (cascade = {cascadetype. all}) <br/> private list <address> addresses; <br/>}< br/> @ entity <br/> @ table (name = "Address ") <br/> Public Class address implements Java. io. serializable {<br/>... <br/> @ manytoone (cascade = {cascadetype. all}) <br/> @ joincolumn (name = "customer_id") <br/> private customer; <br/>} 

 

Bidirectional Association:

@ Entity <br/> @ table (name = "customer") <br/> Public Class Customer implements Java. io. serializable {<br/>... <br/> @ onetoses (mappedby = "customer") <br/> private list <address> addresses; <br/>}< br/> @ entity <br/> @ table (name = "Address") <br/> Public Class address implements Java. io. serializable {<br/>... <br/> @ manytoone (cascade = {cascadetype. all}) <br/> @ joincolumn (name = "customer_id") <br/> private customer; <br/>} 

Use of mappedby

If it is a bidirectional Association, the @ joincolumn annotation must be used in the address object that saves the object relationship. In the object that does not save the object relationship, the edby attribute must be used to specify the object to be associated.

If it is a one-way Association, the @ joincolumn annotation should be used in the entity that saves the object link; it should also be used in the entity that does not save the object link.

 

Many-to-many @ manytomany

The many-to-many ing policy is table join @ jointable.

@ Jointable

Attribute:

Name is used to connect the table names of two tables. If this parameter is not specified, it is the rule name of "table name 1" + "_" + "table name 2 ".

Catablog and schema indicate the directory name or database name specified by the object, which varies depending on different database types.

Joincolumns indicates the external field of the saved Association in the table in the saved link. It is used with @ joincolumn.

Inversejoincolumns is the same as joincolumns, indicating another external field

Uniqueconstraints indicates the unique constraints associated with the object. One object can have multiple unique constraints. By default, there are no constraints.

 

Example

For example, remove the address_id field from the customer table in the initial table structure and add the customer_address field of the relational table.


Create Table customer_address {

Customer_id int (20 ),

Address_id int (20)

}

 

Unidirectional Association:

@ Entity <br/> @ table (name = "customer") <br/> Public Class Customer implements Java. io. serializable {<br/>... <br/> @ manytoyun <br/> @ jointable (name = "customer_address", <br/> joincolumn (name = "customer_id", referencedcolumnname = "ID "), <br/> inversejoincolumns ={@ joinolumn (name = "address_id", referencedcolumnname = "ID")}) <br/> private list <address> addresses; <br/>}< br/> 

Bidirectional:

@ Entity <br/> @ table (name = "customer") <br/> Public Class Customer implements Java. io. serializable {<br/>... <br/> @ manytoyun <br/> @ jointable (name = "customer_address", <br/> joincolumn (name = "customer_id", referencedcolumnname = "ID "), <br/> inversejoincolumns ={@ joinolumn (name = "address_id", referencedcolumnname = "ID")}) <br/> private list <address> addresses; <br/>}< br/> @ entity <br/> @ table (name = "Address") <br/> Public Class address implements Java. io. serializable {<br/>... <br/> @ manytomers (mappedby = "Address") <br/> private list <customer> MERs MERS; <br/>} 

 


 

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.