一、實體基本映射 二、實體表間映射 1、一對一實體映射:人與地址 2、一對多實體映射:員工表與部門表 3、多對多實體映射:老師與學生 ...
一、實體基本映射
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 }