The JPA @column () annotation is detailed __JPA

Source: Internet
Author: User
Tags table definition

Just like @table annotations are used to identify the corresponding relationship between an entity class and a datasheet, @Column annotations identify the corresponding relationship between the attributes in the entity class and the fields in the datasheet.

This annotation is defined as follows:

Source code recreated from a. class file by IntelliJ idea//(powered by Fernflower Decompiler)//PackageJavax.persistence;ImportJava.lang.annotation.ElementType;ImportJava.lang.annotation.Retention;ImportJava.lang.annotation.RetentionPolicy;ImportJava.lang.annotation.Target; @Target ({elementtype.method, Elementtype.field}) @Retention (Retentionpolicy.runtime) Public@InterfaceColumn {String name ()default"";BooleanUnique ()Default False;BooleanNullable ()default True;BooleanInsertable ()default True;BooleanUpdatable ()default True; String ColumnDefinition ()default""; String table ()default"";intLength ()default255;intPrecision ()default0;intScale ()default0; }

As you can see from the definition, there are 10 attributes in the @Column annotation, all of which are optional, and each of these 10 properties has the following meanings:

1, the name Name property defines the names of the fields in the database table to which the fields are labeled;

2, the unique unique property indicates whether the field is a unique identifier, and the default is False. If you have a field in a table that requires a unique identity, you can either use the tag or use the @uniqueconstraint in the @table tag.

3. The Nullable Nullable property indicates whether the field can be a null value and the default is true.

4. The Insertable Insertable property indicates whether the value of the field needs to be inserted when inserting data using the Insert script.

5. The Updatable 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 keys and foreign keys. The values of these fields are usually generated automatically.

6. The ColumnDefinition ColumnDefinition property indicates that the SQL statement created by the field when the table was created is typically used when creating a table definition by entity. (That is, if the table in DB is already built, it is not necessary to use this property.) )

7, the Table Table property defines the name of the form that contains the current field.

8, the length length property represents the size of the field, and when the field is of type varchar, the property is valid and the default is 255 characters.

9, precision and 10, scale

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

API Document Address: http://docs.oracle.com/javaee/5/api/javax/persistence/Column.html

When using this @column tag, you need to be aware of the following issues:

1, this mark can be labeled in the Getter method

2, this mark can be annotated in front of the property

For example, the following two annotation methods are correct:

1, callout in the attribute before: import Javax.persistence.Column;  Import javax.persistence.Entity;  Import javax.persistence.Table;  @Entity @Table (name = "Contacts") public class Contacteo {@Column (name = "Contact_Name") private String name;  Public String GetName () {return name;  public void SetName (String name) {this.name = name; }  }

2, marking in the Getter method before: import Javax.persistence.Column;  Import javax.persistence.Entity;  Import javax.persistence.Table;  @Entity @Table (name = ' Contact ') public class Contacteo {private String name;  @Column (name = "Contact_Name") public String GetName () {return name;  public void SetName (String name) {this.name = name; }  }

Hint: The JPA specification does not explicitly specify the annotation method, as long as either of the two annotation methods can be selected. This is based on personal preferences to choose, I am accustomed to using the first method.

Here are a few small examples:

Example one: The length of the specified field "Contact_Name" is "512" and the value cannot be null.

private String name;

@Column (name= "Contact_Name", nullable=false,length=512)

Public String GetName () {

return name;

}

The SQL statement that you create is shown below.

CREATE TABLE Contact (

ID integer NOT NULL,

Contact_Name varchar () NOT NULL,

Primary KEY (ID)

)

Example two: The type for the specified field "Monthly_income" monthly income is double, with a precision of 12 digits and a decimal point of 2 digits.

Private BigDecimal Monthlyincome;

@Column (name= "Monthly_income", precision=12, scale=2)

Public BigDecimal Getmonthlyincome () {

return monthlyincome;

}

The SQL statement that you create is shown below.

CREATE TABLE Contact (

ID integer NOT NULL,

Monthly_income double (12,2),

Primary KEY (ID)

)

Example three: Customize the SQL statement that generates the CLOB type field.

private String name;

@Column (name= "Contact_Name", columndefinition= "Clob not NULL")

Public String GetName () {

return name;

}

The definition SQL statement for the build table is shown below.

CREATE TABLE Contact (

ID integer NOT NULL,

Contact_Name CLOB is not NULL,

Primary KEY (ID)

)

Where the bold portion is the value set by the ColumnDefinition property. If you do not specify this property, you typically use the default type to build the table, which you can set when you need to customize the type of the table.

Hint: The two ORM strategies are to define the build table through entity, or to configure entity through a table. The mapping strategy for the two methods is good or bad and will be compared in detail in the chapter of this book, "Using the JPA tools."

Example four: The field values are read-only and are not allowed to be inserted and modified. Typically used for primary and foreign keys.

Private Integer ID;

@Column (name= "id", Insertable=false,updatable=false)

Public Integer getId () {

return ID;

}

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.