Common Hibernate annotations and hibernate annotations

Source: Internet
Author: User

Common Hibernate annotations and hibernate annotations

I. Class Annotation

1 ,@EntityModifies an object class and accepts a name attribute as the object class name.

2 ,@TableSpecifies the table name mapped to the persistence class. The following attributes are acceptable:

Catalog: can be omitted. It is used to set the tables mapped to the persistence class to be placed in the specified catalog. If omitted, it is placed in the default catalog.

Indexs: can be omitted. It is used to set indexes for persistence classes. The attribute value is a @ index annotation array.

@Index: Used to set indexes for data tables. Familiar with the following:

ColumnList:Cannot be omittedYou can specify multiple data columns to create indexes for those columns. When multiple columns are specified, use commas to separate multiple columns. For example, @ Index (columnList = "id, name, pass"), and set indexes for id, name, and pass.

Name: Index name, which can be omitted

Unique: It can be omitted. Set whether the index is unique. It can only be set to true or false. The default value is true.

Name: name of the table mapped to the object class. If omitted, it is the same as the class name by default.

Schema: place the table mapped to the persistence in the schema. If this parameter is omitted, it is placed in the default schema.

UniqueConstraints: Specifies the unique constraint for the table mapped by the persistence class. The value is a @ UniqueConstraint annotation array. Can be omitted.

@UniqueConstraint: Create a unique constraint for a data table. You must specify a columnNames attribute as a string array.

3 .@AccessUsed to change the PROPERTY access policy of Hibernate. The PROPERTY supports AccessType. PROPERTY and AccessType. FIELD. The default value is AccessType. PROPERTY.

AccessType. PROPERTY: Use the getter/setter method to access the PROPERTY.

AccessType. FIELD: Access attributes directly through member variables.

4 .@DynamicInsert: Specifies whether the SQL statement used for insertion is dynamically generated during running and only non-empty fields are inserted. The default value is false. Enabling the SQL statement causes hibernate to spend more time generating the SQL statement.

5 .@DynamicUpdate: Specifies whether the SQL statement used for updating is dynamically generated during running, and updates only those fields that have changed. The default value is false. Enabling the SQL statement will take more time for hibernate to generate the SQL statement.

6 .@SelectBeforeUpdate: Specifies whether to perform a query first when hibernate persists the object during update. The default value is false. If the query status is the same as the current status, update is not called to save the status.

7 .@Where:ClauseYou can specify an additional SQL statement filter condition. That is, when load () or get () and other query methods are used, only those that meet the where condition will be loaded. This annotation takes effect only during query.

8 .@BatchSize: When hibernate captures a collection or delays loading, it specifies each loadingSizeQuantity.

9 .@OptimisticLocking: Specifies an optimistic lock policy.TypeProperty acceptance

OptimisticLockType. VERSION: Check the version/timestamp Field

OptimisticLockType. ALL: Check ALL fields

OptimisticLockType. DIRTY: Check the modified field.

OptimisticLockType. NONE: No optimistic lock is used.

The default value is OptimisticLockType. VERSION.

10 .@Check: Pass attributesConstraintsSpecify an SQL expression to specify a check constraint for the corresponding table.

 Ii. Attribute Annotation

  1 ,@ColumnSpecifies the details of the data column mapped to an attribute. Accept common attributes

Attribute Required? Description
ColumnDefinition No The value represents the SQL string defined by a column. For details, see the SQL statement of this data column.
Insertable No Specifies whether the column is in the default iinsert statement. The default value is true.
Length No Specifies the maximum length of data stored in this column. The default value is 255.
Name No Specifies that the Column name of this Column defaults to the one modified by @ Column
Nullable No Specifies whether the column is allowed to be null. The default value is true.
Precisoon No When this column is of the decimal type, this attribute specifies the maximum number of valid digits for this column
Scale No When the column is of the decimal type, this attribute specifies the maximum number of decimal places of the column.
Table No Name of the table to which the column belongs. This attribute is required when multiple tables are used to save an object.
Unique No Specifies whether a unique attribute is required for this column. The default value is false.
Updatable No Whether the column is included in the update Statement List generated by hibernate. The default value is true.
     

2 ,@Formula:Value AttributeAccept an SQL expression. Fields modified by this annotation are calculated based on this SQL statement. The persistence table does not have columns corresponding to this attribute. This attribute will only take effect after reading

Note: The brackets in value = "(SQL)" cannot be omitted.

The column name and table name in SQL must correspond to the table name and column name in the database.

Using parameters in SQL can directly pass in the attributes of the current class

3 ,@Generated: Sets whether the value of the column mapped to this attribute is generated by the database. The value of this annotation can be GenerationTime. NEVER (no database generation), GenerationTime. ALWAYS (generated during insertion and update), GenerationTime. INSERT (updated only when inserted). This attribute requires a trigger in the underlying database. These values are read after the database is generated.

4 ,@Transient: Used to modify attributes that do not want to be permanently saved.

