Hibernate annotations

1. Class-level annotations

@Entity      This annotation declares a class as an entity bean, which must have

name: optional, corresponding to a table in the database, if the table name is the same as the entity class name, it can be omitted
schema: optional, corresponding to the library name of the database, can be omitted

@Table      It can only be marked at the entity class definition to specify the corresponding relationship between the entity class and the database table. It is usually used in conjunction with @Entity and can be omitted.

name: optional, specify the name of the table; the default entity name is the same as the table name, if inconsistent, use this attribute to specify the table name
catalog: optional, specify the catalog name (usually write the database name)
schema: optional, the function is similar to the catalog

@Entity
@Table(name = "IDC_ALARM_BORD", schema = "CATH_THINKOPS")
public class AlarmBord { }
2. Attribute-level annotations

@Id      Define the primary key attribute mapped to the database table. An entity class can only have one attribute defined as the primary key, and a
@GeneratedValue      primary key generation strategy must be defined, which can be omitted.

strategy: Define the primary key generation strategy, the values ​​are:
GenerationType.AUTO (default, automatically selected according to the underlying database)
GenerationType.INDENTITY (generated according to the Identity field of the database, supporting DB2, MySQL, etc.)
GenerationType.SEQUENCE (using Sequence to determine the primary key Value, suitable for Oracle, DB2, etc.)
GenerationType.TABLE (use the specified table to determine the primary key value, combined with @TableGenerator)

generator: define the name of the primary key generator, such as: hibernate can specify the primary key generation method such as uuid

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@GenericGenerator      It is a custom primary key generation strategy generator provided by Hibernate, an extension to the JPA strategy

它要配合 @GeneratedValue 一起使用,并且 @GeneratedValue 注解中的 generator 属性要与 @GenericGenerator 注解中 name 属性一致,strategy 属性表示 hibernate 的主键生成策略

name: The value should be consistent with the generator attribute value in the @GeneratedValue annotation
strategy: The primary key generation strategy, the values ​​are: uuid, hilo, assigned, identity, select, sequence, seqhilo, increment, foreign, guid, uuid.hex, sequence- identity and native

@Id
@GeneratedValue(generator = "paymentableGenerator")
@GenericGenerator(name = "paymentableGenerator", strategy = "increment")
private Long id;

@Column      Map an attribute to a column, describing the details of the field in the database table

name: optional, indicating the name of the field in the database table, the default property name is the same
nullable: optional, indicating whether the field is allowed to be null, the default is true
unique: optional, indicating whether the field is a unique identifier, the default is false
length: optional, indicating the size of the field, only valid for fields of type String, the default value is 255
insertable: optional, indicating whether the field should appear in the INSETRT statement when the ORM framework performs an insert operation, the default is true
updateable : optional, indicating whether the field should appear in the UPDATE statement when the ORM framework performs an update operation, the default is true
columnDefinition: optional, indicating the actual type of the field in the database (VARCHAR, DATE, TEXT, BLOB)

@Column(name = "ALARM_ID")
private String alarmId;

@Transient      Indicates that the property is not a mapping to a field in a database table

@Transient // 定义该属性不映射到数据库表中(数据库表中没有这个字段)
private String hostName;

@Temporal      Used to define the time precision mapped to the database, only valid for Date type data

TemporalType: time precision, the values ​​are: DATE (date), TIME (time), TIMESTAMP (both)

@Temporal(TemporalType = DATE)
@Column
private Date createTime;

@JoinColumn      define that the field is an associated field

name: the name of the field, since @JoinColumn describes an associated field, such as ManyToOne, the default name is determined by its associated entity

@OneToOne(cascade = CascadeType.ALL, optional=true)
@JoinColumn(name = "hostId") // 指向另一张表的属性
private Long hostId;

@OneToOne、@OneToMany、@ManyToOne、@ManyToMany      Association mapping

cascade: set the cascade mode, the values ​​are:
CascadeType.ALL (all)
CascadeType.PERSIST (save)
CascadeType.REMOVE (delete)
CascadeType.MERGE (modify)
CascadeType.REFRESH (refresh)

fetch: configure the loading mode, the values ​​are : Fetch.EAGER (timely loading, many-to-one default is Fetch.EAGER), Fetch.LAZY (lazy loading)
targetEntity: configure the collection property type

@ManyToOne(cascade = { CascadeType.ALL }, fetch = FetchType.EAGER, optional = true)

@Formula      The role is to dynamically generate the properties of a class with a query statement

@Formula("(select COUNT(*) from user)") // 把SQL查询的结果赋值给该字段
private int count;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324840694&siteId=291194637