本文主要通过案例介绍JPA常用注解的使用。
一、实体基本映射
1 /*
2 * @Entity:将领域对象标注为一个实体,表示保存到数据库中
3 * @@Table:保存到数据库中表名,默认表名为类名,可通过name属性命名
4 *
5 * */
6 @Entity
7 @Table(name="t_user")
8 public class User {
9
10
11 @Id //主键
12 @GeneratedValue // JPA自动选择合适的生成策略
13 @Column(name="id_") //列名,默认为属性名,可通过name属性指定列名
14 private Integer id;
15
16
17 /* @Id
18 @GeneratedValue(generator="uuidGenerator")
19 @GenericGenerator(name="uuidGenerator",strategy="uuid") //UUID生成策略
20 @Column(name="id_",length=32)
21 private String id;*/
22
23
24 /* @Id
25 @GeneratedValue(strategy = GenerationType.AUTO) //JPA自动选择合适的策略,是默认选项
26 @Column(name="id_")
27 private Integer id;*/
28
29
30 /*
31 *
32 @Id
33 @GeneratedValue(strategy=GenerationType.IDENTITY) //表示自增键字段,oracle不支持这种方式
34 @Column(name="id_")
35 private Integer id;*/
36
37
38 /*
39 @Id
40 @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "idGenerator") //通过序列产生主键,通过@SequenceGenerator注解指定序列列名,mysql不支持这种方式
41 @SequenceGenerator(name = "idGenerator",sequenceName="mySeq",allocationSize=1)
42 @Column(name="id_")
43 private Integer id;*/
44
45 /*
46 @Id
47 @GeneratedValue(strategy = GenerationType.TABLE, generator = "userGenerator")//通过表产生主键,框架借由表模拟序列产生主键,使用该策略可以使应用更易于数据库移植。
48 @TableGenerator(name = "userGenerator",table="pk_generator",
49 pkColumnName="gen_name",
50 valueColumnName="gen_value",
51 pkColumnValue="user_pk",
52 initialValue=0,
53 allocationSize=1)
54 @Column(name="id_")
55 private Integer id;
56 */
57
58 //length:字段的长度;nullable:是否允许为空;unique:指定是否唯一性;insertable updatable:指定该字段是否插入或修改到数据库表中,对应表中需通过columnDefinition指定默认值;
59 @Column(name="name_", length=60, nullable=false,unique=true,insertable=false,updatable=false)
60 private String name;
61
62 @Column(name="address_", length=60, nullable=false)
63 private String address;
64
65 @Column(name="phone_", length=11, nullable=true,columnDefinition="CHAR(10) default '000'") //columnDefinition:底层数据库的字段类型,根据不同数据库配置
66 private String phone;
67
68 @Column(name="inCome_", precision=12, scale=2) //precision:浮点型总长度;scale:小数的位数
69 private BigDecimal inCome;
70
71 @Temporal(TemporalType.DATE) //如果属性是时间类型,因为数据表对时间类型有更严格的划分,所以必须指定具体时间类型。
72 private Date birthday;
73 //Date 日期型,精确到年月日,例如“2008-08-08”
74 //Time 时间型,精确到时分秒,例如“20:00:00”
75 //Timestamp 时间戳,精确到纳秒,例如“2008-08-08 20:00:00.000000001”
76
77 @Lob
78 @Column(name="pic_")
79 @Basic(fetch=FetchType.LAZY)
80 private byte[] pic;
81
82 @Lob
83 @Column(name="note_")
84 @Basic(fetch=FetchType.LAZY)
85 private String note;
86
87 ...........
88 }
二、实体表间映射
1、一对一实体映射:人与地址
1 @Entity
2 public class Person {
3 @Id
4 @GeneratedValue(strategy = GenerationType.AUTO)
5 private Long id;
6 private String name;
7 private int age;
8 //@OneToOne 一对一映射,级联操作,通过cascade属性设置;懒加载操作,通过fetch属性设置
9 @OneToOne(cascade={CascadeType.REFRESH,CascadeType.REMOVE},fetch=FetchType.LAZY)
10 //@JoinColumn:Person中address字段对应的"address_id"表示Address中"aid"字段,即外键关系;如自动建表,@JoinColum注解可省略
11 @JoinColumn(name="address_id",referencedColumnName="aid")
12 //@JoinColumns(value={@JoinColumn(name="address_id",referencedColumnName="aid"),@JoinColumn(name="address_id2",referencedColumnName="aid2")})
13 private Address address;
14 ...........
15 }
1 @Entity
2 public class Address {
3
4 @Id
5 @GeneratedValue(strategy = GenerationType.AUTO)
6 private Long aid;
7 private String street;
8 private String city;
9 private String country;
10
11
12 @Column(name="aid_")
13 public Long getAid() {
14 return aid;
15 }
16 .............
17 }
2、一对多实体映射:员工表与部门表
1 @Entity
2 public class Depart {
3 @Id
4 @GeneratedValue
5 private Long did;
6 private String name;
7
8 @OneToMany //一对多映射
9 /*
10 通过中间表关联
11 @JoinTable 配置中间表信息,name:中间表名;
12 joinColumns:部门表与中间表间的关系,"depart_id"部门表中的主键字段作为中间表中外键(did)字段
13 inverseJoinColumns:被拥有者,即员工表与中间表的关系
14 */
15 @JoinTable(name = "depart_employee",
16 joinColumns = @JoinColumn(name = "depart_id",referencedColumnName="did"),
17 inverseJoinColumns = @JoinColumn(name = "employee_id",referencedColumnName="eid"))
18 /*
19 通过设置外键关联
20 设置"did"字段做为另一方的外键"depart_id"
21 @JoinColumn(name="depart_id",referencedColumnName="did")
22 */
23 private List<Employee> employees;
24 .....
25 }
1 @Entity
2 public class Employee {
3 @Id
4 @GeneratedValue(strategy = GenerationType.AUTO)
5 private Long eid;
6 private String name;
7 .......
8 }
3、多对多实体映射:老师与学生
1 @Entity
2 public class Teacher {
3
4 @Id
5 @GeneratedValue(strategy = GenerationType.AUTO)
6 private Long tid;
7 private String name;
8 private Boolean gender;
9 private int age;
10 private int height;
11 @ManyToMany
12 @JoinTable(name = "teacher_student",
13 joinColumns = @JoinColumn(name = "teacher_id",referencedColumnName="tid"),
14 inverseJoinColumns = @JoinColumn(name = "student_id",referencedColumnName="sid"))
15 private List<Student> students;
16 ..........
17 }
1 @Entity
2 public class Student {
3
4 @Id
5 @GeneratedValue(strategy = GenerationType.AUTO)
6 private Long sid;
7 private String name;
8 private Boolean gender;
9 private int age;
10 private int height;
11
12 @ManyToMany(mappedBy = "students")
13 private List<Teacher> teachers;
14 ...............
15 }