5 ,@Enumerated: Modify the enumeration type. The value attribute of this annotation can be EnumType. STRING (the underlying storage STRING) and EnumType. ORDINAL (the underlying storage number)

6 ,@Lob,@Basic: Modifies attributes of the big data type (Clob, Blob). When the attribute is byte [], Byte [], java. io. in the Serializable type, @ Lob maps Blob columns at the underlying layer. When the attribute is char [], Character [], java. lang. @ Lob is mapped to the Clob column at the underlying layer.

@BasicThe following two attributes are accepted:

Fetch: whether to delay loading. This attribute accepts FetchType. EAGER (immediate loading) and FetchType. LAZY (delayed loading)

Optional: Specify whether to allow null for the ing column.

7 ,@Temporal: Used to modify the date type. The value of this annotation accepts three values:

TemporalType. TIME (TIME only), TemporalType. DATE (DATE only), TemporalType. TIMESTAMP (TIMESTAMP)

  Iii. Primary Key Generation

For primary keys, use @IdSimple tagging without any attributes

Available @GeneratedValueTo modify the object tag attributes, you can specify the following attributes:

Strategy: Specifies the primary key generation policy of hibernate, which can be omitted. The following four values are accepted:

GenerationType. AUTO automatically selects the appropriate underlying database Generation Policy, which is the default value.

GenerationType. INDENTITY: for databases such as MYSQL and SQL Server, select an auto-increment generation policy.

GenerationType. SEQUENCE: Sequence-based primary key generation policy for Oracle. It should be used with @ SequenceGenerator.

GenerationType. TABLE: Use a secondary TABLE to generate a primary key, which should be used with @ TableGenerator.

Generator: Specify the generator name when GenerationType. SEQUENCE or GenerationType. TABLE is used.

For @SequenceGeneratorThe following table attributes can be specified:

Attribute Required? Description
Name Yes Name of the primary key generator
Catalog No It is used to set the tables mapped to the persistence class to the specified catalog. If this parameter is omitted, it is placed in the default catalog.
Schema No Used to set the tables mapped to the persistence class to be placed in the specified schema. If this parameter is omitted, the tables are placed in the default schema.
InitialValue No Specifies the initial value of the underlying Sequence. The integer specified by this attribute in Oracle is used as the start with value when Sequence is defined.
SequenceName No Specify the name of the underlying Sequence
AllocationSize No Specify the number of primary key values generated by the underlying Sequence.

 

 

 

 

 

 

For @TableGeneratorYou can specify the following table attributes. This annotation will generate an additional auxiliary table in the underlying database.

UniqueConstraints No This attribute is a @ UniqueConstraint array used to create unique constraints for the auxiliary table.
Name Yes Name of the primary key generator
AllocationSize No Specifies the number of primary key values generated for the underlying secondary table each time.
Catalog No Used to set tables mapped to persistence classes to the specified catalog. If this parameter is omitted, it is placed in the default catalog.
Schema No Used to set the tables mapped to the persistence class to be placed in the specified schema. If this parameter is omitted, it is placed in the default schema.
Table No Name of the secondary table
InitialValue No Use the specified value as the starting value of the underlying secondary table. The default value is 0.
PkColumnName No Name of the column that stores the primary key name
PkcolumnValue No Primary Key name
ValueColumnName No Name of the column that stores the primary key value
Indexs No Specify a @ Index array to create an Index for the secondary table.

 

 

 

 

 

 

 

 

 

Use hibernate @GenericGeneratorThe annotation defines the primary key generator. The annotation accepts two parameters:

Name: required. Set the name of the generator, which can be referenced by the generator attribute of @ GeneratedValue.

Strategy: required. Set the primary key generation policy for the generator. Accept the following values:

Increment: long, int, and short generate unique identifiers, which can only be used when no other process is inserted into the same table. Do not use it in the cluster!

Identity: Applicable to databases that provide identity (self-growth), such as DB2, MySql, SQL Server, Sybase, and HypersonicSQL.

Sequence: Applicable to databases with Sequence support, such as DB2, PostgreSQL, Oracle, sap db, and McKoi.

Hilo: uses a high/low level algorithm to generate long, int, and short type identifiers, which are unique in a database.

Seqhilo: A high/low-level algorithm is used to generate long, int, and short type identifiers. A Sequence name must be specified. This attribute applies to the database that provides Sequence.

Uuid: A 128-bit UUID algorithm is used to generate string-type identifiers, which are unique in the whole network. The algorithm generates a 32-Bit String Based on the IP address, JVM startup time, and a counter value (unique in JVM ).

Guid: The GUID string generated by the database in SQL Server and Mysql.

Native: select one of identity, sequenec, or hilo Based on the capabilities of the underlying database.

Assigned: assigns an identifier to the object before sava. It is equivalent to not using the primary key generation policy.

Select: Use a trigger to select a row with a unique primary key and return its primary key value as the ID attribute value.

Foreign: directly use the identifier property value of another associated object (that is, the persistence object does not generate a primary key ). This policy is only valid in the ing of 1-1 associations based on primary keys.

Related Article

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.