Learning about some annotations in hibernate

Source: Internet
Author: User

1. @Column annotations

Just like the @table annotation is used to identify an entity class that is similar to a data table , @Column annotations to identify the attributes in the entity class and the fields in the data table the corresponding relationship .

@Column Note There are 10 properties, all of which are optional, and each attribute has the following meanings:

name
The Name property defines the names of the fields in the database table for the labeled field;

Unique
The Unique property indicates whether the field is a unique identity and defaults to false. If there is a field in the table that needs to be uniquely identified, you can either use the tag or use the @uniqueconstraint in the @table tag.

Nullable
The Nullable property indicates whether the field can be a null value and defaults to True.

insertable
The Insertable property indicates whether the value of the field needs to be inserted when inserting data using an "insert" script.

Updatable
The Updatable property indicates whether the value of the field needs to be updated when inserting data using the "Update" script. Insertable and updatable properties are generally used for read-only properties, such as primary and foreign keys. The values of these fields are usually generated automatically.

ColumnDefinition
The ColumnDefinition property represents the SQL statement created by this field when the table is created, typically used when generating table definitions through entity. (That is, if the table in DB is already built, this property is not necessary.) )

Table
The Table property defines the names of the tables that contain the current field.

length
The Length property represents the duration of the field, which is valid when the field is of type varchar, and the default is 255 characters.

Precision and scale
The Precision property and the scale property represent precision, and when the field type is double, precision represents the total length of the numeric value, which represents the number of digits that the decimal point occupies.

This tag can be labeled before the Getter method or property, for example, the following two labeling methods are correct

2. @JoinColumn

The

differs from @column in that @JoinColumn note is the field that holds the relationship between the table and the table, which you want to label on entity Properties . The @column label is a field in the table that does not contain a table relationship.

      As with @column tags, the Name property is used to identify the names of the fields that correspond to the table. For example, there is a field addr_id in the Customer table, and the code identified is as follows. &NBSP

@OneToOne  

@JoinColumn ( name = "addr_id" strong>)  

Public Addresseo getaddress () { 

          return address; 



If the value of name is not set at this time, by default, the value of name follows the following rules:  

Name= + "_" +  

For example, if you do not specify a value for name in the Customereo entity, the default is name=address_id, because the @joincolumn comment is in the entity On the Addresseo property, the corresponding table name for the entity Addresseo is "address", and the primary key for the table address is "id", so the default field name is "address_id" at this time.  
3, @ManyToOne

From the specific user users to find their corresponding group, in turn can be found through the group to the specific users, so is a two-way association, so the group for users is a "one-to-many" relationship @onetomany,users for group is "many to a" @ Manytoone.

@Entity  @Table (name= "T_group")//Specify a table name public  class Group   {      private int id;      private String name;      Private set<users> Users = new hashset<users> ();        @Id      @GeneratedValue//primary key with self-increment sequence public      int getId () {          return Id;      }      @OneToMany (mappedby= "group", Cascade= (Cascadetype.all))//with "many" side as the dominant management, cascade with all public      set<users> getusers () {          return users;      }  

  

@Entity  @Table (name= "t_users") Public  class Users   {      private int id;      private String name;      Private group group;        @Id      @GeneratedValue Public      int getId () {          return Id;      }      @ManyToOne (fetch=fetchtype.lazy,cascade= (Cascadetype.all))//Resolve 1+N, cascade with all      @JoinColumn (name= "groupId")// Specifies the foreign key name, the default value that is not specified is group_id public      Group Getgroup () {          return Group;      }  

  

Learning about some annotations in hibernate

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.