Hibernate annotation Configuration

Source: Internet
Author: User

In the first practical application of the framework, I used the Hibernate XML configuration method, the XML configuration method is very cumbersome, or recommend that all the people using hibernate to use annotations to configure, in this article, I will enumerate our common annotation configuration, Below I will be divided into the following points for elaboration.

First, class-level annotations

Second, attribute level annotations

The relation of correlation mapping

First, class-level annotations

A) @Entity

@Entity: Mapping Entity types

@Entity (name= ""): Specifies the table name that corresponds to it in the database

The primary key must exist when using @entity.

B) @Table (name= "", catalog= "", Schema= "")

Used in conjunction with @entity, the Name property represents the table name that corresponds to it in the database

Catalog Optional: Indicates that the catalog name defaults to ""

Schema Optional: Indicates that the schema name defaults to ""

C) @Embeddable

From an object-oriented perspective, it means that the class can be embedded in other classes.

From the perspective of a relational database, the attributes identified in the class are mapped into columns and then embedded in other tables.

Under normal use, the attributes identified by the annotation can be embedded in other classes as attributes, but there is a special case where the embedded class acts as a primary key, which is described in @embeddedid.

Second, attribute level annotations

A) @Id: Map to a database to act as a primary key, which can identify multiple properties at the same time and act as a federated primary key.

If used as a federated primary key, the class must implement the Serializable interface

String property must be limited to a string length, maximum length of 40 bits, or a primary key failure if you are a primary key

B) @GeneratedValue (strategy= "", generator= "")

Strategy can have four kinds of values, respectively:

Generationtype.auto: Automatic selection based on the underlying database

Generationtype.indentity: Generated based on the identity field of the database

Generationtype.sequence: Use SEQUENCE to determine the value of the primary key

Generationtype.table: Use the value of the specified voting key, combined with @TableGenerator

Example:

@Id

@TableGenerator (table= "Tab_cat_gen", allocationsize=1)

@GeneratedValue (strategy=generationtype.table)

C) @Column

Common Properties:

Name: The names of the columns that correspond to this property

Nullable: Whether the value of the column corresponding to this property can be null

Uinique: Whether the value of the column corresponding to this property is unique

Length: The upper bound of the field of the column corresponding to the property

Insertable: Indicates whether the field appears in the INSERT statement when a record is inserted in the ORM framework.

Updateable: Indicates whether the field appears in the INSERT statement when a record is updated in the ORM framework.

D) @Embedded and @EmbeddedId

The attribute that identifies the annotation represents the class of the property as an embedded class, and the embedded class must have a @Embeddable annotation

When the property is identified as a primary key, it must be identified as @embeddedid, and the following conditions are met:

1 Implementing the Serializable interface

2 The torture method for public with a public.

3 Implement the Equals () and Hashcode () methods.

E) @Transient:

The annotation property is not a field property that maps the inverted database table, and the ORM framework ignores the property.

Third, the relationship mapping annotations

A) One-way to foreign key association

There is a one-to-one relationship between both A and B

@OneToOne (Cascade=cascadetype.all)

@JoinColumn (name= "", Unique=true)

Where a party identifies above annotations, the name attribute represents the column name of the B-party with which it corresponds

If party B uses @column to reassign the column name, the name should point to the modified name

B) bidirectional one-to-one foreign key association

On the basis of a), add the following annotations to party B at the same time

@OneToOne (mappedby= "")

Mappedby attribute variable name pointing to a party

C) One-way many-to-one foreign key association

Set a-side to maintain a lot of relationships, B-party maintenance one of the relationship

A one-to-one relationship between a and B parties is required

@ManyToOne (Cascade={cascadetype.all}, Fetch=fetchtype.eager)

@JoinColumn (name= "")

The essence is the need to maintain a foreign key in a party

Only need to configure the above annotations in party A, the Fetch attribute represents the acquisition policy for that attribute, and as many parties need to be actively (eager) to obtain to conserve resources.

The name attribute in the @JoinColumn (name= "") represents the column name to be referenced, not the property name.

D) unidirectional one-to-many foreign key association

@OneToMany (Cascade={cascadetype.all}, Fetch=fetchtype.lazy)

@JoinColumn (name= "")

A one-to-many form of representation in a database is the same as a representation in a database, and it is a foreign key that needs to maintain a reference to a party in many parties, but the difference is that we need to configure annotations so that we can use one party's object to get a collection of one (many)

As a party, we need to use the Lazy loading technology (fetch=fetchtype.lazy), otherwise we will always have to maintain the collection, very lossy server resources.

E) bidirectional one-to-many foreign key association

Bidirectional one-to-many foreign key association = unidirectional one-to-many foreign key association + one-way multi-pair foreign Key Association

You just need to iterate over the two configurations.

F) One-way many-to-many foreign key associations

The first thing to note is that we used in the previous relationship, we have to directly use the corresponding two tables is enough for us to complete the maintenance of the relationship.

But in many-to-many relationships, we can't use two tables, but we need an intermediate table to maintain a many-to-many relationship between the two tables.

The focus of many-to-many relationships is in specifying the intermediate table.

Suppose there are many-to-many relationships between A and B:

You only need to identify the following annotations on either a or B:

@ManyToMany

@JoinTable (

Name= "",

joincolumns={@JoinColumn (name= "")},

inversejoincolumns={@JoinColumn (name= ""),

@JoinColumn (name= ""),

@JoinColumn (name= ""),

@JoinColumn (name= "")}

)

@JoinTable used to identify the intermediate table

Name indicates table name

The corresponding foreign key column names can be specified in Joincolumns and Inversejoincolumn, and if there is a federated primary key in A, B, you only need to specify multiple @joincolumn.

G) bidirectional Many-to-many foreign key association

Bidirectional many-to-many foreign key association only need on a one-way basis, in the B-side add a note (assuming a party has maintained a one-way many-to-many relationship):

@ManyToMany (mappedby= "")

Mappedby points to the variable attribute name in party A.

Hibernate annotation Configuration

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.