Hibernate annotation Parsing

Source: Internet
Author: User

Recently learn hibernate annotation form configuration Pojo class, the interpretation of annotations written down for later use.

Example 1.
  @Entity 
@Table (name= "user")    class  flight  Implements  serializable {    long id; @Id
@GeneratedValue (generator= "generator")
@GenericGenerator (name= "generator", strategy = "native") public  long getid ()  {  return  id; }  void  setid (Long id) {  this . Id = ID; } }

Hibernate can annotate a class's properties or methods. The GetXxx attribute corresponds to the field category, and the method's () corresponds to the property category.

@Entity

Declares a class as an entity bean.

@Table

Describes the name of the table, directory, and schema for this entity class mapping.

@Id

Declares the primary key for this table.

@GeneratedValue

Defines the growth strategy for the primary key. I am generally referred to the underlying database processing, so called the generator called the growth of the way, by the bottom of the @GenericGenerator implementation

@GenericGenerator

The primary key growth mode within Hibernate.

A detailed description of @GeneratedValue and @GenericGenerator is available in one of my other reproduced articles.
@GeneratedValue and @GenericGenerator

Example 2.

@Table (name= "Tbl_sky",
Uniqueconstraints = {@UniqueConstraint (columnnames={"Month", "Day"})})

@UniqueConstraint

Set a unique identifier for the corresponding field

Example 3.
1  Public class Flight implements  Serializable {2  @Version  3 @Column (name= "Optlock"  )  4public      Integer getversion () {}}

@Version

Annotations are used to support optimistic locking version control. It is generally possible to support version with a number or timestamp type.

@Column

Used to map the corresponding fields, where the parameters are detailed as follows:

Name = "ColumnName";(1)
BooleanUnique ()DefaultFalse(2)
BooleanNullable ()DefaultTrue(3)
BooleanInsertable ()DefaultTrue(4)
BooleanUpdatable ()DefaultTrue(5)
StringColumnDefinition ()Default"";(6)
StringTable ()Default "";(7)
IntLength ()Default 255;(8)
IntPrecision ()Default 0;(9)
IntScale ()Default 0;(10)
(1)Name optional, column name (default value is property name)
(2)Unique optional,Whether to set a unique constraint on this column (default value false)
(3)Nullable optional,Whether to set the value of the column can be null (default true)
(4)Insertable Optional,Whether the column is a column in the resulting INSERT statement (the default value is True)
(5)Updatable optional,Whether the column is a column in the generated UPDATE statement (the default value is True)
(6) columndefinition Optional , overwriting the SQL DDL fragment for this particular column (this may result in the inability to migrate between different databases)
(7) Table optional , define the corresponding tables (default is the main table)
(8) Length optional , column length (default value 255)
(9) Precision Optional , column decimal precision (decimal precision) (default value 0)
(Ten) scale is optional , if the column decimal value range (decimal level) is available, this setting (default value 0)

Example 4.
 Public class user{    @Transient        private  Integer ID;    @Basic (fetch=fetchtype.lazy,optional=true)    private  String name;    @Transient    public  Integer getId () {}
@Temporal (Temporaltype.time)
Public Java.util.Date GetDateTime () {};
}

@Transient
A getter method or attribute that is annotated as @Transient will not be persisted.

@Basic
All attributes that do not have an annotation defined are equivalent to the FETCH policy (fetch strategy) that adds an @Basic Note to a property that can be declared.

FETCH: FETCH strategy, delay load and immediate load, Optional: Specifies whether the field is allowed to be NULL when building the database structure.

@Temporal

Time accuracy (temporal precision) is not defined in the core Java API. So when working with time-type data, you also need to define the precision that you expect to store it in the database.
In the database, the data representing the time type is Date,time, and the TIMESTAMP three accuracy (that is, simple date, time, or both). You can use @Temporal annotations to adjust the precision.

Other properties:

@Enumerated
@javax. persistence.enumerated (enumtype.string)
Value:enumtype.string,enumtype.ordinal
Enumeration type member property mapping, enumtype.string specifies that the property is mapped to a string, enumtype.ordinal specifies that the attribute is mapped to the data order

@Lob
@javax. Persistence.lob
Used for labeling field types CLOB and BLOB types
Clob (Character Large ojects) type is a long string type, the entity can be of type char[], character[], or String type
Blob (Binary Large Objects) types are byte types, entities can be of type byte[], byte[], or classes that implement serializable interfaces.
Typically using lazy loading, @Basic (Fetch=fetchtype.lazy)

@SecondaryTable
@javax. persistence.secondarytable
Map an entity to multiple database tables
Such as:
@Entity
@SecondaryTables ({
@SecondaryTable (name = "Address"),
@SecondaryTable (name = "Comments")
})
public class Forum implements Serializable {
@Column (table = "Address", length = 100)
Private String Street;
@Column (table = "Address", Nullable = False)
Private String City;
@Column (table = "Address")
Private String conutry;
@Column (table = "Comments")
Private String title;
@Column (table = "Comments")
Private String Comments;
@Column (table = "Comments")
The value of the}table property specifies that the table name stored by the field is not used @Column annotation Change Property The default field will be present in the Forum table

Hibernate annotation Parsing

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.