Hibernate-关系映射
一、一对一双向外键关联【Husband-Wife】
Husband.java
View Code
1 package org.xiong.hibernate.model; 2 3 import javax.persistence.Entity; 4 import javax.persistence.GeneratedValue; 5 import javax.persistence.Id; 6 import javax.persistence.JoinColumn; 7 import javax.persistence.OneToOne; 8 9 @Entity 10 public class Husband 11 { 12 private int id; 13 private String name; 14 private Wife wife; 15 16 @OneToOne 17 @JoinColumn(name="wifeId") 18 public Wife getWife() 19 { 20 return wife; 21 } 22 23 public void setWife(Wife wife) 24 { 25 this.wife = wife; 26 } 27 28 @Id 29 @GeneratedValue 30 public int getId() 31 { 32 return id; 33 } 34 35 public void setId(int id) 36 { 37 this.id = id; 38 } 39 40 public String getName() 41 { 42 return name; 43 } 44 45 public void setName(String name) 46 { 47 this.name = name; 48 } 49 50 }
Wife.java
View Code
1 package org.xiong.hibernate.model; 2 3 import javax.persistence.CascadeType; 4 import javax.persistence.Entity; 5 import javax.persistence.GeneratedValue; 6 import javax.persistence.Id; 7 import javax.persistence.JoinColumn; 8 import javax.persistence.OneToOne; 9 10 @Entity 11 public class Wife 12 { 13 private int id; 14 private String name; 15 private Husband husband=new Husband(); 16 17 @OneToOne(mappedBy="wife",cascade=CascadeType.ALL) 18 public Husband getHusband() 19 { 20 return husband; 21 } 22 23 public void setHusband(Husband husband) 24 { 25 this.husband = husband; 26 } 27 28 @Id 29 @GeneratedValue 30 public int getId() 31 { 32 return id; 33 } 34 35 public void setId(int id) 36 { 37 this.id = id; 38 } 39 40 public String getName() 41 { 42 return name; 43 } 44 45 public void setName(String name) 46 { 47 this.name = name; 48 } 49 }
生成的SQL语句如下:
View Code
1 16:06:06,578 INFO SchemaExport:226 - Running hbm2ddl schema export 2 16:06:06,578 DEBUG SchemaExport:242 - import file not found: /import.sql 3 16:06:06,578 INFO SchemaExport:251 - exporting generated schema to database 4 16:06:06,765 DEBUG SchemaExport:377 - 5 drop table Husband cascade constraints 6 16:06:06,906 DEBUG SchemaExport:377 - 7 drop table Wife cascade constraints 8 16:06:06,968 DEBUG SchemaExport:377 - 9 drop sequence hibernate_sequence 10 16:06:06,984 DEBUG SchemaExport:377 - 11 create table Husband ( 12 id number(10,0) not null, 13 name varchar2(255 char), 14 wifeId number(10,0), 15 primary key (id) 16 ) 17 16:06:07,062 DEBUG SchemaExport:377 - 18 create table Wife ( 19 id number(10,0) not null, 20 name varchar2(255 char), 21 primary key (id) 22 ) 23 16:06:07,109 DEBUG SchemaExport:377 - 24 alter table Husband 25 add constraint FKAEEA401BD5255EDF 26 foreign key (wifeId) 27 references Wife 28 16:06:07,109 DEBUG SchemaExport:377 - 29 create sequence hibernate_sequence 30 16:06:07,109 INFO SchemaExport:268 - schema export complete
生成的表结构如下:
二、一对多单向关联【UserGroup-User】
User.java
View Code
1 package org.xiong.hibernate.model; 2 3 import javax.persistence.Entity; 4 import javax.persistence.GeneratedValue; 5 import javax.persistence.Id; 6 import javax.persistence.Table; 7 8 @Entity 9 @Table(name = "t_User") 10 public class User 11 { 12 private int id; 13 private String name; 14 15 public User() 16 { 17 18 } 19 20 public User(String name) 21 { 22 super(); 23 this.name = name; 24 } 25 26 @Id 27 @GeneratedValue 28 public int getId() 29 { 30 return id; 31 } 32 33 public void setId(int id) 34 { 35 this.id = id; 36 } 37 38 public String getName() 39 { 40 return name; 41 } 42 43 public void setName(String name) 44 { 45 this.name = name; 46 } 47 48 }
UserGroup.java
View Code
1 package org.xiong.hibernate.model; 2 3 import java.util.HashSet; 4 import java.util.Set; 5 6 import javax.persistence.CascadeType; 7 import javax.persistence.Entity; 8 import javax.persistence.GeneratedValue; 9 import javax.persistence.Id; 10 import javax.persistence.JoinColumn; 11 import javax.persistence.OneToMany; 12 13 @Entity 14 public class UserGroup 15 { 16 private int id; 17 private String name; 18 private Set<User> moreUsers=new HashSet<User>(); 19 20 @OneToMany(cascade=CascadeType.ALL) 21 @JoinColumn(name="groupId") //向多的一方(User)加外键,必须要指定外键的名称,否则会出现中间表 22 public Set<User> getMoreUsers() 23 { 24 return moreUsers; 25 } 26 27 public void setMoreUsers(Set<User> moreUsers) 28 { 29 this.moreUsers = moreUsers; 30 } 31 32 @Id 33 @GeneratedValue 34 public int getId() 35 { 36 return id; 37 } 38 39 public void setId(int id) 40 { 41 this.id = id; 42 } 43 44 public String getName() 45 { 46 return name; 47 } 48 49 public void setName(String name) 50 { 51 this.name = name; 52 } 53 54 }
生成的SQL语句如下:
View Code
1 16:17:23,625 INFO SchemaExport:226 - Running hbm2ddl schema export 2 16:17:23,625 DEBUG SchemaExport:242 - import file not found: /import.sql 3 16:17:23,625 INFO SchemaExport:251 - exporting generated schema to database 4 16:17:23,781 DEBUG SchemaExport:377 - 5 drop table UserGroup cascade constraints 6 16:17:23,812 DEBUG SchemaExport:377 - 7 drop table t_User cascade constraints 8 16:17:23,828 DEBUG SchemaExport:377 - 9 drop sequence hibernate_sequence 10 16:17:23,828 DEBUG SchemaExport:377 - 11 create table UserGroup ( 12 id number(10,0) not null, 13 name varchar2(255 char), 14 primary key (id) 15 ) 16 16:17:23,906 DEBUG SchemaExport:377 - 17 create table t_User ( 18 id number(10,0) not null, 19 name varchar2(255 char), 20 groupId number(10,0), 21 primary key (id) 22 ) 23 16:17:23,968 DEBUG SchemaExport:377 - 24 alter table t_User 25 add constraint FKCB5540D68698CAAC 26 foreign key (groupId) 27 references UserGroup 28 16:17:23,968 DEBUG SchemaExport:377 - 29 create sequence hibernate_sequence 30 16:17:23,968 INFO SchemaExport:268 - schema export complete
生成的表结构如下:
插入数据:
View Code
1 @Test 2 public void testUserGroupAdd() 3 { 4 Transaction transaction = null; 5 try 6 { 7 Session session = sessionFactory.getCurrentSession(); 8 transaction = session.beginTransaction(); 9 UserGroup group=new UserGroup(); 10 group.setName("组1"); 11 User u1=new User("张三"); 12 User u2=new User("李四"); 13 User u3=new User("王武"); 14 group.getMoreUsers().add(u1); 15 group.getMoreUsers().add(u2); 16 group.getMoreUsers().add(u3); 17 session.save(group); 18 19 transaction.commit(); 20 } 21 catch (Exception e) 22 { 23 e.printStackTrace(); 24 transaction.rollback(); 25 } 26 }
三、多对一单向关联【User-UserGroup】
User.java
View Code
1 package org.xiong.hibernate.model; 2 3 import javax.persistence.CascadeType; 4 import javax.persistence.Entity; 5 import javax.persistence.GeneratedValue; 6 import javax.persistence.Id; 7 import javax.persistence.JoinColumn; 8 import javax.persistence.ManyToOne; 9 import javax.persistence.Table; 10 11 @Entity 12 @Table(name = "t_User") 13 public class User 14 { 15 private int id; 16 private String name; 17 private UserGroup userGroup=new UserGroup(); 18 19 public User() 20 { 21 22 } 23 24 public User(String name) 25 { 26 super(); 27 this.name = name; 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 @ManyToOne(cascade=CascadeType.ALL) 43 @JoinColumn(name = "userGroupId") 44 public UserGroup getUserGroup() 45 { 46 return userGroup; 47 } 48 49 public void setUserGroup(UserGroup userGroup) 50 { 51 this.userGroup = userGroup; 52 } 53 54 public String getName() 55 { 56 return name; 57 } 58 59 public void setName(String name) 60 { 61 this.name = name; 62 } 63 64 }
UserGroup.java
View Code
1 package org.xiong.hibernate.model; 2 3 import javax.persistence.Entity; 4 import javax.persistence.GeneratedValue; 5 import javax.persistence.Id; 6 7 @Entity 8 public class UserGroup 9 { 10 private int id; 11 private String name; 12 13 @Id 14 @GeneratedValue 15 public int getId() 16 { 17 return id; 18 } 19 20 public void setId(int id) 21 { 22 this.id = id; 23 } 24 25 public String getName() 26 { 27 return name; 28 } 29 30 public void setName(String name) 31 { 32 this.name = name; 33 } 34 35 }
表结构与生成的SQL语句同二。
插入数据:
View Code
1 @Test 2 public void testUserAdd() 3 { 4 Transaction transaction = null; 5 try 6 { 7 Session session = sessionFactory.getCurrentSession(); 8 transaction = session.beginTransaction(); 9 User u1 = new User("张三"); 10 u1.getUserGroup().setName("组1"); 11 User u2 = new User("李四"); 12 u2.getUserGroup().setName("组1"); 13 User u3 = new User("王武"); 14 u3.getUserGroup().setName("组1"); 15 16 session.save(u1); 17 session.save(u2); 18 session.save(u3); 19 20 transaction.commit(); 21 } 22 catch (Exception e) 23 { 24 e.printStackTrace(); 25 transaction.rollback(); 26 } 27 }
四、一对多,多对一双向关联【User-UserGroup】
User.java
View Code
1 package org.xiong.hibernate.model; 2 3 import javax.persistence.CascadeType; 4 import javax.persistence.Entity; 5 import javax.persistence.GeneratedValue; 6 import javax.persistence.Id; 7 import javax.persistence.JoinColumn; 8 import javax.persistence.ManyToOne; 9 import javax.persistence.Table; 10 11 @Entity 12 @Table(name = "t_User") 13 public class User 14 { 15 private int id; 16 private String name; 17 private UserGroup userGroup; 18 19 public User() 20 { 21 22 } 23 24 public User(String name) 25 { 26 super(); 27 this.name = name; 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 @ManyToOne 43 @JoinColumn(name = "userGroupId") 44 public UserGroup getUserGroup() 45 { 46 return userGroup; 47 } 48 49 public void setUserGroup(UserGroup userGroup) 50 { 51 this.userGroup = userGroup; 52 } 53 54 public String getName() 55 { 56 return name; 57 } 58 59 public void setName(String name) 60 { 61 this.name = name; 62 } 63 64 }
UserGroup.java
View Code
1 package org.xiong.hibernate.model; 2 3 import java.util.HashSet; 4 import java.util.Set; 5 6 import javax.persistence.CascadeType; 7 import javax.persistence.Entity; 8 import javax.persistence.GeneratedValue; 9 import javax.persistence.Id; 10 import javax.persistence.JoinColumn; 11 import javax.persistence.OneToMany; 12 13 @Entity 14 public class UserGroup 15 { 16 private int id; 17 private String name; 18 private Set<User> moreUsers=new HashSet<User>(); 19 20 @OneToMany(mappedBy="userGroup",cascade=CascadeType.ALL) 21 public Set<User> getMoreUsers() 22 { 23 return moreUsers; 24 } 25 26 public void setMoreUsers(Set<User> moreUsers) 27 { 28 this.moreUsers = moreUsers; 29 } 30 31 @Id 32 @GeneratedValue 33 public int getId() 34 { 35 return id; 36 } 37 38 public void setId(int id) 39 { 40 this.id = id; 41 } 42 43 public String getName() 44 { 45 return name; 46 } 47 48 public void setName(String name) 49 { 50 this.name = name; 51 } 52 53 }
表结构与生成的SQL语句同二。
插入数据:
View Code
1 @Test 2 public void testUserGroupAdd() 3 { 4 Transaction transaction = null; 5 try 6 { 7 Session session = sessionFactory.getCurrentSession(); 8 transaction = session.beginTransaction(); 9 UserGroup group=new UserGroup(); 10 group.setName("组1"); 11 User u1=new User("张三"); 12 u1.setUserGroup(group); 13 User u2=new User("李四"); 14 u2.setUserGroup(group); 15 User u3=new User("王武"); 16 u3.setUserGroup(group); 17 group.getMoreUsers().add(u1); 18 group.getMoreUsers().add(u2); 19 group.getMoreUsers().add(u3); 20 session.save(group); 21 22 transaction.commit(); 23 } 24 catch (Exception e) 25 { 26 e.printStackTrace(); 27 transaction.rollback(); 28 } 29 }
五、多对多双向关联【Student-Teacher】
Student.java
View Code
1 package org.xiong.hibernate.model; 2 3 import java.util.Set; 4 5 import javax.persistence.Entity; 6 import javax.persistence.GeneratedValue; 7 import javax.persistence.Id; 8 import javax.persistence.ManyToMany; 9 10 @Entity 11 public class Student 12 { 13 private int id; 14 private String name; 15 private Set<Teacher> allTeacher; 16 17 public Student() 18 { 19 20 } 21 22 public Student(String name) 23 { 24 super(); 25 this.name = name; 26 } 27 28 @ManyToMany(mappedBy = "allStudent") 29 public Set<Teacher> getAllTeacher() 30 { 31 return allTeacher; 32 } 33 34 public void setAllTeacher(Set<Teacher> allTeacher) 35 { 36 this.allTeacher = allTeacher; 37 } 38 39 @Id 40 @GeneratedValue 41 public int getId() 42 { 43 return id; 44 } 45 46 public void setId(int id) 47 { 48 this.id = id; 49 } 50 51 public String getName() 52 { 53 return name; 54 } 55 56 public void setName(String name) 57 { 58 this.name = name; 59 } 60 61 }
Teacher.java
View Code
1 package org.xiong.hibernate.model; 2 3 import java.util.HashSet; 4 import java.util.Set; 5 6 import javax.persistence.CascadeType; 7 import javax.persistence.Entity; 8 import javax.persistence.GeneratedValue; 9 import javax.persistence.Id; 10 import javax.persistence.JoinColumn; 11 import javax.persistence.JoinTable; 12 import javax.persistence.ManyToMany; 13 14 @Entity 15 public class Teacher 16 { 17 private int id; 18 private String name; 19 private Set<Student> allStudent=new HashSet<Student>(); 20 21 @ManyToMany(cascade=CascadeType.ALL) 22 @JoinTable(name = "T_S_fk", joinColumns = { @JoinColumn(name = "tencher_idt") }, inverseJoinColumns = { @JoinColumn(name = "student_ids") }) 23 public Set<Student> getAllStudent() 24 { 25 return allStudent; 26 } 27 28 public void setAllStudent(Set<Student> allStudent) 29 { 30 this.allStudent = allStudent; 31 } 32 33 @Id 34 @GeneratedValue 35 public int getId() 36 { 37 return id; 38 } 39 40 public void setId(int id) 41 { 42 this.id = id; 43 } 44 45 public String getName() 46 { 47 return name; 48 } 49 50 public void setName(String name) 51 { 52 this.name = name; 53 } 54 }
生成的SQL语句如下:(多对多的关联需要生成中间表,通过@joinTable指定中间表的名称)
View Code
1 16:38:56,187 INFO SchemaExport:226 - Running hbm2ddl schema export 2 16:38:56,187 DEBUG SchemaExport:242 - import file not found: /import.sql 3 16:38:56,187 INFO SchemaExport:251 - exporting generated schema to database 4 16:38:56,343 DEBUG SchemaExport:377 - 5 drop table Student cascade constraints 6 16:38:56,390 DEBUG SchemaExport:377 - 7 drop table T_S_fk cascade constraints 8 16:38:56,515 DEBUG SchemaExport:377 - 9 drop table Teacher cascade constraints 10 16:38:56,515 DEBUG SchemaExport:377 - 11 drop sequence hibernate_sequence 12 16:38:56,531 DEBUG SchemaExport:377 - 13 create table Student ( 14 id number(10,0) not null, 15 name varchar2(255 char), 16 primary key (id) 17 ) 18 16:38:56,578 DEBUG SchemaExport:377 - 19 create table T_S_fk ( 20 tencher_idt number(10,0) not null, 21 student_ids number(10,0) not null, 22 primary key (tencher_idt, student_ids) 23 ) 24 16:38:56,625 DEBUG SchemaExport:377 - 25 create table Teacher ( 26 id number(10,0) not null, 27 name varchar2(255 char), 28 primary key (id) 29 ) 30 16:38:56,656 DEBUG SchemaExport:377 - 31 alter table T_S_fk 32 add constraint FK94B8F93CFF45A8A9 33 foreign key (tencher_idt) 34 references Teacher 35 16:38:56,671 DEBUG SchemaExport:377 - 36 alter table T_S_fk 37 add constraint FK94B8F93C87009DCD 38 foreign key (student_ids) 39 references Student 40 16:38:56,671 DEBUG SchemaExport:377 - 41 create sequence hibernate_sequence 42 16:38:56,671 INFO SchemaExport:268 - schema export complete
生成的表结构如下:
插入数据测试:
View Code
1 @Test 2 public void testTeacher_MTM_Add() 3 { 4 Transaction transaction=null; 5 try 6 { 7 Session session=sessionFactory.getCurrentSession(); 8 transaction=session.beginTransaction(); 9 Teacher t=new Teacher(); 10 t.setName("张三"); 11 t.getAllStudent().add(new Student("李四")); 12 t.getAllStudent().add(new Student("王武")); 13 t.getAllStudent().add(new Student("赵麻子")); 14 session.save(t); 15 transaction.commit(); 16 } 17 catch(Exception e) 18 { 19 e.printStackTrace(); 20 transaction.commit(); 21 } 22 }
六、补充
1,组件映射通过@Embedded,将另一个实体列表嵌入到一张表中。【Husband-Wife】
View Code
1 package org.xiong.hibernate.model; 2 3 import javax.persistence.Embedded; 4 import javax.persistence.Entity; 5 import javax.persistence.GeneratedValue; 6 import javax.persistence.Id; 7 import javax.persistence.JoinColumn; 8 import javax.persistence.OneToOne; 9 10 @Entity 11 public class Husband 12 { 13 private int id; 14 private String name; 15 private Wife wife=new Wife(); 16 17 @Embedded 18 public Wife getWife() 19 { 20 return wife; 21 } 22 23 public void setWife(Wife wife) 24 { 25 this.wife = wife; 26 } 27 28 @Id 29 @GeneratedValue 30 public int getId() 31 { 32 return id; 33 } 34 35 public void setId(int id) 36 { 37 this.id = id; 38 } 39 40 public String getName() 41 { 42 return name; 43 } 44 45 public void setName(String name) 46 { 47 this.name = name; 48 } 49 50 }
View Code
1 package org.xiong.hibernate.model; 2 3 public class Wife 4 { 5 private int wife_id; 6 private String wife_name; 7 private int age; 8 9 public int getWife_id() 10 { 11 return wife_id; 12 } 13 14 public void setWife_id(int wife_id) 15 { 16 this.wife_id = wife_id; 17 } 18 19 public String getWife_name() 20 { 21 return wife_name; 22 } 23 24 public void setWife_name(String wife_name) 25 { 26 this.wife_name = wife_name; 27 } 28 29 public int getAge() 30 { 31 return age; 32 } 33 34 public void setAge(int age) 35 { 36 this.age = age; 37 } 38 39 }
2,联合主键通过@JoinColumns指定联合主键的属性名称。【Husband-Wife】
View Code
1 package org.xiong.hibernate.model; 2 3 import javax.persistence.Entity; 4 import javax.persistence.GeneratedValue; 5 import javax.persistence.Id; 6 import javax.persistence.JoinColumn; 7 import javax.persistence.JoinColumns; 8 import javax.persistence.OneToOne; 9 import javax.persistence.PrimaryKeyJoinColumn; 10 11 @Entity 12 public class Husband 13 { 14 private int id; 15 private String name; 16 private Wife wife; 17 18 @OneToOne 19 @JoinColumns( 20 { 21 @JoinColumn(name="wifeId",referencedColumnName="id"), 22 @JoinColumn(name="wifeName",referencedColumnName="name") 23 } 24 ) 25 public Wife getWife() 26 { 27 return wife; 28 } 29 30 public void setWife(Wife wife) 31 { 32 this.wife = wife; 33 } 34 35 @Id 36 @GeneratedValue 37 public int getId() 38 { 39 return id; 40 } 41 42 public void setId(int id) 43 { 44 this.id = id; 45 } 46 47 public String getName() 48 { 49 return name; 50 } 51 52 public void setName(String name) 53 { 54 this.name = name; 55 } 56 57 }
View Code
1 package org.xiong.hibernate.model; 2 3 import javax.persistence.CascadeType; 4 import javax.persistence.EmbeddedId; 5 import javax.persistence.Entity; 6 import javax.persistence.OneToOne; 7 8 @Entity 9 public class Wife 10 { 11 private WifePK wifePK; 12 private int age; 13 private Husband husband=new Husband(); 14 15 @OneToOne(mappedBy = "wife", cascade = CascadeType.ALL) 16 public Husband getHusband() 17 { 18 return husband; 19 } 20 21 public void setHusband(Husband husband) 22 { 23 this.husband = husband; 24 } 25 26 @EmbeddedId 27 public WifePK getWifePK() 28 { 29 return wifePK; 30 } 31 32 public void setWifePK(WifePK wifePK) 33 { 34 this.wifePK = wifePK; 35 } 36 37 public int getAge() 38 { 39 return age; 40 } 41 42 public void setAge(int age) 43 { 44 this.age = age; 45 } 46 47 }
View Code
1 package org.xiong.hibernate.model; 2 3 import java.io.Serializable; 4 5 public class WifePK implements Serializable 6 { 7 /** 8 * 9 */ 10 private static final long serialVersionUID = 1L; 11 private int id; 12 private String name; 13 14 public int getId() 15 { 16 return id; 17 } 18 19 public void setId(int id) 20 { 21 this.id = id; 22 } 23 24 public String getName() 25 { 26 return name; 27 } 28 29 public void setName(String name) 30 { 31 this.name = name; 32 } 33 34 @Override 35 public int hashCode() 36 { 37 final int prime = 31; 38 int result = 1; 39 result = prime * result + id; 40 result = prime * result + ((name == null) ? 0 : name.hashCode()); 41 return result; 42 } 43 44 @Override 45 public boolean equals(Object obj) 46 { 47 if (this == obj) return true; 48 if (obj == null) return false; 49 if (getClass() != obj.getClass()) return false; 50 WifePK other = (WifePK) obj; 51 if (id != other.id) return false; 52 if (name == null) 53 { 54 if (other.name != null) return false; 55 } 56 else if (!name.equals(other.name)) return false; 57 return true; 58 } 59 60 }