Common annotation of JPA

Source: Internet
Author: User

Http://www.blogjava.net/zJun/archive/2007/01/24/95747.html

1,

@ Entity: Use the @ entity annotation to declare a class as an entity bean.

@ Table (name = "t_husband "): The @ table annotation can be used to specify a table for object bean ing. The name attribute indicates the name of the table corresponding to the object. If @ table is not defined, the system automatically uses the default value: object Class Name (without package name)

2,

@ ID: Primary key used to mark an attribute

@ Generatedvalue(Default value: Anto, Id Generation Policy)

3,

@ Column (name = "wifeid "): Indicates the fields in the table mapped to the persistent attribute. If the attribute name is the same as the field name in the table, it can be omitted.

4,

Joint primary key: When entity class uses a composite primary key, a class must be defined as the ID class. The ID class must comply with the following requirements: the class must be declared as public and provide an empty constructor declared as public. Serializable must be implemented to override the equals () and hashcode () methods. All ID fields of entity class must be defined in ID class and have the same type.

On the class name@ [Email protected] (teacherpk. Class)

On the two attributes of the Union primary key@ [Email protected]

Public class employeepk implements java. Io. serializable {
String empname;
Integer empage;
Publicemployeepk (){}
Publicboolean equals (Object OBJ ){......}
Public int hashcode (){......}
}

@ Idclass (employeepk. Class)
@ Entity
Public class employee {
@ ID string empname;
@ ID integer empage;
}

5,

Associating:

@ Onetoone

@ Onetoworkflow

@ Manytoone

@ Manytoyun

6,

@ Joincolumn

If the relation (one2one or one2second) is defined on the field of the entity class, we use joincolumn to define the attribute of the link.

Name: column name.

Referencedcolumnname: name of the column to which this column points (when a table is created, this column points as a foreign key column to a specified column at the other end of the Link)

Unique: unique or not

Nullable: whether to allow null

Insertable: whether insertion is allowed

Updatable: whether update is allowed

Columndefinition: defines the DDL for creating this column when creating a table

Secondarytable: name of the slave table. If this column is not created on the primary table (it is created on the primary table by default), this attribute defines the name of the slave table where the column is located.

The following code shows that husband have a one-to-one relationship with wife. In the ing table corresponding to husband, create a column named wifeid, which serves as the foreign key pointing to the primary key ID of the table corresponding to wife.

Public class husband {

Privatewife wife;

@ Onetoone

@ Joincolumn (name = "wifeid ")

Publicwife getwife (){

Returnwife;

}

7,

@ Joincolumns

If a relation (such as one2one or one2second) is defined on the field of the entity class and there are multiple joincolumns In the relation, use joincolumns to define attributes of multiple joincolumns.

Metadata attribute description:

Value: defines the joincolumn array and specifies the attributes of each joincolumn.

The following code shows that husband have a one-to-one relationship with wife. Create two columns in the ing table corresponding to husband, one is the wifeid column, which serves as the foreign key pointing to the primary key ID in the corresponding table of wife; the other is the wifename column, this column points to the name of the primary key in the table corresponding to the wife.

Public class husband {

Privatewife wife;

@ Onetoone

@ Joincolumns ({

@ Joincolumn (name = "wifeid", referencedcolumnname = "ID "),

@ Joincolumn (name = "wifename", referencedcolumnname = "name ")

})

Publicwife getwife (){

Returnwife;

}

8,

@ Jointable

Jointable is defined on the owner side of the sequence-to-sequence relationship. If no jointable is defined, use the default value of jointable.

Metadata attribute description:

  • Name: name of the join table.
  • Joincolumns: defines the foreign key column pointing to the owner's main table. The data type is the joincolumn array.
  • Inversejoincolumns: defines a foreign key column pointing to a non-owner master table. The data type is the joincolumn array.

The following teacher Class Code defines a join table for the join tables teacher and student. The table name of join table is teacher_student, which contains two foreign keys. One foreign key is t_id, which points to the primary key ID of the table teacher. The other foreign key is s_id, which points to the primary key ID of the table student. Referencedcolumnname can be omitted.

Public class teacher {

Private set <student> students = new hashser <student> ();

@ Manytoyun

@ Jointable (

Name = "teacher_student ",

[Email protected] (Name = "t_id", referencedcolumnname = "ID "),

[Email protected] (Name = "s_id", referencedcolumnname = "ID ")

)

Public set <student> getstudents (){

Return students;

}

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.