1. @ Entity (name = "EntityName ")
Required. The name parameter is optional and corresponds to a table in the database.
2. @ Table (name = "", catalog = "", schema = "")
Optional. It is usually used with @ Entity. It can only be labeled in the class definition of an object, indicating the information of the database table corresponding to the object.
Name: (optional) name of the table. By default, the table name and object name are consistent. You must specify the table name catalog only when they are inconsistent: optional, indicating the Catalog name. The default value is Catalog ("") schema. Optional. It indicates the Schema name. The default value is Schema ("")
3. @ id
Required
@ Id defines the attribute mapped to the primary key of the database table. Only one attribute of an object can be mapped to the primary key. Placed before getXxxx ()
4. @ GeneratedValue (strategy = GenerationType, generator = "")
Optional
Strategy: indicates the primary key generation policy, which can be AUTO, INDENTITY, SEQUENCE, and TABLE, the primary keys are automatically selected by the ORM framework, generated based on the database Identity field, generated based on the database table Sequence field, and generated based on an additional table. The default value is AUTO.
Generator: name of the primary key generator. This attribute is usually related to the ORM framework. For example, Hibernate can specify the uuid and other primary key generation methods.
Example:
@ Id
@ GeneratedValues (strategy = StrategyType. SEQUENCE)
Public int getPk (){
Return pk;
}
5. @ Basic (fetch = FetchType, optional = true)
Optional
@ Basic indicates a simple ing of attributes to fields in the database table. For getXxxx () methods without any annotations. The default value is @ Basic.
Fetch: indicates the read policy for this attribute. There are two types: EAGER and LAZY, which respectively indicate the master fetch and delayed loading. The default value is EAGERoptional, which indicates whether the attribute can be null. The default value is true.
Example:
@ Basic (optional = false)
Public String getAddress (){
Return address;
}
6. @ Column
Optional
@ Column describes the detailed definition of this field in the database table, which is very useful for tools that generate the database table structure based on JPA annotations.
Name: name of the field in the database table. By default, the attribute names are the same as nullable, indicating whether the field can be null. Trueunique by default: indicates whether the field is a unique identifier. The default value is falselength, which indicates the size of the field. It is only valid for String fields. insertable: indicates whether the field should appear in the INSETRT statement when the ORM framework performs the insert operation. Trueupdateable: indicates whether the field should appear in the UPDATE statement when the ORM framework performs the UPDATE operation. The default value is true. This attribute is useful for fields that cannot be changed once created. For example, columnDefinition of the birthday field indicates the actual type of the field in the database. Generally, the ORM framework can automatically determine the type of fields in the database based on the attribute type, but the Date type still cannot determine whether the field type in the database is DATE, TIME or TIMESTAMP. In addition, the default ing type of String is VARCHAR. This attribute is useful if you want to map the String type to the BLOB or TEXT field type of a specific database.
Example:
@ Column (name = "BIRTH", nullable = "false", columnDefinition = "DATE ")
Public String getBithday (){
Return birthday;
}
7. @ Transient
Optional
@ Transient indicates that this attribute is not a ing of fields in the database table. The ORM framework ignores this attribute.
If an attribute is not a field ing of a database table, you must mark it as @ Transient. Otherwise, the ORM framework uses its annotation as @ Basic by default.
Example:
// Calculate the age attribute based on birth
@ Transient
Public int getAge (){
Return getYear (new Date ()-getYear (birth );
}
8. @ ManyToOne (fetch = FetchType, cascade = CascadeType)
Optional
@ ManyToOne indicates a multi-to-one ing. The annotation attribute is usually the foreign key of the database table.
Optional: whether to allow this field to be null. This attribute should be determined based on the foreign key constraints of the database table. The default value is truefetch, which indicates the crawling policy. The default value is FetchType. EAGERcascade: indicates the default cascade operation policy. It can be specified as a combination of ALL, PERSIST, MERGE, REFRESH, and REMOVE. By default, it is a stepless Join Operation targetEntity: indicates the object type associated with this attribute. This attribute does not need to be specified. The ORM framework automatically determines targetEntity Based on the attribute type.
Example:
// The Order and User are in a ManyToOne relationship.
// Defined in the Order class
@ ManyToOne ()
@ JoinColumn (name = "USER ")
Public User getUser (){
Return user;
}
9. @ JoinColumn
Optional
@ JoinColumn is similar to @ Column. The description is not a simple field, but an associated field. For example, it describes a field of @ ManyToOne.
Name: name of the field. Because @ JoinColumn describes an associated field, for example, ManyToOne, the default name is determined by the associated object.
For example, if the object Order has a user attribute to associate with the object User, the Order user attribute is a foreign key.
The default name is the Object User Name + underline + the primary key name of the Object User
Example:
See @ ManyToOne
10. @ onetoetype (fetch = FetchType, cascade = CascadeType)
Optional
@ Onetoworkflow describes a one-to-many association. This attribute should be of the collective type and has no actual fields in the database.
Fetch: indicates the capture policy. The default value is FetchType. LAZY, because the associated objects do not need to be read from the database in advance to the memory cascade: indicates the cascade operation policy, which is very important for the onetoworkflow type association. Generally, when the object is updated or deleted, its associated entities should also be updated or deleted.
For example, if the relationship between the Object User and Order is onetoworkflow, the object Order associated with the object User should also be deleted when the object User is deleted.
Example:
@ Onetyade (cascade = ALL)
Public List getOrders (){
Return orders;
}
11. @ OneToOne (fetch = FetchType, cascade = CascadeType)
Optional
@ OneToOne describes a one-to-one association.
Fetch: indicates the capture policy. The default value is FetchType. LAZY.
Cascade: indicates the cascade operation policy.
Example:
@ OneToOne (fetch = FetchType. LAZY)
Public Blog getBlog (){
Return blog;
}
12. @ manytoyun
Optional
@ ManyToMany describe a multi-to-many Association. There are two one-to-many associations on the Multi-to-many Association. However, in the description of ManyToMany, the intermediate table is automatically processed by the ORM framework.
TargetEntity: The full name of another object class associated with multiple-to-multiple pairs, for example, package. Book. class.
MappedBy: name of the corresponding set attribute of another object class associated with multiple to multiple objects.
13. @ MappedSuperclass
Optional
@ MappedSuperclass: the JPA annotation of the superclass can be passed to the subclass so that the subclass can inherit the JPA annotation of the superclass.
Example:
@ MappedSuperclass
Public class Employee (){
....
}
@ Entity
Public class Engineer extends Employee {
.....
}
@ Entity
Public class Manager extends Employee {
.....
}
14. @ Embedded
Optional
@ Embedded combines several fields into a class and serves as an attribute of the entire Entity
For example, a User includes the id, name, city, street, and zip attributes.
We want the city, street, and zip attributes to be mapped to the Address object. In this way, the User object has three attributes: id, name, and address.
The Address object must be defined as @ Embededable.
Example:
@ Embeddable
Public class Address {city, street, zip}
@ Entity
Public class User {
@ Embedded
Public Address getAddress (){
..........
}
}