Annotations commonly used in spring

Source: Internet
Author: User

Recently learning spring, where the data inserted table, personally think, in the way of annotations will be simple, the following to do some common comments summary.

I. Declaration of entities

@Entity

Comment on the entity. Any hibernate mapping object should have this comment

@Table

Declares that this object maps to the database's data table, through which it can specify the name of the table (Talbe), directory (catalog), and schema for the entity. The comment is not required, and if not, the system uses the default value (the short class name of the entity).

@Version

This comment can be used to add optimistic lock support in the entity bean.

Second, the declaration of the primary key

@Id

Declares this property as the primary key. This property value can be created by itself, but hibernate is recommended to be generated by hibernate

@GeneratedValue

Specifies the generation policy for the primary key. Has the following four values

Table: Save ID value with table

Identity:identitycolumn

Sequencr:sequence

AUTO: Use the above three according to the different database

1 /* 2       * Primary key 3      * Build policy to autogrow 4      * Unique, length5*/        6     @Id7     @GeneratedValue8      True  false, length = 20)

Iii. Declaration of Common Properties

@Column

Declares the mapping between this property and the database field.

1@Table (name= "Attributes")2 @Entity3 4  Public classAttributesextendsbaseentity{5     Private Static Final LongSerialversionuid = 8462412232619290034L;6     7@Column (name= "image_id")8     Longimage_id;9@Column (name= "Synsets")Ten     PrivateString synsets; One@Column (name= "H") A     PrivateString H; -@Column (name= "object_id") -     Private intobject_id; the@Column (name= "Names") -     PrivateString names; -@Column (name= "W") -     PrivateString W; +@Column (name= "Y") -     PrivateString y; +@Column (name= "X") A     PrivateString x; at      -      -      Public Longgetimage_id () { -         returnimage_id; -     } -      Public voidSETIMAGE_ID (Longimage_id) { in          This. image_id =image_id; -     } to      PublicString getsynsets () { +         returnsynsets; -     } the      Public voidsetsynsets (String synsets) { *          This. Synsets =synsets; $     }Panax Notoginseng      PublicString Geth () { -         returnh; the     } +      Public voidSetH (String h) { A          This. H =h; the     } +      Public intgetobject_id () { -         returnobject_id; $     } $      Public voidSETOBJECT_ID (inti) { -          This. object_id =i; -     } the      PublicString GetNames () { -         returnnames;Wuyi     } the      Public voidsetnames (String names) { -          This. Names =names; Wu     } -      PublicString getw () { About         returnW; $     } -      Public voidSETW (String w) { -          This. W =W; -     } A      PublicString GetY () { +         returny; the     } -      Public voidsety (String y) { $          This. y =y; the     } the      PublicString GetX () { the         returnx; the     } -      Public voidSetX (String x) { in          This. x =x; the     } the @Override About     PublicString toString () the    { the         return"attributes[image_id" +image_id+ "Synsets" +synsets+ "H" +h+ "object_id" +object_id+ "names" +names+ the"W" + W + "Y" +y+ "x" + x + "]"; +}

Iv. Declaration of Association relations

One-to-many correlation relationship

1 @OneToMany (mappedby = "Attributes", Cascade={cascadetype.persist,cascadetype.merge,cascadetype.remove}, Fetch = fetchtype.eager)2     set<myobject> myobjects=new hashset<myobject> ();

One-to-many declarations

@ManyToOne (Cascade=cascadetype.refresh,)

@JoinColumn

A multiple-to-one declaration, declared as a two-way association

One to one correlation relationship

@OneToOne (optional= true,cascade =cascadetype.all, mappedby = "Person")
One-to-one correlation statement
@OneToOne (optional = false, Cascade = Cascadetype.refresh)
@JoinColumn (name = "person_id", referencedcolumnname = "PersonID", unique = True)
Declare as bidirectional association

Many-to-many association relationships

@ManyToMany (mappedby= "Students")
Many-to-many association claims.
@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")})

So much for the first time.

Reference Blog: https://www.cnblogs.com/hoojjack/p/6568920.html

72650464

Annotations commonly used in spring

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.