Annotation of Hibernate

Source: Internet
Author: User

When the project becomes relatively large, how to use the hbm. xml file to configure Hibernate entities becomes more complex. Here, Hibernate provides the Annotation method, which makes the Hibernate ing file easy to manage.

Here is a brief introduction to the Annotation of Hibernate.

@ Entity

Annotate objects. This annotation is required for any Hibernate ing object.

@ Table

Declare the data table mapped to the database. You can specify the table (talbe), directory (Catalog), and schema names for the object. This annotation is not required. If not, the system uses the default value (short class name of the object ).

@ Version

This annotation can be used to add optimistic lock support to the Entity Bean.

 

@ Id

Declare this attribute as the primary key. This attribute value can be created by itself, but it is recommended that it be generated by Hibernate.

@ GeneratedValue

Specifies the primary key generation policy. There are four values:

TABLE: use the TABLE to save the id value.

IDENTITY: identitycolumn

SEQUENCR: sequence

AUTO: Use the preceding three methods based on different databases.

@ Column

Declares the ing between this attribute and the database fields.

 @Column(nam=”category_name” length=20 Public      Return  } 

1. When POJO has attributes that do not need to be mapped, it must be modified with @ Transitent. This comment indicates that this attribute has no ing relationship with the table and is only a temporary attribute.

2. @ Lob annotation indicates that the attribute persists to the Blob or Clob type, depending on the attribute type.

 

One-to-Multiple Association

@ OneToMany (mappedBy = "person", cascade = CascadeType. ALL, fetch = FetchType. LAZY)

One-to-multiple Declaration

@ ManyToOne (cascade = CascadeType. REFRESH ,)

@ JoinColumn

Multiple-to-one declaration, declared as two-way Association

 

One-to-one Association

@ OneToOne (optional = true, cascade = CascadeType. ALL, mappedBy = "person ")
One-to-one association statement
@ OneToOne (optional = false, cascade = CascadeType. REFRESH)
@ JoinColumn (name = "Person_ID", referencedColumnName = "personid", unique = true)
Declared as bidirectional Association

 

Multi-to-Multi-Association

@ ManyToMany (mappedBy = "students ")
Multiple-to-Multiple Association Declaration.
@ ManyToMany (cascade = CascadeType. PERSIST, fetch = FetchType. LAZY)
@ JoinTable (name = "Teacher_Student ",
JoinColumns = {@ JoinColumn (name = "Teacher_ID", referencedColumnName = "teacherid ")},
InverseJoinColumns = {@ JoinColumn (name = "Student_ID", referencedColumnName = "studentid ")})

 

Instance:

There are two entities: Goods and Category. The two are many-to-one associations.

The Hibernate Annotation is as follows:

    @Table(name = "goods", catalog = "test"   Goods           serialVersionUID = 1L                                            @Column(name = "goods_id", unique = , nullable = , length = 20                                .goodsId =                      @ManyToOne(fetch = FetchType.LAZY,cascade=     @JoinColumn(name = "category_id"                                .category =       @Column(name = "goods_name", nullable = , length = 50                                .goodsName =   }

 

Category. java

  @Table(name = "category", catalog = "test"   Category           serialVersionUID = -1877960009126534682L                 Set<Goods> goodses =  HashSet<Goods>(0                             @Column(name = "category_id", unique = , length = 10                                .categoryId =       @Column(name = "category_name", length = 20                                .categoryName =                      @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "category"      Set<Goods>                   setGoodses(Set<Goods>         .goodses =   }
 

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.