Hibernate 映射关系 ---One2Many 双向关联

一种商品类别下有多个商品,多个商品对应同一个商品类别,这种关系就是一对多双向关联。

 

商品类:

[java]  view plain copy
  1. package com.pojo;  
  2. /** 
  3.  * Product entity. 
  4.  *  
  5.  * @author MyEclipse Persistence Tools 
  6.  */  
  7. public class Product implements java.io.Serializable {  
  8.     private static final long serialVersionUID = -7963752710605063544L;  
  9.     private int id;  
  10.     private String productname;  
  11.     private String remark;  
  12.     // 多对一  
  13.     private Category category;  
  14.     public int getId() {  
  15.         return id;  
  16.     }  
  17.     public void setId(int id) {  
  18.         this.id = id;  
  19.     }  
  20.     public Category getCategory() {  
  21.         return category;  
  22.     }  
  23.     public void setCategory(Category category) {  
  24.         this.category = category;  
  25.     }  
  26.     public Product() {  
  27.     }  
  28.     public String getProductname() {  
  29.         return this.productname;  
  30.     }  
  31.     public void setProductname(String productname) {  
  32.         this.productname = productname;  
  33.     }  
  34.     public String getRemark() {  
  35.         return this.remark;  
  36.     }  
  37.     public void setRemark(String remark) {  
  38.         this.remark = remark;  
  39.     }  
  40. }  

 

商品类别:

[java]  view plain copy
  1. package com.pojo;  
  2. import java.util.HashSet;  
  3. import java.util.Set;  
  4. /** 
  5.  * Category entity. 
  6.  *  
  7.  * @author MyEclipse Persistence Tools 
  8.  */  
  9. public class Category implements java.io.Serializable {  
  10.     private static final long serialVersionUID = -666185106951167028L;  
  11.     private int id;  
  12.     private String name;  
  13.     private String mark;  
  14.     // 集合,一对多  
  15.     private Set<Product> products = new HashSet<Product>();  
  16.     public int getId() {  
  17.         return id;  
  18.     }  
  19.     public void setId(int id) {  
  20.         this.id = id;  
  21.     }  
  22.     public Set<Product> getProducts() {  
  23.         return products;  
  24.     }  
  25.     public void setProducts(Set<Product> products) {  
  26.         this.products = products;  
  27.     }  
  28.     public Category() {  
  29.     }  
  30.     public String getName() {  
  31.         return this.name;  
  32.     }  
  33.     public void setName(String name) {  
  34.         this.name = name;  
  35.     }  
  36.     public String getMark() {  
  37.         return this.mark;  
  38.     }  
  39.     public void setMark(String mark) {  
  40.         this.mark = mark;  
  41.     }  
  42. }  

 

XML配置文件,一对多的关系:

[xhtml]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  3. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  4. <!--  
  5.     Mapping file autogenerated by MyEclipse Persistence Tools 
  6. -->  
  7. <hibernate-mapping>  
  8.     <class name="com.pojo.Category" table="CATEGORY" schema="ZM">  
  9.         <id name="id" type="java.lang.Integer">  
  10.             <column name="ID" precision="8" scale="0" />  
  11.             <generator class="increment" />  
  12.         </id>  
  13.         <property name="name" type="java.lang.String">  
  14.             <column name="NAME" length="100" />  
  15.         </property>  
  16.         <property name="mark" type="java.lang.String">  
  17.             <column name="MARK" length="100" />  
  18.         </property>  
  19.           
  20.         <!-- 一对多,一种商品类型有多个商品 -->  
  21.         <set name="products" cascade="all" outer-join="true">  
  22.         <key column="producttypeid" foreign-key="id"></key>  
  23.         <one-to-many class="com.pojo.Product" />  
  24.         </set>  
  25.           
  26.     </class>  
  27. </hibernate-mapping>  

 

多个商品对应一个商品类别:

 

[xhtml]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  3. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  4. <!--  
  5.     Mapping file autogenerated by MyEclipse Persistence Tools 
  6. -->  
  7. <hibernate-mapping>  
  8.     <class name="com.pojo.Product" table="PRODUCT" schema="ZM">  
  9.         <id name="id" type="java.lang.Integer">  
  10.             <column name="ID" precision="8" scale="0" />  
  11.             <generator class="increment" />  
  12.         </id>  
  13.         <property name="productname" type="java.lang.String">  
  14.             <column name="PRODUCTNAME" length="100" />  
  15.         </property>  
  16.         <property name="remark" type="java.lang.String">  
  17.             <column name="REMARK" length="100" />  
  18.         </property>  
  19.         <!-- 多对一,多个商品对应一个分类 -->  
  20.         <many-to-one name="category" class="com.pojo.Category" outer-join="true" >  
  21.         <column name="producttypeid"></column>          
  22.         </many-to-one>  
  23.     </class>  
  24. </hibernate-mapping>  

 

 

测试类:

 

