一种商品类别下有多个商品,多个商品对应同一个商品类别,这种关系就是一对多双向关联。
商品类:
- 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