Hibernate Annotation
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 only when they are inconsistent.
Catalog: (optional) specifies 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.
Before.
4. @ generatedvalue (Strategy = generationtype, generator = "")
Optional
Strategy: indicates the primary key generation policy, which has four types: auto, indentity, sequence, and table.
Framework automatically selected,
Generated Based on the database identity field and the database table sequence field to generate an additional table
Primary Key. 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
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 the ing of a simple attribute to fields in the database table. For the getxxxx () method without any annotation, the default value is
That is, @ basic
Fetch: indicates the read policy for this attribute. There are two types: eager and lazy, which respectively indicate master branch crawling and delayed loading. The default value is
Eager.
Optional: 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 according to the JPA annotation.
It works.
Name: indicates the name of the field in the database table. By default, the attribute names are consistent.
Nullable: indicates whether the field can be null. The default value is true.
Unique: indicates whether the field is a unique identifier. The default value is false.
Length: 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. The default value is true.
Updateable: 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, the birthday field.
Columndefinition: indicates the actual type of the field in the database. Generally, the ORM framework can automatically determine the type of the field based on the attribute type.
The field type in the database is disconnected, but the date type still cannot be determined whether the field type in the database is date, time or
Timestamp. In addition, the default ing type of string is varchar. If you want to map the string type to
Blob or text field type. This attribute is very useful.
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 Note by default.
@ Basic
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 the field to be null. This attribute should be determined based on the foreign key constraints of the database table. The default value is true.
Fetch: indicates the capture policy. The default value is fetchtype. Eager.
Cascade: indicates the default cascade operation policy, which can be specified as all, persist, merge, refresh, or remove
Dry combination. The default value is stepless join operation.
Targetentity: indicates the object type associated with this attribute. This attribute does not need to be specified, and the orm framework automatically determines Based on the attribute type.
Targetentity is broken.
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 and @ column are similar. The description is not a simple field, but an associated field, for example, describing
@ Manytoone field.
Name: name of the field. Because @ joincolumn describes an associated field, for example, manytoone, the default name is
Its associated entities are determined.
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 multiple associated objects do not need to be read from the database in advance.
To memory
Cascade: indicates a cascade operation policy. It is very important for an onetoworkflow Association. Generally, when the object is updated or deleted
The 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.
Deleted all
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 many-to-many Association. There are two one-to-many associations on the many-to-many Association, but in the manytomany description
The middle 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.
Example:
The user entity represents the user, and the book entity represents the book. To describe the user's favorite books, you can
Create a manytomany Association
@ Entity
Public class user {
Private list books;
@ Manytoty (targetentity = package. Book. Class)
Public list getbooks (){
Return books;
}
Public void setbooks (list books ){
This. Books = books;
}
}
@ Entity
Public class book {
Private List users;
@ Manytoty (targetentity = package. Users. Class, mappedby = "books ")
Public list getusers (){
Return users;
}
Public void setusers (List users ){
This. Users = users;
}
}
The attributes associated with the two entities must be marked as @ manytoty and the targetentity attribute must be specified for each other,
Note that the @ manytomany Annotation with only one object needs to specify the mappedby attribute, pointing
Set attribute name of targetentity
In addition to the user and book tables, the table automatically generated by the ORM tool also generates a user_book table for multiple
Multi-join
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 will have the 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 (){
..........
}
}