在前面我们提到过ORM(Object Relational Mapping),这里面的O对应的是架构中应用层的对象实体。在JAP中,我们需要创建一个类对应这个实体,而这个实体一般对应着数据库中的一张表。接下来我们好好讲一下,如何创建一个实体类,以及相关的注解。
示例代码:
import javax.persistence.*;
@Entity(name = "user")
@Table("t_user")
public class user {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Integer id;
@Column(name = "name")
private String name;
@Column(name = "age")
private Integer age;
@Column(name = "address")
private String address;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
当我们创建好了这个实体类,运行项目,框架会扫描到该类的注解,并且查看数据库中是否有这么一张对应的表。如果存在该表,再检查对应的属性是否相同。如果不同,框架会根据实体类更新数据库的表(前提是我们配好了"ddl-auto: update",数据定义自动更新)。下面,我们来一一介绍在实体类中使用到的注解。
- @Entity:
The @Entity annotation is used to specify that the currently annotate class represents an entity type. Unlike basic and embeddable types, entity types have an identity and their state is managed by the underlying Persistence Context.
@Entity注解用于指定当前被注解的类为一个实体类;与其他类型不同,实体类含有一个标识(主键),其状态被当前持久化上下文管理。The @Entity annotation defines just one attribute name which is used to give a specific entity name for use in JPQL queries. By default, the entity name represents the unqualified name of the entity class itself.
打开源码我们可以发现@Entity注解可以设置name属性值,该属性值默认为空。如果为空,框架默认类名为其值;该值用于生成查询语句。
@Table:
1 The @Table annotation is used to specify additional information to a JPA @Table annotation, like custom INSERT, UPDATE or DELETE statements or a specific FetchMode.
2 该注解有比较多的属性可以设置,具体如下:
String catalog
(Optional) The catalog of the table.
Index[] indexes
(Optional) Indexes for the table.
String name
(Optional) The name of the table.
String schema
(Optional) The schema of the table.
UniqueConstraint[] uniqueConstraints
(Optional) Unique constraints that are to be placed on the table.@Id:
- Specifies the primary key of an entity. The field or property to which the Id annotation is applied should be one of the following types: any Java primitive type; any primitive wrapper type; String; java.util.Date; java.sql.Date; java.math.BigDecimal; java.math.BigInteger.
@Id注解指定一个实体的主键,该注解可以用于Java的所有原始类型,元素类型对应的包装类和以上所示类。
2 The mapped column for the primary key of the entity is assumed to be the primary key of the primary table. If no Column annotation is specified, the primary key column name is assumed to be the name of the primary key property or field.
注解的属性被认为是对应表的主键,如果实体类中没有属性被@Id注解,那么实体类的主键名被默认为数据库表的主键名。
- @GeneratedValue:
- Provides for the specification of generation strategies for the values of primary keys.
为主键值的生成指定策略。 - The GeneratedValue annotation may be applied to a primary key property or field of an entity or mapped superclass in conjunction with the Id annotation. The use of the GeneratedValue annotation is only required to be supported for simple primary keys. Use of the GeneratedValue annotation is not supported for derived primary keys.
该主键配合@Id注解使用,策略如下(需要根据数据库的支持,选择使用对应的生成策略):
AUTO
Indicates that the persistence provider should pick an appropriate strategy for the particular database.
IDENTITY
Indicates that the persistence provider must assign primary keys for the entity using a database identity column.
SEQUENCE
Indicates that the persistence provider must assign primary keys for the entity using a database sequence.
TABLE
Indicates that the persistence provider must assign primary keys for the entity using an underlying database table to ensure uniqueness.
- @Column:
- Specifies the mapped column for a persistent property or field. If no Column annotation is specified, the default values apply.
指定实体类属性与数据库表中对应的属性名。 - 该注解有比较多的属性可以设置,具体如下:
String columnDefinition
(Optional) The SQL fragment that is used when generating the DDL for the column.
boolean insertable
(Optional) Whether the column is included in SQL INSERT statements generated by the persistence provider.
int length
(Optional) The column length.
String name
(Optional) The name of the column.
boolean nullable
(Optional) Whether the database column is nullable.
int precision
(Optional) The precision for a decimal (exact numeric) column.
int scale
(Optional) The scale for a decimal (exact numeric) column.
String table
(Optional) The name of the table that contains the column.
boolean unique
(Optional) Whether the column is a unique key.
boolean updatable
(Optional) Whether the column is included in SQL UPDATE statements generated by the persistence provider.
- 除了这些注解,其实还有其他注解没有列出。这里提供一个链接,提供参考:hibernate使用说明