JPA Learning - Lesson 4, JPA Annotations

Commonly used basic annotations

@Entity

must

Before the @Entity(name="") annotation is used in the entity class declaration statement, it indicates that the Java class is an entity class and will be mapped to the specified database table.

name is optional, corresponding to the database table name

@Table

Optional
When the name of the entity class is different from the name of the database table it is mapped to, the @Table annotation needs to be used. This annotation is used in parallel with the @Entity annotation and is placed before the entity class declaration statement. It can be written in a separate statement line, or it can be written with the declaration statement. peers.

The commonly used option for @Table annotation is name, which is used to indicate the table name of the database. If the table name is set at the same time as @Entity, this will override the @Entity setting.

The @Table annotation also has two options catalog and schema to set the database catalog or schema to which the table belongs, usually the database name.

@Entity
@Table(name="employee")
public class Employee {
}

@Id

The @Id annotation must be
used to declare that an attribute of an entity class is mapped to the primary key column of the database. The attribute is usually placed before the attribute declaration statement, can be in the same line with the declaration statement, or can be written on a separate line.

    @Id
    private Integer Id;

The @Id annotation can also be placed before a property's getter method.

@GeneratedValue

optional

@GeneratedValue is used to mark the generation strategy of the primary key, specified by the strategy attribute. By default, JPA automatically chooses a primary key generation strategy that is most suitable for the underlying database: SqlServer corresponds to identity, MySQL corresponds to auto increment.

The following alternative strategies are defined in javax.persistence.GenerationType:

IDENTITY: The primary key field is increased by the database ID self-increasing method, which is not supported by Oracle;

AUTO: JPA automatically selects the appropriate strategy, which is the default option;

SEQUENCE: Generate the primary key through the sequence, specify the sequence name through the @SequenceGenerator annotation, MySql does not support this method

@Id
@SequenceGenerator(name = "ID_SEQUENCE_GENERATOR", sequenceName = "ID_SEQUENCE", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ID_SEQUENCE_GENERATOR")
@Column(name = "ID")
private Long id;

TABLE: The primary key is generated by the table, and the framework generates the primary key by the table simulation sequence. Using this strategy can make the application easier for database migration.

@Basic

optional

@Basic represents a simple mapping of attributes to the fields of the database table. For attributes that do not use @Column to specify the name of the generated table field, the default is @Basic, and the column name of the mapped database table will be mapped according to the configured naming strategy.

fetch: Indicates the read strategy of the attribute, there are two types: EAGER and LAZY, which represent main branch fetching and lazy loading respectively. The default is EAGER.

optional: indicates whether the property is allowed to be null, the default is true

@Column

optional

@Column describes the detailed definition of the field in the database table, which is very useful for tools that generate database table structure based on JPA annotations.
Attribute description:
name: Indicates the name of the field in the database table, the default case attribute name is the same as
nullable: Indicates Whether the field is allowed to be null, the default is true
unique: indicates whether the field is a unique identifier, the default is false
length: indicates the size of the field, only valid for fields of type String
insertable: indicates that when the ORM framework performs an insert operation, the Whether the field should appear in the INSETRT statement, the default is true
updateable: Indicates whether the field should appear in the UPDATE statement when the ORM framework performs an update operation, the default is true. This property is very useful for fields that cannot be changed once created , such as for the birthday field.
columnDefinition: Indicates the actual type of the field in the database. Usually the ORM framework can automatically determine the type of the field in the database according to the attribute type, but for the Date type, it is still impossible to determine whether the field type in the database is DATE, TIME or TIMESTAMP. Also, the default mapping type for String is VARCHAR, which is useful if you want to map a String type to a BLOB or TEXT field type for a specific database.

@Column(name="BIRTH", nullable="false", columnDefinition="DATE")
public String getBithday() {
      return birthday;
 }

@Transient

optional

Indicates that the property is not a mapping to a field in the database table, the ORM framework will ignore this property.

If an attribute is not the field mapping of the database table, it must be marked as @Transient, otherwise, the ORM framework defaults to its annotation as @Basic

@Temporal

optional

The temporal precision of the Date type is not defined in the core Java API. In the database, the data representing the Date type has three precisions: DATE, TIME, and TIMESTAMP (ie simple date, time, or both). ). You can use the @Temporal annotation to adjust the precision when doing property mapping.

@Temporal(TemporalType.DATE)
private Date empBirth;

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326759245&siteId=291194637