笔记45 Hibernate快速入门(二)
Hibernate O/R 映射
一、多对一
一个Product对应一个Category,一个Category对应多个Product,所以Product和Category是多对一的关系。使用hibernate实现多对一。
1.准备Category类
1 package hibernate.pojo; 2 3 import java.util.Set; 4 5 public class Category { 6 int id; 7 String name; 8 9 public int getId() { 10 return id; 11 } 12 13 public void setId(int id) { 14 this.id = id; 15 } 16 17 public String getName() { 18 return name; 19 } 20 21 public void setName(String name) { 22 this.name = name; 23 } 24 25 }
2.准备Category.hbm.xml
1 <?xml version="1.0"?> 2 3 <!DOCTYPE hibernate-mapping PUBLIC 4 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 5 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 6 <hibernate-mapping package="hibernate.pojo"> 7 <class name="Category" table="category"> 8 <!-- <cache usage="read-only"/> --> 9 <id name="id" column="id"> 10 <generator class="native"> 11 </generator> 12 </id> 13 <property name="name" /> 14 </class> 15 </hibernate-mapping>
3.为Product.java增加Category属性
1 Category category; 2 public Category getCategory() { 3 return category; 4 } 5 6 public void setCategory(Category category) { 7 this.category = category; 8 }
4.在Product.hbm.xml中设置Category多对一关系
1 <?xml version="1.0"?> 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 6 <hibernate-mapping package="hibernate.pojo"> 7 <class name="Product" table="product"> 8 <id name="id" column="id"> 9 <generator class="native"> 10 </generator> 11 </id> 12 <property name="name" /> 13 <property name="price" /> 14 <many-to-one name="category" class="Category" column="cid"></many-to-one> 15 </class> 16 17 </hibernate-mapping>
使用many-to-one 标签设置多对一关系
name="category" 对应Product类中的category属性
class="Category" 表示对应Category类
column="cid" 表示指向 category_表的外键
5.在hibernate.cfg.xml中增加Category的映射
1 <mapping resource="hibernate/pojo/Category.hbm.xml" />
6.测试
在这个测试例子中,增加了五个新的Category对象,然后对商品进行随机分类。
1 package hibernate.test; 2 3 import java.util.List; 4 import java.util.Random; 5 6 import org.hibernate.Query; 7 import org.hibernate.Session; 8 import org.hibernate.SessionFactory; 9 import org.hibernate.cfg.Configuration; 10 11 import hibernate.pojo.Category; 12 import hibernate.pojo.Product; 13 14 public class testManyToOne { 15 16 public static void main(String[] args) { 17 // TODO Auto-generated method stub 18 19 SessionFactory sFactory = new Configuration().configure().buildSessionFactory(); 20 Session session = sFactory.openSession(); 21 session.beginTransaction(); 22 23 for (int i = 1; i <= 5; i++) { 24 Category category = new Category(); 25 category.setName("C" + i); 26 session.save(category); 27 } 28 29 Query query2 = session.createQuery("from Category"); 30 List<Category> categorys = query2.list(); 31 32 for (int i = 1; i <= 10; i++) { 33 Random random = new Random(); 34 Product product = (Product) session.get(Product.class, i); 35 product.setCategory(categorys.get(random.nextInt(5))); 36 session.update(product); 37 } 38 39 session.getTransaction().commit(); 40 session.close(); 41 sFactory.close(); 42 } 43 44 }
二、一对多
一个Product对应一个Category,一个Category对应多个Product,所以Category和Product是一对多的关系。
1.为Category增加一个Set集合
1 package hibernate.pojo; 2 3 import java.util.Set; 4 5 public class Category { 6 int id; 7 String name; 8 Set<Product> products; 9 10 public int getId() { 11 return id; 12 } 13 14 public void setId(int id) { 15 this.id = id; 16 } 17 18 public String getName() { 19 return name; 20 } 21 22 public void setName(String name) { 23 this.name = name; 24 } 25 26 public Set<Product> getProducts() { 27 return products; 28 } 29 30 public void setProducts(Set<Product> products) { 31 this.products = products; 32 } 33 34 }
2.为Category.hbm.xml增加one-to-many映射
1 <?xml version="1.0"?> 2 3 <!DOCTYPE hibernate-mapping PUBLIC 4 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 5 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 6 <hibernate-mapping package="hibernate.pojo"> 7 <class name="Category" table="category"> 8 <!-- <cache usage="read-only"/> --> 9 <id name="id" column="id"> 10 <generator class="native"> 11 </generator> 12 </id> 13 <property name="name" /> 14 <set name="products" lazy="true"> 15 <key column="cid" not-null="false"></key> 16 <one-to-many class="Product" /> 17 </set> 18 </class> 19 20 </hibernate-mapping>
3.测试
1 package hibernate.test; 2 3 import java.util.List; 4 5 import org.hibernate.Query; 6 import org.hibernate.Session; 7 import org.hibernate.SessionFactory; 8 import org.hibernate.cfg.Configuration; 9 10 import hibernate.pojo.Category; 11 import hibernate.pojo.Product; 12 13 public class testOneToMany { 14 15 public static void main(String[] args) { 16 // TODO Auto-generated method stub 17 18 SessionFactory sFactory = new Configuration().configure().buildSessionFactory(); 19 Session session = sFactory.openSession(); 20 session.beginTransaction(); 21 22 Query query = session.createQuery("from Category"); 23 List<Category> categories = query.list(); 24 for (Category c : categories) { 25 System.out.println("属于" + c.getName() + "类的商品有:"); 26 for (Product p : c.getProducts()) { 27 System.out.println(p.getName()); 28 } 29 } 30 31 session.getTransaction().commit(); 32 session.close(); 33 sFactory.close(); 34 } 35 36 }
三、多对多
一种Product可以被多个User购买 一个User可以购买多种Product 所以Product和User之间的关系是多对多 many-to-many 要实现多对多关系,必须有一张中间表 user_product 用于维护 User和Product之间的关系。
1.User.java
1 package hibernate.pojo; 2 3 import java.util.Set; 4 5 public class User { 6 int id; 7 String name; 8 Set<Product> products; 9 10 public int getId() { 11 return id; 12 } 13 14 public void setId(int id) { 15 this.id = id; 16 } 17 18 public String getName() { 19 return name; 20 } 21 22 public void setName(String name) { 23 this.name = name; 24 } 25 26 public Set<Product> getProducts() { 27 return products; 28 } 29 30 public void setProducts(Set<Product> products) { 31 this.products = products; 32 } 33 34 }
2.User.hbm.xml
1 <?xml version="1.0"?> 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 6 <hibernate-mapping package="hibernate.pojo"> 7 <class name="User" table="user"> 8 <id name="id" column="id"> 9 <generator class="native"> 10 </generator> 11 </id> 12 <property name="name" /> 13 14 <set name="products" table="user_product" lazy="false"> 15 <key column="uid" /> 16 <many-to-many column="pid" class="Product" /> 17 </set> 18 19 </class> 20 21 </hibernate-mapping>
3.Product.java
1 Set<User> users; 2 public Set<User> getUsers() { 3 return users; 4 } 5 6 public void setUsers(Set<User> users) { 7 this.users = users; 8 }
4.Product.hbm.xml
1 <?xml version="1.0"?> 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 6 <hibernate-mapping package="hibernate.pojo"> 7 <class name="Product" table="product"> 8 <id name="id" column="id"> 9 <generator class="native"> 10 </generator> 11 </id> 12 <!--version元素必须紧挨着id后面 --> 13 <version name="version" column="ver" type="int"></version> 14 <property name="name" /> 15 <property name="price" /> 16 <many-to-one name="category" class="Category" column="cid"></many-to-one> 17 <set name="users" table="user_product" lazy="false" > 18 <key column="pid"></key> 19 <many-to-many column="uid" class="User"></many-to-many> 20 </set> 21 </class> 22 23 </hibernate-mapping>
5.在hibernate中增加User的映射
1 <mapping resource="hibernate/pojo/User.hbm.xml" />
6.测试
首先添加三个用户,然后实现一件商品多人购买和一个人购买多件商品(再新增用户“user3”)两个功能。
1 package hibernate.test; 2 3 import java.util.HashSet; 4 import java.util.List; 5 import java.util.Set; 6 7 import org.hibernate.Query; 8 import org.hibernate.Session; 9 import org.hibernate.SessionFactory; 10 import org.hibernate.cfg.Configuration; 11 12 import hibernate.pojo.Product; 13 import hibernate.pojo.User; 14 15 public class testManyToMany { 16 17 public static void main(String[] args) { 18 // TODO Auto-generated method stub 19 20 SessionFactory sFactory = new Configuration().configure().buildSessionFactory(); 21 Session session = sFactory.openSession(); 22 session.beginTransaction(); 23 // 添加三个用户 24 Set<User> users = new HashSet<User>(); 25 for (int i = 0; i < 3; i++) { 26 User user = new User(); 27 user.setName("user" + i); 28 users.add(user); 29 session.save(user); 30 } 31 // 一件商品多个人购买 32 Product p1 = (Product) session.get(Product.class, 2); 33 p1.setUsers(users); 34 session.save(p1); 35 36 // 一个人购买多个商品 37 // 获取商品列表 38 39 User user = new User(); 40 user.setName("user3"); 41 session.save(user); 42 43 Query query = session.createQuery("from Product"); 44 List<Product> products = query.list(); 45 Set<Product> products2 = new HashSet<Product>(); 46 for (int i = 0; i < products.size(); i++) { 47 products2.add(products.get(i)); 48 } 49 User u1 = (User) session.get(User.class, 4); 50 u1.setProducts(products2); 51 session.save(u1); 52 53 session.getTransaction().commit(); 54 session.close(); 55 sFactory.close(); 56 } 57 58 }