前面的咱们使用的是一个表的操做,但咱们实际的开发中不可能只使用一个表的…所以,本博文主要讲解关联映射java
需求分析:当用户购买商品,用户可能有多个地址。数据库
咱们通常以下图同样设计数据库表,通常咱们不会在User表设计多个列来保存地址的。由于每一个用户的地址个数都不一的,会形成数据冗余markdown
因为地址只是使用String类型来保存着,那么咱们直接使用一个User对象就能够了session
public class User { private String id; private String username; private String password; private Set<String> address; //各类setter和getter
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <!--在domain包下--> <hibernate-mapping package="zhongfucheng.domain"> <class name="User" table="user"> <!--主键映射--> <id name="id" column="id" > <generator class="native"/> </id> <!--普通字段映射--> <property name="username" column="username"></property> <property name="password" column="password"></property> <!-- Set: name: 映射集合的名称 table:集合的属性要映射到哪张表(address) key: column:指定要映射的表(address)中的外键列 element:要映射的表的其余字段 类型必定要指定! --> <set name="address" table="address"> <key column="user_id"></key> <element column="addr" type="string"></element> </set> </class> </hibernate-mapping>
package zhongfucheng.domain; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.classic.Session; /** * Created by ozc on 2017/5/6. */ public class App { public static void main(String[] args) { //建立对象 User user = new User(); user.setUsername("123"); user.setPassword("1234"); user.getAddress().add("广州"); //获取加载配置管理类 Configuration configuration = new Configuration(); //加载User的映射文件! configuration.configure().addClass(User.class); //建立Session工厂对象 SessionFactory factory = configuration.buildSessionFactory(); //获得Session对象 Session session = factory.openSession(); //使用Hibernate操做数据库,都要开启事务,获得事务对象 Transaction transaction = session.getTransaction(); //开启事务 transaction.begin(); session.save(user); //提交事务 transaction.commit(); //关闭Session session.close(); } }
既然咱们如今已经会了如何配置Set集合了,List集合又怎么配置呢??app
想一下,List集合和Set集合有什么区别…List集合是有序的,所以要多配置一个列来维护数据的有序性!dom
<list name="address" table="address"> <key column="user_id"></key> <!--index是关键字,不能使用!!!!--> <list-index column="index"></list-index> <element column="addr" type="string"></element> </list>
Map集合和Collection集合的区别就是键值对模型,那么在配置的时候多一个key便可!测试
<map name="address" table="address"> <key column="user_id" ></key> <map-key type="string" column="short"></map-key> <element type="string" column="addr"></element> </map>
上面咱们讲解了集合映射是怎么配置的,那集合装载的元素有没有多是对象呢??而不是简单的String类型..那个就太多了!通常地,咱们集合装载的都是对象,而不是简单的String,若是咱们的装载在集合的数据有不少类型,那么String就不能用了!…ui
需求:部门与员工之间的关系this
员工表应该使用一个外键来记住部门表。这样才能够维护员工和部门之间的关系spa
部门实体要使用一个集合来记住全部的员工,员工要使用一个对象引用着部门
package zhongfucheng.domain; import java.util.HashSet; import java.util.Set; /** * Created by ozc on 2017/5/6. */ public class Dept { private int id ; private Set<Employee> set = new HashSet<>(); private String deptName; public String getDeptName() { return deptName; } public void setDeptName(String deptName) { this.deptName = deptName; } public int getId() { return id; } public void setId(int id) { this.id = id; } public Set<Employee> getSet() { return set; } public void setSet(Set<Employee> set) { this.set = set; } }
package zhongfucheng.domain; /** * Created by ozc on 2017/5/6. */ public class Employee { private int id; private String empName; private double salary; private Dept dept; public Dept getDept() { return dept; } public void setDept(Dept dept) { this.dept = dept; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getEmpName() { return empName; } public void setEmpName(String empName) { this.empName = empName; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } }
咱们在写映射配置文件以前,分析一下怎么写。以部门映射配置文件为例…
如今使用了一个Set集合来维护与员工的关系,Set集合的类型是员工对象…所以在映射文件中须要如下几点
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <!--在domain包下--> <hibernate-mapping package="zhongfucheng.domain"> <class name="Dept" table="dept"> <id column="id" name="id"> <generator class="native"> </generator> </id> <!--普通字段映射--> <property name="deptName" column="deptname"></property> <!--维护关系的是Set集合,对应employee表--> <set cascade="save-update" name="set" table="employee"> <!--employee的外键列是dept_no--> <key column="dept_no"></key> <!--一个部门对应多个员工,集合的类型是Employee--> <one-to-many class="Employee" ></one-to-many> </set> </class> </hibernate-mapping>
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <!--在domain包下--> <hibernate-mapping package="zhongfucheng.domain"> <class name="Employee" table="employee"> <id column="id" name="id"> <generator class="native"> </generator> </id> <!--普通字段数据--> <property name="empName" column="empName"></property> <property name="salary" column="salary"></property> <!--Hibernate这个标签可当作在当前表中设置一个外键dept_no--> <many-to-one name="dept" class="Dept" column="dept_no"></many-to-one> </class> </hibernate-mapping>
package zhongfucheng.domain; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.classic.Session; /** * Created by ozc on 2017/5/6. */ public class App { public static void main(String[] args) { //建立对象 Dept dept = new Dept(); dept.setDeptName("开发部"); Employee zs = new Employee(); zs.setEmpName("张珊"); zs.setSalary(1111); Employee ls = new Employee(); ls.setEmpName("李四"); ls.setSalary(2222); //添加关系 dept.getSet().add(zs); dept.getSet().add(ls); //获取加载配置管理类 Configuration configuration = new Configuration(); //加载User的映射文件! configuration.configure().addClass(Dept.class).addClass(Employee.class); //建立Session工厂对象 SessionFactory factory = configuration.buildSessionFactory(); //获得Session对象 Session session = factory.openSession(); //使用Hibernate操做数据库,都要开启事务,获得事务对象 Transaction transaction = session.getTransaction(); //开启事务 transaction.begin(); session.save(dept); session.save(zs); session.save(ls); //提交事务 transaction.commit(); //关闭Session session.close(); } }
Hibernate执行了5条SQL语句
package zhongfucheng.domain; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.classic.Session; /** * Created by ozc on 2017/5/6. */ public class App { public static void main(String[] args) { //建立对象 Dept dept = new Dept(); dept.setDeptName("开发部"); Employee zs = new Employee(); zs.setEmpName("张珊"); zs.setSalary(1111); Employee ls = new Employee(); ls.setEmpName("李四"); ls.setSalary(2222); //维护关系 zs.setDept(dept); ls.setDept(dept); //获取加载配置管理类 Configuration configuration = new Configuration(); //加载User的映射文件! configuration.configure().addClass(Dept.class).addClass(Employee.class); //建立Session工厂对象 SessionFactory factory = configuration.buildSessionFactory(); //获得Session对象 Session session = factory.openSession(); //使用Hibernate操做数据库,都要开启事务,获得事务对象 Transaction transaction = session.getTransaction(); //开启事务 transaction.begin(); session.save(dept); session.save(zs); session.save(ls); //提交事务 transaction.commit(); //关闭Session session.close(); } }
Hibernate执行了3条SQL
在一对多与多对一的关联关系中,保存数据最好的经过多的一方来维护关系,这样能够减小update语句的生成,从而提升hibernate的执行效率!
值得注意是:配置了哪一方,哪一方才有维护关联关系的权限!