[java]  view plain copy
  1. package com.test;  
  2. import java.util.List;  
  3. import java.util.Set;  
  4. import org.hibernate.Session;  
  5. import com.pojo.Category;  
  6. import com.pojo.Product;  
  7. import com.util.HibernateManager;  
  8. public class Test1 {  
  9.     /** 
  10.      * beckham Dec 14, 2009 7:30:06 PM 
  11.      */  
  12.     public static void main(String[] args) {  
  13.         Test1.delete();  
  14.     }  
  15.     public static void addType() {  
  16.         Session session = HibernateManager.openSession();  
  17.         Category c = new Category();  
  18.         c.setName("电脑");  
  19.         c.setMark("这是电脑类别");  
  20.         try {  
  21.             session.save(c);  
  22.             HibernateManager.closeSession();  
  23.         } catch (Exception e) {  
  24.             e.printStackTrace();  
  25.             HibernateManager.rollbackTransaction();  
  26.         }  
  27.     }  
  28.     public static void addPro() {  
  29.         Session session = HibernateManager.openSession();  
  30.         // 获取类型  
  31.         Category c = (Category) session.load(Category.classnew Integer(1));  
  32.         // 商品1  
  33.         Product p = new Product();  
  34.         p.setProductname("摩托罗拉v8");  
  35.         p.setRemark("很好的手机");  
  36.         // 商品2  
  37.         Product p1 = new Product();  
  38.         p1.setProductname("摩托罗拉A1200");  
  39.         p1.setRemark("非常好的手机");  
  40.         // 一对多关联  
  41.         Set<Product> set = c.getProducts();  
  42.         set.add(p);  
  43.         set.add(p1);  
  44.         // 直接保存商品类别,商品自动级联就保存  
  45.         session.save(c);  
  46.         try {  
  47.             HibernateManager.closeSession();  
  48.         } catch (Exception e) {  
  49.             e.printStackTrace();  
  50.             HibernateManager.rollbackTransaction();  
  51.         }  
  52.     }  
  53.     public static void loadPro() {  
  54.         Session session = HibernateManager.openSession();  
  55.         Product p = (Product) session.load(Product.classnew Integer(1));  
  56.         System.out.println("类别编号:" + p.getCategory().getId());  
  57.         System.out.println("类别名称:" + p.getCategory().getName());  
  58.         System.out.println("类别描述:" + p.getCategory().getMark());  
  59.         System.out.println("商品编号" + p.getId());  
  60.         System.out.println("商品名称:" + p.getProductname());  
  61.         System.out.println("商品描述:" + p.getRemark());  
  62.         try {  
  63.             HibernateManager.closeSession();  
  64.         } catch (Exception e) {  
  65.             e.printStackTrace();  
  66.             HibernateManager.rollbackTransaction();  
  67.         }  
  68.     }  
  69.     @SuppressWarnings("unchecked")  
  70.     public static void loadProList() {  
  71.         Session session = HibernateManager.openSession();  
  72.         // 先获取类型  
  73.         List<Product> list = session.createQuery("from Product").list();  
  74.         for (Product p : list) {  
  75.             System.out.println("类别编号:" + p.getCategory().getId());  
  76.             System.out.println("类别名称:" + p.getCategory().getName());  
  77.             System.out.println("类别描述:" + p.getCategory().getMark());  
  78.             System.out.println("商品编号" + p.getId());  
  79.             System.out.println("商品名称:" + p.getProductname());  
  80.             System.out.println("商品描述:" + p.getRemark());  
  81.             System.out.println("<br/>");  
  82.         }  
  83.         try {  
  84.             HibernateManager.closeSession();  
  85.         } catch (Exception e) {  
  86.             e.printStackTrace();  
  87.             HibernateManager.rollbackTransaction();  
  88.         }  
  89.     }  
  90.     // 修改商品类型,级联操作  
  91.     public static void updateCate() {  
  92.         Session session = HibernateManager.openSession();  
  93.         Category c = (Category) session.load(Category.classnew Integer(1));  
  94.         Set<Product> products = c.getProducts();  
  95.         for (Product p : products) {  
  96.             if (p.getId() == 1) {  
  97.                 p.setProductname("诺基亚6300");  
  98.                 p.setRemark("very good");  
  99.             }  
  100.         }  
  101.         session.update(c);  
  102.         try {  
  103.             HibernateManager.closeSession();  
  104.         } catch (Exception e) {  
  105.             e.printStackTrace();  
  106.             HibernateManager.rollbackTransaction();  
  107.         }  
  108.     }  
  109.     // 修改商品类型,级联操作  
  110.     public static void updatePro() {  
  111.         Session session = HibernateManager.openSession();  
  112.         Product p = (Product) session.load(Product.classnew Integer(1));  
  113.         Category c = p.getCategory();  
  114.         p.setProductname("三星");  
  115.         p.setRemark("很实用的手机");  
  116.         session.update(c);  
  117.         try {  
  118.             HibernateManager.closeSession();  
  119.         } catch (Exception e) {  
  120.             e.printStackTrace();  
  121.             HibernateManager.rollbackTransaction();  
  122.         }  
  123.     }  
  124.     // 删除某一类别,并级联删除该类别下面的所有商品  
  125.     public static void delete() {  
  126.         Session session = HibernateManager.openSession();  
  127.         Category c = (Category) session.load(Category.classnew Integer(1));  
  128.         //级联删除  
  129.         session.delete(c);  
  130.         try {  
  131.             HibernateManager.closeSession();  
  132.         } catch (Exception e) {  
  133.             e.printStackTrace();  
  134.             HibernateManager.rollbackTransaction();  
  135.         }  
  136.     }  
  137. }  




本文出自 :http://blog.csdn.net/gaowenming/article/details/5008665


猜你喜欢

转载自blog.csdn.net/qq_28059559/article/details/49979185