Hibernate 對映關係 ---One2Many單向關聯
阿新 • • 發佈:2019-01-10
一個商品類別下對應著多個商品,在關聯關係裡,就是以集合的方式關聯,一般是Set集合
[java] view plain copy
- package com.pojo;
- import java.util.HashSet;
- import java.util.Set;
- /**
- *
- * 商品類別
- */
- public class Category implements java.io.Serializable {
- private static final long serialVersionUID = 4772491895582904188L;
- private
- private String name;
- private String mark;
- private Set<Product> product = new HashSet<Product>() ;
- public Category() {
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- 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;
- }
- public Set<Product> getProduct() {
- return product;
- }
- public void setProduct(Set<Product> product) {
- this.product = product;
- }
- }
[java] view plain copy
- package com.pojo;
- /**
- *
- *
- * 商品
- */
- public class Product implements java.io.Serializable {
- private static final long serialVersionUID = -4090151482387518292L;
- private int id;
- private String productname;
- private String remark;
- 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;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- }
在XML檔案裡面就是配置一對多的關聯了:
[xhtml] view plain copy
- <?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="product" >
- <key column="PRODUCTTYPEID" foreign-key="id"></key>
- <one-to-many class="com.pojo.Product" />
- </set>
- </class>
- </hibernate-mapping>
測試類:
[java] view plain copy
- package com.test;
- 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.loadPro() ;
- }
- 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.getProduct() ;
- set.add(p) ;
- set.add(p1) ;
- session.save(p) ;
- session.save(p1) ;
- try {
- session.save(p);
- HibernateManager.closeSession();
- } catch (Exception e) {
- e.printStackTrace();
- HibernateManager.rollbackTransaction();
- &nb