2.2. Mapping with JPA
2.2.1. Marking a POJO as persistent entity实体注解
@Entity public class Flight implements Serializable { Long id; @Id public Long getId() { return id; } public void setId(Long id) { this.id = id; } }
@Entity
declares the class as an entity (i.e. a persistent POJO class), @Id
declares the identifier property of this entity. The other mapping declarations are implicit. The class Flight is mapped to the Flight table, using the column id as its primary key column.
@Entity
声明一个实体类。
@Id
声明该类的主键
2.2.1.1. Defining the table
@Table
is set at the class level; it allows you to define the table, catalog, and schema names for your entity mapping. If no @Table
is defined the default values are used: the unqualified class name of the entity.
@Table
声明在类级别。用来定义表名,如果不声明,默认类名即为表名。
@Entity @Table(name="tbl_sky") public class Sky implements Serializable { ... }
2.2.2. Mapping simple properties通用属性注解
2.2.2.1. Declaring basic property mappings
Every non static non transient property (field or method depending on the access type) of an entity is considered persistent, unless you annotate it as @Transient
.
任务非静态非临时变量都会被持久化,除非声明为@Transient
public transient int counter; //transient property private String firstname; //persistent property @Transient String getLengthInMeter() { ... } //transient property String getName() {... } // persistent property @Basic int getLength() { ... } // persistent property @Basic(fetch = FetchType.LAZY) String getDetailedComment() { ... } // persistent property @Temporal(TemporalType.TIME) java.util.Date getDepartureTime() { ... } // persistent property @Enumerated(EnumType.STRING) Starred getNote() { ... } //enum persisted as String in database
@Lob
public String getFullText() {
return fullText;
}
@Lob
public byte[] getFullCode() {
return fullCode;
}
@Transient
, will be ignored by the entity manager。不会被持久化。
fetch = FetchType.LAZY 延迟加载。The detailedComment
property value will be lazily fetched from the database once a lazy property of the entity is accessed for the first time.
Temporal data can have DATE
, TIME
, or TIMESTAMP
precision (ie the actual date, only the time, or both). Use the @Temporal
annotation to fine tune that.
@Lob
indicates that the property should be persisted in a Blob or a Clob depending on the property type: java.sql.Clob
, Character[]
, char[]
and java.lang.String
will be persisted in a Clob. java.sql.Blob
, Byte[]
, byte[]
and serializable type will be persisted in a Blob.
2.2.3. Mapping identifier properties主键注解
The @Id
annotation lets you define which property is the identifier of your entity. This property can be set by the application itself or be generated by Hibernate (preferred). You can define the identifier generation strategy thanks to the @GeneratedValue
annotation.
@Id
定义主键
@GeneratedValue
定义主键生成策略
JPA defines five types of identifier generation strategies:五种主键生成策略
@Id @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SEQ_STORE")
public Integer getId() { ... }
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
public Long getId() { ... }
2.2.5. Mapping entity associations/relationships关系映射注解
说明文档:
英文:http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/
中文:http://docs.jboss.org/hibernate/annotations/3.4/reference/zh_cn/html_single/