hibernate annotation註解方式來處理映射關系
在hibernate中,通常配置對象關系映射關系有兩種,一種是基於xml的方式,另一種是基於annotation的註解方式,熟話說,蘿蔔青菜,可有所愛,每個人都有自己喜歡的配置方式,我在試了這兩種方式以後,發現使用annotation的方式可以更簡介,所以這裏就簡單記錄下通過annotation來配置各種映射關系,在hibernate4以後已經將annotation的jar包集成進來了,如果使用hibernate3的版本就需要引入annotation的jar包。
一、單對象操作
1 @Entity ---> 如果我們當前這個bean要設置成實體對象,就需要加上Entity這個註解2 @Table(name="t_user") ----> 設置數據庫的表名 3 public class User 4 { 5 private int id; 6 private String username; 7 private String password; 8 private Date born; 9 private Date registerDate; 10 11 @Column(name="register_date") ---> Column中的name屬性對應了數據庫的該字段名字,裏面還有其他屬性,例如length,nullable等等12 public Date getRegisterDate() 13 { 14 return registerDate; 15 } 16 public void setRegisterDate(Date registerDate) 17 { 18 this.registerDate = registerDate; 19 } 20 21 @Id ---> 定義為數據庫的主鍵ID (建議不要在屬性上引入註解,因為屬性是private的,如果引入註解會破壞其封裝特性,所以建議在getter方法上加入註解)22 @GeneratedValue ----> ID的生成策略為自動生成 23 public int getId() 24 { 25 return id; 26 } 27 public void setId(int id) 28 { 29 this.id = id; 30 } 31 ............ 32 }
最後只需要在hibernate.cfg.xml文件裏面將該實體類加進去即可:
<!-- 基於annotation的配置 -->
<mapping class="com.qinkang.bean.User"/>
<!-- 基於hbm.xml配置文件 -->
<mapping resource="com/qinkang/bean/User.hbm.xml"/>
這樣我們就可以寫測試類來進行我們的CRUD操作了。
二、一對多的映射(one-to-many)
這裏我們定義了兩個實體類,一個是ClassRoom,一個是Student,這兩者是一對多的關聯關系。
ClassRoom類:
1 @Entity 2 @Table(name="t_classroom") 3 public class ClassRoom 4 { 5 private int id; 6 private String className; 7 private Set<Student> students; 8 9 public ClassRoom() 10 { 11 students = new HashSet<Student>(); 12 } 13 14 public void addStudent(Student student) 15 { 16 students.add(student); 17 } 18 19 @Id 20 @GeneratedValue 21 public int getId() 22 { 23 return id; 24 } 25 26 public void setId(int id) 27 { 28 this.id = id; 29 } 30 31 public String getClassName() 32 { 33 return className; 34 } 35 36 public void setClassName(String className) 37 { 38 this.className = className; 39 } 40 41 @OneToMany(mappedBy="room") ---> OneToMany指定了一對多的關系,mappedBy="room"指定了由多的那一方來維護關聯關系,mappedBy指的是多的一方對1的這一方的依賴的屬性,(註意:如果沒有指定由誰來維護關聯關系,則系統會給我們創建一張中間表) 42 @LazyCollection(LazyCollectionOption.EXTRA) ---> LazyCollection屬性設置成EXTRA指定了當如果查詢數據的個數時候,只會發出一條 count(*)的語句,提高性能 43 public Set<Student> getStudents() 44 { 45 return students; 46 } 47 48 public void setStudents(Set<Student> students) 49 { 50 this.students = students; 51 } 52 53 }
Student類:
1 @Entity 2 @Table(name="t_student") 3 public class Student 4 { 5 private int id; 6 private String name; 7 private int age; 8 private ClassRoom room; 9 10 @ManyToOne(fetch=FetchType.LAZY) ---> ManyToOne指定了多對一的關系,fetch=FetchType.LAZY屬性表示在多的那一方通過延遲加載的方式加載對象(默認不是延遲加載) 11 @JoinColumn(name="rid") ---> 通過 JoinColumn 的name屬性指定了外鍵的名稱 rid (註意:如果我們不通過JoinColum來指定外鍵的名稱,系統會給我們聲明一個名稱) 12 public ClassRoom getRoom() 13 { 14 return room; 15 } 16 public void setRoom(ClassRoom room) 17 { 18 this.room = room; 19 } 20 @Id 21 @GeneratedValue 22 public int getId() 23 { 24 return id; 25 } 26 public void setId(int id) 27 { 28 this.id = id; 29 } 30 public String getName() 31 { 32 return name; 33 } 34 public void setName(String name) 35 { 36 this.name = name; 37 } 38 public int getAge() 39 { 40 return age; 41 } 42 public void setAge(int age) 43 { 44 this.age = age; 45 } 46 47 }
三、一對一映射(One-to-One)
一對一關系這裏定義了一個Person對象以及一個IDCard對象
Person類:
1 @Entity 2 @Table(name="t_person") 3 public class Person 4 { 5 private int id; 6 private String name; 7 private IDCard card; 8 9 @OneToOne(mappedBy="person") ---> 指定了OneToOne的關聯關系,mappedBy同樣指定由對方來進行維護關聯關系 10 public IDCard getCard() 11 { 12 return card; 13 } 14 public void setCard(IDCard card) 15 { 16 this.card = card; 17 } 18 @Id 19 @GeneratedValue 20 public int getId() 21 { 22 return id; 23 } 24 public void setId(int id) 25 { 26 this.id = id; 27 } 28 public String getName() 29 { 30 return name; 31 } 32 public void setName(String name) 33 { 34 this.name = name; 35 } 36 37 }
IDCard類:
1 @Entity 2 @Table(name="t_id_card") 3 public class IDCard 4 { 5 private int id; 6 private String no; 7 private Person person; 8 9 @Id 10 @GeneratedValue 11 public int getId() 12 { 13 return id; 14 } 15 public void setId(int id) 16 { 17 this.id = id; 18 } 19 public String getNo() 20 { 21 return no; 22 } 23 public void setNo(String no) 24 { 25 this.no = no; 26 } 27 @OneToOne ---> OnetoOne指定了一對一的關聯關系,一對一中隨便指定一方來維護映射關系,這裏選擇IDCard來進行維護 28 @JoinColumn(name="pid") ---> 指定外鍵的名字 pid 29 public Person getPerson() 30 { 31 return person; 32 } 33 public void setPerson(Person person) 34 { 35 this.person = person; 36 } 37 }
四、Many-to-Many映射(多對多映射關系)
多對多這裏通常有兩種處理方式,一種是通過建立一張中間表,然後由任一一個多的一方來維護關聯關系,另一種就是將多對多拆分成兩個一對多的關聯關系
1.通過中間表由任一一個多的一方來維護關聯關系
Teacher類:
1 @Entity 2 @Table(name="t_teacher") 3 public class Teacher 4 { 5 private int id; 6 private String name; 7 private Set<Course> courses; 8 9 public Teacher() 10 { 11 courses = new HashSet<Course>(); 12 } 13 public void addCourse(Course course) 14 { 15 courses.add(course); 16 } 17 18 @Id 19 @GeneratedValue 20 public int getId() 21 { 22 return id; 23 } 24 public void setId(int id) 25 { 26 this.id = id; 27 } 28 public String getName() 29 { 30 return name; 31 } 32 public void setName(String name) 33 { 34 this.name = name; 35 } 36 @ManyToMany(mappedBy="teachers") ---> 表示由Course那一方來進行維護 37 public Set<Course> getCourses() 38 { 39 return courses; 40 } 41 public void setCourses(Set<Course> courses) 42 { 43 this.courses = courses; 44 } 45 46 }
Course類:
1 @Entity 2 @Table(name="t_course") 3 public class Course 4 { 5 private int id; 6 private String name; 7 private Set<Teacher> teachers; 8 9 public Course() 10 { 11 teachers = new HashSet<Teacher>(); 12 } 13 public void addTeacher(Teacher teacher) 14 { 15 teachers.add(teacher); 16 } 17 @ManyToMany ---> ManyToMany指定多對多的關聯關系 18 @JoinTable(name="t_teacher_course", joinColumns={ @JoinColumn(name="cid")}, 19 inverseJoinColumns={ @JoinColumn(name = "tid") }) ---> 因為多對多之間會通過一張中間表來維護兩表直接的關系,所以通過 JoinTable 這個註解來聲明,name就是指定了中間表的名字,JoinColumns是一個 @JoinColumn類型的數組,表示的是我這方在對方中的外鍵名稱,我方是Course,所以在對方外鍵的名稱就是 rid,inverseJoinColumns也是一個 @JoinColumn類型的數組,表示的是對方在我這放中的外鍵名稱,對方是Teacher,所以在我方外鍵的名稱就是 tid 20 public Set<Teacher> getTeachers() 21 { 22 return teachers; 23 } 24 25 public void setTeachers(Set<Teacher> teachers) 26 { 27 this.teachers = teachers; 28 } 29 30 @Id 31 @GeneratedValue 32 public int getId() 33 { 34 return id; 35 } 36 37 public void setId(int id) 38 { 39 this.id = id; 40 } 41 42 public String getName() 43 { 44 return name; 45 } 46 47 public void setName(String name) 48 { 49 this.name = name; 50 } 51 52 }
2.將Many-to-Many拆分成兩個One-to-Many的映射(Admin、Role、AdminRole)
Admin類:
1 @Entity 2 @Table(name="t_admin") 3 public class Admin 4 { 5 private int id; 6 private String name; 7 private Set<AdminRole> ars; 8 public Admin() 9 { 10 ars = new HashSet<AdminRole>(); 11 } 12 public void add(AdminRole ar) 13 { 14 ars.add(ar); 15 } 16 @Id 17 @GeneratedValue 18 public int getId() 19 { 20 return id; 21 } 22 public void setId(int id) 23 { 24 this.id = id; 25 } 26 public String getName() 27 { 28 return name; 29 } 30 public void setName(String name) 31 { 32 this.name = name; 33 } 34 @OneToMany(mappedBy="admin") ---> OneToMany關聯到了AdminRole這個類,由AdminRole這個類來維護多對一的關系,mappedBy="admin" 35 @LazyCollection(LazyCollectionOption.EXTRA) 36 public Set<AdminRole> getArs() 37 { 38 return ars; 39 } 40 public void setArs(Set<AdminRole> ars) 41 { 42 this.ars = ars; 43 } 44 }
Role類:
1 @Entity 2 @Table(name="t_role") 3 public class Role 4 { 5 private int id; 6 private String name; 7 private Set<AdminRole> ars; 8 public Role() 9 { 10 ars = new HashSet<AdminRole>(); 11 } 12 public void add(AdminRole ar) 13 { 14 ars.add(ar); 15 } 16 @Id 17 @GeneratedValue 18 public int getId() 19 { 20 return id; 21 } 22 public void setId(int id) 23 { 24 this.id = id; 25 } 26 public String getName() 27 { 28 return name; 29 } 30 public void setName(String name) 31 { 32 this.name = name; 33 } 34 @OneToMany(mappedBy="role") ---> OneToMany指定了由AdminRole這個類來維護多對一的關聯關系,mappedBy="role" 35 @LazyCollection(LazyCollectionOption.EXTRA) 36 public Set<AdminRole> getArs() 37 { 38 return ars; 39 } 40 public void setArs(Set<AdminRole> ars) 41 { 42 this.ars = ars; 43 } 44 }
AdminRole類:
1 @Entity 2 @Table(name="t_admin_role") 3 public class AdminRole 4 { 5 private int id; 6 private String name; 7 private Admin admin; 8 private Role role; 9 @Id 10 @GeneratedValue 11 public int getId() 12 { 13 return id; 14 } 15 public void setId(int id) 16 { 17 this.id = id; 18 } 19 public String getName() 20 { 21 return name; 22 } 23 public void setName(String name) 24 { 25 this.name = name; 26 } 27 @ManyToOne ---> ManyToOne關聯到Admin 28 @JoinColumn(name="aid") 29 public Admin getAdmin() 30 { 31 return admin; 32 } 33 public void setAdmin(Admin admin) 34 { 35 this.admin = admin; 36 } 37 @ManyToOne ---> 38 @JoinColumn(name="rid") 39 public Role getRole() 40 { 41 return role; 42 } 43 public void setRole(Role role) 44 { 45 this.role = role; 46 } 47 }
小提示:通過hibernate來進行插入操作的時候,不管是一對多、一對一還是多對多,都只需要記住一點,在哪個實體類聲明了外鍵,就由哪個類來維護關系,在保存數據時,總是先保存的是沒有維護關聯關系的那一方的數據,後保存維護了關聯關系的那一方的數據
hibernate annotation註解方式來處理映射關系