The commonly used hibernate annotation tags are as follows:
@Entity--The comment declares that the class is a persistent class. It is best to serialize a JavaBean class as a database table mapping class for an entity. At this point, all class properties are mapped to the persisted field in the data table by default. If you add additional properties to the class, instead of mapping to the database, use the following transient to annotate.
@Table (name= "Promotion_info")--Persistent Mapping table (table name = "Promotion_info) [email protected] is a class-level annotation, defined under @entity, for the Entity Bean Mapping table, The name of the directory and schema, default to the class name of the entity Bean, without the package name.
@Id--a comment can indicate which property is a unique identifier in the class (that is, the primary key of the data table).
@GeneratedValue-Defines the generation strategy for the auto-growing primary key.
@Transient-These fields and properties are ignored and not persisted to the database. Applicable to, in the current persistence class, some properties are not used to map to the data table, but to other business logic needs, at this time, These attributes must be transient. Otherwise, the system will fail to map the corresponding fields of the data table.
@Temporal (Temporaltype.timestamp)--Declaration time format
@Enumerated--Declaring enumerations
@Version-declaration adds support for optimistic locking
@OneToOne-a one-to-one association between entity beans can be established
@OneToMany-a one-to-many association between entity beans can be established
@ManyToOne-You can establish a many-to-one association between entity beans
@ManyToMany-a many-to-many association between entity beans can be established
@Formula--An SQL expression that is read-only and not in the database generation properties (you can use SUM, Average, max, and so on)
@OrderBy--many end a field sort (List) Hibernate annotation Details
Hibernate annotations Detailed description
1, @Entity (name= "EntityName")
Have to
Name is optional and corresponds to one of the tables in the database
2, @Table (name= "", catalog= "", Schema= "")
Options available
Usually used in conjunction with @entity, can only be labeled at the entity's class definition, representing the entity's database table information
Name: optional, which represents the name of the table. By default, the table name and entity name are the same, and you only need to specify the table name in case of inconsistency
Catalog: Optional, which represents the catalog name, which defaults to catalog ("").
Schema: Optional, representing the schema name, which defaults to schema ("").
3. @id
Have to
@id defines a property that maps to the primary key of a database table, an entity can have only one attribute mapped to the primary key. Before Getxxxx ().
4, @GeneratedValue (strategy=generationtype,generator= "")
Options available
Strategy: Represents the primary key generation strategy, there are auto,indentity,sequence and TABLE 4, respectively, to let the ORM framework automatically select, based on the identity field of the database generation, based on the database table SEQUENCE field generation, To have a primary key generated based on an additional table, the default is auto
Generator: Represents the name of the primary key generator, which is usually related to the ORM framework, for example, hibernate can specify how the primary key is generated, such as the UUID.
Generator = "UUID": a 16 binary value is generated by hibernate based on a 128-bit unique value generation algorithm (encoded with a string of length 32) 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)
Options available
@Basic represents a simple property mapping of a field to a database table, and for a getxxxx () method that does not have any callouts, the default is @Basic
Fetch: Represents the read policy for this property, there are eager and lazy two, respectively, the main support crawl and lazy load, the default is eager.
(1), Fetchtype.lazy: Lazy loading, when loading an entity, the properties that define lazy loading are not immediately loaded from the database. (2), Fetchtype.eager: Urgent load, when loading an entity, the properties that define the urgent load are immediately loaded from the database. (3), for example, the user class has two attributes, name and address, as Baidu knows, login after the user name is required to display, this attribute is used to the odds, to immediately to the database to check, with the urgent load; and the user address in most cases do not need to show out, Only in view of user information is needed to display, need to use to check the database, with lazy loading just fine. Therefore, not a login to the user's all the data loaded into the object, so there are two load modes. Optional: Indicates whether the property is allowed to be null, default is True
Example:
@Basic (Optional=false)
Public String getaddress () {return address;}
6. @Column
Options available
@Column describes a detailed definition of the field in a database table, which is useful for tools that generate database table structures based on JPA annotations.
Name: Indicates the names of the fields in the database table, and the default case property names are the same
Nullable: Indicates whether the field is allowed to be null, default is True
Unique: Indicates whether the field is a unique identity and defaults to False
Length: Indicates the size of the field, only valid for fields of type string
Insertable: Indicates whether the field should appear in the INSETRT statement when the ORM Framework performs an insert operation, which is true by default
Updateable: Indicates whether the field should appear in the UPDATE statement when the ORM framework performs an update operation, which is true by default. This property is useful for fields that cannot be changed once created, such as for birthday fields.
ColumnDefinition: Represents the actual type of the field in the database. Typically ORM frameworks can automatically determine the type of fields in a database based on their property type, but for date types it is still not possible to determine whether the field type in the database is Date,time or TIMESTAMP. In addition, the default mapping type for string is varchar, which is useful if you want to map a string type to a blob or text field type of a specific database.
Example:
@Column (name= "BIRTH", nullable= "false", columndefinition= "DATE")
Public String Getbithday () {return birthday;}
7. @Transient
Options available
@Transient indicates that the property is not a mapping to a field in a database table, the ORM framework ignores the property. If a property is not a field mapping for a database table, it must be marked as @Transient, otherwise the ORM framework defaults to its annotation as @Basic
Example://Calculates the age property based on birth
@Transient
public int getage () {return getYear (new Date ())-getYear (birth);}
8. @ManyToOne (Fetch=fetchtype,cascade=cascadetype)
Options available
@ManyToOne represents a many-to-one mapping, which is typically a foreign key to a database table
Optional: If the field is allowed to be null, the property should be determined by the foreign KEY constraint of the database table, which is true by default
Fetch: Indicates a crawl policy, default is Fetchtype.eager
Cascade: Represents the default cascading action policy, which can be specified as several combinations in All,persist,merge,refresh and remove, with no cascading action by default
Targetentity: Represents the entity type that the attribute is associated with. This property is not usually specified, and the ORM framework automatically determines targetentity based on the property type.
Example:
Order orders and user users are a manytoone relationship
defined in the Order class
@ManyToOne ()
@JoinColumn (name= "USER")
Public User GetUser () {return user;}
9. @JoinColumn
Options available
@JoinColumn similar to @column, a mediator describes not a simple field, but one by one associated fields, such as. Describes a @ManyToOne field.
Name: The names of the fields. Because @joincolumn describes an associated field, such as Manytoone, the default name is determined by its associated entity. For example, an entity order has a user property that associates an entity user, the order's user property is a foreign key whose default name is the entity user's name + underscore + The entity user's primary Key name example: See @manytoone
10. @OneToMany (Fetch=fetchtype,cascade=cascadetype)
Options available
@OneToMany describes a one-to-many association, which should be a collective type, with no actual fields in the database.
Fetch: Represents a crawl policy, which defaults to Fetchtype.lazy, because multiple objects associated often do not have to be pre-read from the database to memory
Cascade: Represents a cascading action policy that is important for an association of onetomany types, usually when the entity is updated or deleted, its associated entities should also be updated or deleted for example: entity user and Order are onetomany relationships, when entity user is deleted, Its associated entity order should also be completely deleted
Example:
@OneTyMany (Cascade=all)
Public List getorders () {return orders;}
11. @OneToOne (Fetch=fetchtype,cascade=cascadetype)
Options available
@OneToOne describe a one-to-one association
Fetch: Indicates a crawl policy, default is Fetchtype.lazy
Cascade: Indicates cascading action policies
Example:
@OneToOne (Fetch=fetchtype.lazy)
Public blog Getblog () {return blog;}
12. @ManyToMany
Options available
@ManyToMany describes a many-to-many association. Many-to-many associations are two one-to-many associations, but in the manytomany description, the intermediate table is automatically processed by the ORM framework
Targetentity: The full name of another entity class that represents a many-to-many association, for example: package. Book.class
Mappedby: The corresponding collection property name of another entity class that represents a many-to-many association
Example:
User entities represent users, book entities represent books, and in order to describe user-collected books, a Manytomany association can be established between user and book.
@Entity
public class User {
Private List Books;
@ManyToMany (targetentity=package. Book.class)
Public List Getbooks () {return books;}
public void Setbooks (List books) {this.books=books;}
}
@Entity
public class Book {
Private List users;
@ManyToMany (targetentity=package. Users.class, mappedby= "books")
Public List Getusers () {return users;}
public void Setusers (List users) {this.users=users;}
}
Two entities that are related to each other must be marked as @manytomany and specify the Targetentity attribute to each other, and it is important to note that @manytomany annotations with only one entity need to specify the Mappedby attribute, pointing to Targetentity Collection property name automatically generated tables with ORM tools in addition to the user and book tables, a User_book table is automatically generated to implement many-to-many associations
13. @MappedSuperclass
Options available
@MappedSuperclass can pass a superclass's JPA annotations to subclasses, enabling subclasses to inherit the JPA annotations of the superclass
Example:
@MappedSuperclass
public class Employee () {...}
@Entity
public class Engineer extends Employee {...}
@Entity public class Manager extends Employee {...}
14. @Embedded
Options available
@Embedded combine several fields into a single class and act as a property of the entire entity. For example, user includes the Id,name,city,street,zip property. We want the City,street,zip property to be mapped to the Address object. This way, the user object will have the three properties of Id,name and address. Address object must be defined as @embededable
Example:
@Embeddable
public class Address {City,street,zip}
@Entity public class User {@Embedded public Address getaddress () {...}}
Hibernate automatically generates entity class annotations (GO)