一、单向多对一和双向多对一的区别
如果只需要从一方获取另一方数据,就用单向多对一;如果需要从双方都获取对方数据,就用双向多对一。
如果有两个对象,一个为User对象,一个为Department对象,一个用户只能属于一个部门,而一个部门可以包含多个用户。这样就是多对一关系。如下图
假设:我们需要通过用户找到所对应的部门,不需要通过部门查询该部门有哪些用户,就采用单向多对一关系
如果:我们不仅需要通过用户获取所对应的部门,还需要通过部门对象获取该部门下的用户,那么就采用双向多对一
二、单向多对一关系
Department.java
1 package com.proc.pojo; 2 3 import java.io.Serializable; 4 5 public class Department implements Serializable { 6 7 private Integer id; 8 private String deptname; 9 10 //提供构造方法 11 public Department() { 12 } 13 14 public Department(String deptname) { 15 this.deptname = deptname; 16 } 17 18 //getter和setter实现 19 public Integer getId() { 20 return id; 21 } 22 public void setId(Integer id) { 23 this.id = id; 24 } 25 public String getDeptname() { 26 return deptname; 27 } 28 public void setDeptname(String deptname) { 29 this.deptname = deptname; 30 } 31 }
User.java
1 package com.proc.pojo; 2 3 import java.io.Serializable; 4 5 public class User implements Serializable { 6 7 private Integer id; 8 private String name; 9 private Department dept; 10 11 //提供构造方法 12 public User() { 13 } 14 public User(String name) { 15 this.name = name; 16 } 17 //getter和setter实现 18 public Integer getId() { 19 return id; 20 } 21 22 public void setId(Integer id) { 23 this.id = id; 24 } 25 public String getName() { 26 return name; 27 } 28 public void setName(String name) { 29 this.name = name; 30 } 31 public Department getDept() { 32 return dept; 33 } 34 public void setDept(Department dept) { 35 this.dept = dept; 36 } 37 }
Department.hbm.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.proc.pojo"> <class name="Department" table="department"> <id name="id" type="int"> <generator class="native"></generator> </id> <property name="deptname" length="20" not-null="true"></property> </class> </hibernate-mapping>
User.hbm.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 5 <hibernate-mapping package="com.proc.pojo"> 6 <class name="User"> 7 <id name="id" type="int"> 8 <generator class="native"></generator> 9 </id> 10 <property name="name" length="20" not-null="true"></property> 11 <many-to-one name="dept" column="deptid" class="Department" ></many-to-one> 12 </class> 13 </hibernate-mapping>
代码讲解:在配置User(多)->Department(一)时,采用外键映射,其中
name="dept":User对象dept属性
class="Department":表示该dept属性的类型是Department类型,因为User和Department在同一个包中,所以直接使用了类名称
column="deptid":指定在用user表中,对应department表的中主键的列为deptid
这里并没有指出user表中deptid对应的值department表中哪一列,默认为主键,这里也会自动给deptid列添加一个外键约束
测试代码:
1 package com.proc.test; 2 3 import org.hibernate.Session; 4 import org.hibernate.SessionFactory; 5 import org.hibernate.Transaction; 6 import org.hibernate.cfg.Configuration; 7 import org.junit.Test; 8 9 import com.proc.pojo.Department; 10 import com.proc.pojo.User; 11 12 public class TestOneToMany { 13 14 private static SessionFactory factory=new Configuration() 15 .configure() 16 .addClass(User.class) 17 .addClass(Department.class) 18 .buildSessionFactory(); 19 @Test 20 public void set(){ 21 22 Session session=factory.openSession(); 23 Transaction tran=session.beginTransaction(); 24 Department dept1=new Department("IT部"); 25 26 User user1=new User("caoyc"); 27 User user2=new User("zhh"); 28 29 user1.setDept(dept1); 30 user2.setDept(dept1); 31 32 session.save(dept1); 33 session.save(user1); 34 session.save(user2); 35 36 tran.commit(); 37 session.close(); 38 } 39 }
在控制台输出SQL语句:
Hibernate: create table department (id integer not null auto_increment, deptname varchar(20) not null, primary key (id))
Hibernate: create table User (id integer not null auto_increment, name varchar(20) not null, deptid integer, primary key (id))
Hibernate: alter table User add constraint FK66lqw6kl2gcxhs44gamc91fbd foreign key (deptid) references department (id)
Hibernate: insert into department (deptname) values (?)
Hibernate: insert into User (name, deptid) values (?, ?)
Hibernate: insert into User (name, deptid) values (?, ?)