博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Hibernate注解
阅读量:4583 次
发布时间:2019-06-09

本文共 3440 字,大约阅读时间需要 11 分钟。

 

2.2. Mapping with JPA

2.2.1. Marking a POJO as persistent entity实体注解

@Entitypublic 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 propertyprivate String firstname; //persistent property@TransientString getLengthInMeter() { ... } //transient propertyString getName() {... } // persistent property@Basicint 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
@Lobpublic 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定义主键生成策略

2.2.3.1. Generating the identifier property

JPA defines five types of identifier generation strategies:五种主键生成策略

  • AUTO - either identity column, sequence or table depending on the underlying DB

  • TABLE - table holding the id

  • IDENTITY - identity column

  • SEQUENCE - sequence

  • identity copy - the identity is copied from another entity

 

@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关系映射注解

 

 

官网:

说明文档:

英文:

中文:

转载于:https://www.cnblogs.com/sunilsun/p/4766008.html

你可能感兴趣的文章
基于H5 pushState实现无跳转页面刷新
查看>>
关于同余与模运算的总结
查看>>
js中top、clientTop、scrollTop、offsetTop的区别 文字详细说明版
查看>>
【转载】法线贴图Nomal mapping 原理
查看>>
prado 初步分析
查看>>
php 做守护进程1
查看>>
简单员工管理实例
查看>>
SAP 到出XLS
查看>>
HSV
查看>>
JAVA程序中SQL语句无法传递中文参数
查看>>
Android学习_数据库查询使用rawQuery遇到的问题
查看>>
|待研究|委托付款的支付状态触发器
查看>>
redis集群中的主从复制架构(3主3从)
查看>>
初始Linux(其实之前接触过(*^__^*) 嘻嘻……)
查看>>
一些多项式的整理
查看>>
NIO selector
查看>>
MySQL中DATETIME、DATE和TIMESTAMP类型的区别
查看>>
asp代码获取年数,季度数.星期数,天数,小时数,分钟数,秒数等时
查看>>
python之建完model之后操作admin
查看>>
Java 类加载机制 ClassLoader Class.forName 内存管理 垃圾回收GC
查看>>