1, @Entity (name= "EntityName")
must, name is optional, corresponds to one of the tables in the database
2, @Table (name= "", catalog= "", Schema= "")
Optional, usually used in conjunction with @entity, can only be labeled at the class definition of the entity, indicating the database table information for the entity
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,
Generated based on the identity field of the database, generated from the sequence field of the database table to generate a primary key based on an additional table, default to 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.
Example:
@Id
@GeneratedValues (strategy=strategytype.sequence)
public int getpk () {
return PK;
}
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.
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. Usually the ORM framework can automatically determine the type of a field in a database based on the type of the attribute, but for the date type 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, the entity order has a user attribute to associate the entity user, and the order's user property is a foreign key.
Its default name is the name of the entity user + underscore + 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, if the entity user and order are onetomany relationships, the entity user is deleted and its associated entity order should also be 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;
}
}
The attributes of the two entities that are related to each other must be marked as @manytomany and specify the Targetentity attribute to each other.
Note that @manytomany annotations with only one entity need to specify the Mappedby attribute, which points to the 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 for 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 validation Annotations
| annotations |
applicable type |
Description |
Example |
| @Pattern |
string |
to validate strings through regular expressions |
@attern (regex= "[A-z]{6}") |
| @Length |
string |
Verify the length of the string |
@length (min=3,max=20) |
| &nbs p; @Email |
String |
to verify that an Email address is valid |
@email |
| @Range |
L Ong |
To verify that an integral type is in a valid range |
@Range (min=0,max=100) |
| @Min |
Long |
Verify that an integral type must be no less than the specified value |
@Min (value=10) |
| @Max |
Long |
Verify that an integral type must be no greater than the specified value |
@Max (value=20) |
| @Size |
Collection or array |
whether the size of the collection or array is within the specified range |
@Size (min=1,max=255) |
Each of these annotations is likely to have a message property that is used to return messages to the user after validation fails, and that multiple annotations can be used on three properties
Reference: http://log-cd.iteye.com/blog/288909
Java, Hibernate (JPA) annotations Daquan