主键映射以及特殊类型映射
前面没有过多的描述@Id标注,现在拿出来单独谈谈!
1、主键标识(@Id)属性定义如下:
1
2
|
@Target ({METHOD, FIELD}) @Retention (RUNTIME) public @interface Id {} |
2、标注@Id后,还要涉及@GeneratedValue的使用
@GeneratedValue用于主键的生成策略,属性定义如下:
1
2
3
4
5
|
@Target ({METHOD, FIELD}) @Retention (RUNTIME) public @interface GeneratedVaule { GenerationType strategy() default AUTO; String generator() default "" ; } |
strategy属性表示生成主键的策略,定义在枚举类型GenerationType中:
1
|
public enum GenerationType { TABLE, SEQUENCE, IDENTITY, AUTO } |
注:double和float浮点类型和它们对应的封装类不能作为主键,因为判断是否唯一是通过equals方法判断的,浮点型的精度太大,不能准确地匹配
3、自增主键
MySQL实现:
1
2
3
|
create table customer ( id int (20) not null auto_increment, ) |
而oracle可能需要创建Sequence来实现自增。
实体类的配置如下:
1
2
3
4
5
6
7
8
9
10
|
@Entity @Table (name= "customer" ) public class CustomerEO implements java.io.Serializable { @Id @GeneratedVaule (strategy = GenerationType.AUTO) private Integer id; ... } |
4、表生成器(@TableGenerator)
使用sql创建一个表"tb_generator",如下:
1
2
3
4
5
6
7
8
|
create table tb_generator ( id int (20) unsigned not null auto_increment, gen_name varchar (255) not null , gen_vaule int (20) not null , primary key (id) ) insert into tb_generator (gen_name, gen_value) values ( 'CUSTOMER_PK' , '1' ); insert into tb_generator (gen_name, gen_value) values ( 'CONTACT_PK' , '100' ) |
数据库如下:
id | gen_name | gen_value |
1 | CUSTOMER_PK | 1 |
2 | CONTACT_PK | 100 |
使用@TableGenerator标记生成策略的具体设置:
1
2
3
4
5
6
7
8
9
|
@Id GeneratedValue(strategy = GenerationType.TABLE, generator= "customer_gen" ) @TableGenerator (name = "customer_gen" , table= "tb_generator" , pkColumnName= "gen_name" , vauleColumnName= "gen_value" , pkColumnValue= "CUSTOMER_PK" , allocatonSize= 1 ) private Integer id; |
@TableGenerator标记属性定义如下:
1
2
3
4
5
6
7
8
9
10
11
12
|
@Target ({TYPE, METHOD, FIELD}) @Retention (RUNTIME) public @interface TableGenerator { String name(); String table() default "" ; String catalog() default "" ; String schema() default "" ; String pkColumnName() default "" ; String valueColumnName() default "" ; int initiaValue() default 0 ; int allocationSize() default 50 ; UniqueConstraint[] uniqueConstraints() default (); } |
5、Sequence生成器(@Sequence)
SQL脚本:
1
|
create sequence customer_sequence start with 1 increment by 10 |
这个sequence从1开始,每次增长10(采用MySQL数据库中不支持Squence)
具体设置:
1
2
3
4
|
@Id @GeneratedValue (strategy = GenerationType.SEQUENCE, generator= "customer_seq" ) @SequenceGenerator (name= "customer_seq" , sequenceName= "customer_sequence" ) private Integer id; |
@SequenceGenerator属性定义如下:
1
2
3
4
5
6
7
|
@Target ({TYPE, METHOD, FIELD}) @Retention (RUNTIME) public @interface SequenceGenerator { String name(); String sequenceName() default "" ; int inittialVaule() default 0 ; int allocationSize() default 50 ; } |
6、Identity生成器
1
2
3
|
@Id @GeneratedValue (strategy = GenerationType.IDENTITY) private Integer id; |
7、几种主键生成策略的比较
(1)sequence,identity 两种策略针对的是一些特殊的数据库
(2)auto自动生成策略由JPA实现,对于比较简单的主键,对主键生成策略要求较少时,采用这种策略好
(3)table生成策略是将主键的持久化在数据库中,建议使用该策略
8、映射Blob和Clob(@Lob)
加载图片或长文本时,使用@Lob标记:
1
2
3
|
@Lob @Basic (fetch=FetchType.LAZY) private byte [] image; |
9、映射时间(Temporal)类型(@Temporal)
1
2
3
4
5
|
@Temporal (TemporalType.TIME) private java.util.Date createTime; @Temporal (TemporalType.DATE) private java.util.Date birthday; |
@Temporal的定义如下:
1
2
3
|
public @interface Temporal { TemporalType value() default TIMESTAMP; } |
TemporalType枚举类型如下:
1
2
3
4
5
|
public enum TemporalType { DATE, // java.sql.Date TIME, // java.sql.Time TIMESTAMP // java.sql.TimeStamp } |
10、映射非持久化类型(@Transient)
1
2
|
@Transient private String property; |
表示该实体内的"property"属性设置成非持久化的