Hibernate automatically generates object class annotations

Source: Internet
Author: User
Tags class manager

Common hibernate annotation labels are as follows:

@ Entity-- Annotation declares that the class is a persistent class. Declare a JavaBean class as a database table ing Class of an object, so it is best to implement serialization. by default, all class attributes are persistent fields mapped to the data table. if another attribute is added to the class instead of mapped to the database, the following transient is used for annotation.

@ Table(Name = "promotion_info") -- Persistence ing table (table name = "promotion_info ). @ table is a Class-level annotation. It is defined under @ entity as the object bean ing table, the directory and schema name. By default, it is the class name of the object bean without the package name.

@ ID-- Annotation can indicate which attribute is the unique identifier in the class (that is, equivalent to the primary key of the data table ).
@ Generatedvalue-- Defines the automatic growth primary key generation policy.

@ Transient-- These fields and attributes are ignored and do not need to be persisted to the database. yes. In the current persistent class, some attributes are not used to map to a data table, but to other business logic needs. In this case, these attributes must be annotated with transient. otherwise, the system will fail to map the corresponding fields of the data table.

@ Temporal(Temporaltype. timestamp) -- declare the time format
@ Enumerated-- Declare Enumeration
@ Version-- Declare support for Optimistic Locking
@ Onetoone-- One-to-one association between object beans can be established.
@ Onetoworkflow-- One-to-multiple associations between object beans can be established.
@ Manytoone-- Multiple-to-one association between object beans can be established.
@ Manytoyun-- Multiple-to-multiple associations between object beans can be established.
@ Formula-- An SQL expression is read-only and does not generate a database attribute (sum, average, and Max can be used)
@ Orderby-- Sort a field sorting (list) on the worker side hibernate Annotation

Hibernate annotations

1. @ entity (name = "entityname ")
Required
Name is optional, corresponding to a table in the database

2. @ table (name = "", catalog = "", schema = "")
Optional
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. It is placed before getxxxx.

4. @ generatedvalue (Strategy = generationtype, generator = "")
Optional
Strategy: indicates the primary key generation policy. There are four types: auto, indentity, sequence, and table. They indicate that the ORM framework is automatically selected and generated based on the identity field of the database, generated Based on the sequence field of the database table. A primary key is generated based on an additional table. The default value is auto.
Generator: indicates the 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.

Generator = "UUID": The algorithm generated by hibernate Based on the 128-bit unique value generates a hexadecimal value (encoded with a 32-Bit String) as the primary key.

Example:
@ ID
@ Generatedvalues (Strategy = strategytype. Sequence)
Public int getpk () {return PK ;}

@ ID
@ Generatedvalue (Strategy = generationtype. sequence, generator = "s_teacher") @ sequencegenerator (name = "s_teacher", allocationsize = 1, initialvalue = 1, sequencename = "s_teacher ")
 
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 @ 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.

(1) fetchtype. Lazy: lazy loading. when an object is loaded, attributes defining lazy loading will not be loaded from the database immediately. (2) fetchtype. Eager: when an object is loaded, attributes that are defined to be loaded urgently are loaded immediately from the database. (3) For example, the user class has two attributes: Name and address. As Baidu knows, the user name must be displayed after logon. This attribute has a high chance of use, you need to check the database immediately and load it in a hurry. In most cases, the user address does not need to be displayed. You only need to view the user information and check the database if necessary, just load it with laziness. Therefore, instead of loading all the user data into the object as soon as you log on, you can use these two loading modes. 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 based on JPA annotations.
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 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, by default, the orm framework returns @ 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 a combination of all, persist, merge, refresh, and remove. The default cascade operation is
Targetentity: indicates the object type associated with this attribute. This attribute is not required. The ORM framework automatically determines the 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 and @ column are similar. The description is not a simple field, but an associated field, for example, describing a field of @ manytoone.
Name: name of the field. because @ joincolumn describes an associated field, such as 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 entity user name + underline + entity user's primary key name 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 the memory.
Cascade: indicates a cascade operation policy. It is very important for an onetoworkflow Association. Generally, when an object is updated or deleted, its associated entities should also be updated or deleted. For example: the relationship between the Object User and order is onetoworkflow. When the Object User is deleted, its associated object order should also be 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 many-to-many Association. There are two one-to-many associations on the many-to-many Association. However, in the manytomany description, 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.

Example:
The user entity represents the user, and the book entity represents the book. To describe the user's favorite books, you can establish a manytomany association between the user and the book.
@ 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 ;}
}

Attributes associated with two entities must be marked as @ manytoty and targetentity attributes must be specified. Note that there is only one object's @ manytoty annotation that must specify the mappedby attribute, the set attribute name pointing to targetentity. In addition to the user and book tables, the table automatically generated by the ORM tool also generates a user_book table for multi-to-Multi Association.

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 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 (){..........}}

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.