一種商品類別下有多個商品,多個商品對應同一個商品類別,這種關係就是一對多雙向關聯。
商品類:
- package com.pojo;
- /**
- * Product entity.
- *
- * @author MyEclipse Persistence Tools
- */
- public class Product implements java.io.Serializable {
- private static final long serialVersionUID = -7963752710605063544L;
- private int id;
- private String productname;
- private String remark;
- // 多對一
- private Category category;
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public Category getCategory() {
- return category;
- }
- public void setCategory(Category category) {
- this.category = category;
- }
- public Product() {
- }
- public String getProductname() {
- return this.productname;
- }
- public void setProductname(String productname) {
- this.productname = productname;
- }
- public String getRemark() {
- return this.remark;
- }
- public void setRemark(String remark) {
- this.remark = remark;
- }
- }
商品類別:
- package com.pojo;
- import java.util.HashSet;
- import java.util.Set;
- /**
- * Category entity.
- *
- * @author MyEclipse Persistence Tools
- */
- public class Category implements java.io.Serializable {
- private static final long serialVersionUID = -666185106951167028L;
- private int id;
- private String name;
- private String mark;
- // 集合,一對多
- private Set<Product> products = new HashSet<Product>();
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public Set<Product> getProducts() {
- return products;
- }
- public void setProducts(Set<Product> products) {
- this.products = products;
- }
- public Category() {
- }
- public String getName() {
- return this.name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getMark() {
- return this.mark;
- }
- public void setMark(String mark) {
- this.mark = mark;
- }
- }
XML配置文件,一對多的關係:
- <?xml version="1.0" encoding="utf-8"?>
- <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <!--
- Mapping file autogenerated by MyEclipse Persistence Tools
- -->
- <hibernate-mapping>
- <class name="com.pojo.Category" table="CATEGORY" schema="ZM">
- <id name="id" type="java.lang.Integer">
- <column name="ID" precision="8" scale="0" />
- <generator class="increment" />
- </id>
- <property name="name" type="java.lang.String">
- <column name="NAME" length="100" />
- </property>
- <property name="mark" type="java.lang.String">
- <column name="MARK" length="100" />
- </property>
- <!-- 一對多,一種商品類型有多個商品 -->
- <set name="products" cascade="all" outer-join="true">
- <key column="producttypeid" foreign-key="id"></key>
- <one-to-many class="com.pojo.Product" />
- </set>
- </class>
- </hibernate-mapping>
多個商品對應一個商品類別:
- <?xml version="1.0" encoding="utf-8"?>
- <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <!--
- Mapping file autogenerated by MyEclipse Persistence Tools
- -->
- <hibernate-mapping>
- <class name="com.pojo.Product" table="PRODUCT" schema="ZM">
- <id name="id" type="java.lang.Integer">
- <column name="ID" precision="8" scale="0" />
- <generator class="increment" />
- </id>
- <property name="productname" type="java.lang.String">
- <column name="PRODUCTNAME" length="100" />
- </property>
- <property name="remark" type="java.lang.String">
- <column name="REMARK" length="100" />
- </property>
- <!-- 多對一,多個商品對應一個分類 -->
- <many-to-one name="category" class="com.pojo.Category" outer-join="true" >
- <column name="producttypeid"></column>
- </many-to-one>
- </class>
- </hibernate-mapping>
測試類:
- package com.test;
- import java.util.List;
- import java.util.Set;
- import org.hibernate.Session;
- import com.pojo.Category;
- import com.pojo.Product;
- import com.util.HibernateManager;
- public class Test1 {
- /**
- * beckham Dec 14, 2009 7:30:06 PM
- */
- public static void main(String[] args) {
- Test1.delete();
- }
- public static void addType() {
- Session session = HibernateManager.openSession();
- Category c = new Category();
- c.setName("電腦");
- c.setMark("這是電腦類別");
- try {
- session.save(c);
- HibernateManager.closeSession();
- } catch (Exception e) {
- e.printStackTrace();
- HibernateManager.rollbackTransaction();
- }
- }
- public static void addPro() {
- Session session = HibernateManager.openSession();
- // 獲取類型
- Category c = (Category) session.load(Category.class, new Integer(1));
- // 商品1
- Product p = new Product();
- p.setProductname("摩托羅拉v8");
- p.setRemark("很好的手機");
- // 商品2
- Product p1 = new Product();
- p1.setProductname("摩托羅拉A1200");
- p1.setRemark("非常好的手機");
- // 一對多關聯
- Set<Product> set = c.getProducts();
- set.add(p);
- set.add(p1);
- // 直接保存商品類別,商品自動級聯就保存
- session.save(c);
- try {
- HibernateManager.closeSession();
- } catch (Exception e) {
- e.printStackTrace();
- HibernateManager.rollbackTransaction();
- }
- }
- public static void loadPro() {
- Session session = HibernateManager.openSession();
- Product p = (Product) session.load(Product.class, new Integer(1));
- System.out.println("類別編號:" + p.getCategory().getId());
- System.out.println("類別名稱:" + p.getCategory().getName());
- System.out.println("類別描述:" + p.getCategory().getMark());
- System.out.println("商品編號" + p.getId());
- System.out.println("商品名稱:" + p.getProductname());
- System.out.println("商品描述:" + p.getRemark());
- try {
- HibernateManager.closeSession();
- } catch (Exception e) {
- e.printStackTrace();
- HibernateManager.rollbackTransaction();
- }
- }
- @SuppressWarnings("unchecked")
- public static void loadProList() {
- Session session = HibernateManager.openSession();
- // 先獲取類型
- List<Product> list = session.createQuery("from Product").list();
- for (Product p : list) {
- System.out.println("類別編號:" + p.getCategory().getId());
- System.out.println("類別名稱:" + p.getCategory().getName());
- System.out.println("類別描述:" + p.getCategory().getMark());
- System.out.println("商品編號" + p.getId());
- System.out.println("商品名稱:" + p.getProductname());
- System.out.println("商品描述:" + p.getRemark());
- System.out.println("<br/>");
- }
- try {
- HibernateManager.closeSession();
- } catch (Exception e) {
- e.printStackTrace();
- HibernateManager.rollbackTransaction();
- }
- }
- // 修改商品類型,級聯操作
- public static void updateCate() {
- Session session = HibernateManager.openSession();
- Category c = (Category) session.load(Category.class, new Integer(1));
- Set<Product> products = c.getProducts();
- for (Product p : products) {
- if (p.getId() == 1) {
- p.setProductname("諾基亞6300");
- p.setRemark("very good");
- }
- }
- session.update(c);
- try {
- HibernateManager.closeSession();
- } catch (Exception e) {
- e.printStackTrace();
- HibernateManager.rollbackTransaction();
- }
- }
- // 修改商品類型,級聯操作
- public static void updatePro() {
- Session session = HibernateManager.openSession();
- Product p = (Product) session.load(Product.class, new Integer(1));
- Category c = p.getCategory();
- p.setProductname("三星");
- p.setRemark("很實用的手機");
- session.update(c);
- try {
- HibernateManager.closeSession();
- } catch (Exception e) {
- e.printStackTrace();
- HibernateManager.rollbackTransaction();
- }
- }
- // 刪除某一類別,並級聯刪除該類別下面的所有商品
- public static void delete() {
- Session session = HibernateManager.openSession();
- Category c = (Category) session.load(Category.class, new Integer(1));
- //級聯刪除
- session.delete(c);
- try {
- HibernateManager.closeSession();
- } catch (Exception e) {
- e.printStackTrace();
- HibernateManager.rollbackTransaction();
- }
- }
- }
本文出自 :http://blog.csdn.net/gaowenming/article/details/5008665