JPA annotation Annotations

Source: Internet
Author: User

Let's talk about the relationship between JPA and hibernate.

JPA (Java Persistence API), a standard ORM interface for Java EE 5, is also part of the EJB3 specification.

Hibernate is a popular ORM framework today, as well as a JPA implementation, and other ROM frameworks such as TopLink.

The relationship between JPA and hibernate can simply be understood as JPA is the standard interface, and Hibernate is implemented.
Hibernate is achieved primarily through three components:

    • The core implementation of Hibernate-core:hibernate provides all the core functions of hibernate.
    • Hibernate-entitymanager:hibernate implements the standard JPA, which can be seen as an adapter between Hibernate-core and JPA, which does not directly provide ORM functionality, but rather encapsulates the hibernate-core. Make hibernate conform to JPA specifications.
    • The Hibernate-annotation:hibernate supports the foundation of the annotation mode configuration, which includes the standard JPA annotation and the annotation of Hibernate's own special features.

The annotations are detailed as follows:

1 @Entity(name="EntityName")

must, name is optional, corresponds to one of the tables in the database

1 @Table(name="",catalog="",schema="")

Optional, usually used in conjunction with @Entity, can only be labeled at the class definition of the entity, representing the information of the database table 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 need to specify the table name only 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 ("").

@id
@id defines a property that maps to the primary key of a database table, and an entity can have only one attribute mapped to the primary key.

@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.

@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, which is true by default.

@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: Represents 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, which is true by default.
Unique: Indicates whether the field is a unique identity and defaults to false.
Length: Indicates the size of the field and is valid only 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 the Birthday field.
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 property, but it is still not possible to determine whether the field type in the database is Date,time or timestamp for the date type. 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 particular database.

@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. Be sure to mark it as @transient. Otherwise. The ORM framework defaults to its annotations as @basic

@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

@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 normally specified, and the ORM framework automatically determines targetentity based on the property type.

@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

@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
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

@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

@JoinTable (name = "Student_teacher", Inversejoincolumns = @JoinColumn (name = "Tid"), Joincolumns = @JoinColumn (name = "Sid ”))

Options available

The relationship between two tables is maintained by the third table

Name: Are the names of the relational tables

Joincolumns: The primary key at the end of yourself

Inversejoincolumns: The other's primary key

@MappedSuperclass
Options available
@MappedSuperclass can pass a superclass's JPA annotations to subclasses, enabling subclasses to inherit the JPA annotations of the superclass

@Embedded
@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

Validation annotations

@Pattern
String
Validating strings with regular expressions
@Pattern (regex= "[A-z]{6}")

@Length
String
Verify the length of the string
@length (MIN=3,MAX=20)

@Email
String
Verify that an email address is valid
@email

@Range
Long
Verify that an integral type is within 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)

Excerpt from: http://www.cnblogs.com/luoxiaolei/p/4272494.html

JPA annotation Annotations

Related Article

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.