RSS

映射多对多关联关系

05 Apr

多对多单向关联

使用多对多关联,需要借助一个起中介作用的连接表来完成。一个多对多关联通常可以拆分成两个一对多关联。

不需要提供外键,只需要提供两个主表。

Order.java

package hibernate.example.vo;

import java.util.HashSet;

import java.util.Set;

public class Order {

    private Integer id;

    private String realname;

    private Double total;

    private String address;

    private String postcode;

    private String phone;

    private Set<Product5> products = new HashSet<Product5>();

   

    public Order(){

       

    }

 

    public Integer getId() {

        return id;

    }

 

    public void setId(Integer id) {

        this.id = id;

    }

 

    public String getRealname() {

        return realname;

    }

 

    public void setRealname(String realname) {

        this.realname = realname;

    }

 

    public Double getTotal() {

        return total;

    }

 

    public void setTotal(Double total) {

        this.total = total;

    }

 

    public String getAddress() {

        return address;

    }

 

    public void setAddress(String address) {

        this.address = address;

    }

 

    public String getPostcode() {

        return postcode;

    }

 

    public void setPostcode(String postcode) {

        this.postcode = postcode;

    }

 

    public String getPhone() {

        return phone;

    }

 

    public void setPhone(String phone) {

        this.phone = phone;

    }

 

    public Set<Product5> getProducts() {

        return products;

    }

 

    public void setProducts(Set<Product5> products) {

        this.products = products;

    }

}

 

Order.hbm.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">

<hibernate-mapping>

 <class name="hibernate.example.vo.Order" table="ORDERS">

  <id column="ID" name="id">

   <generator class="increment"/>

  </id>

  <property column="REALNAME" name="realname" type="java.lang.String"/>

  <property column="PHONE" name="phone" type="java.lang.String"/>

  <property column="ADDRESS" name="address" type="java.lang.String"/>

  <property column="POSTCODE" name="postcode" type="java.lang.String"/>

  <property column="TOTAL" name="total" type="java.lang.Double"/>

  <set name="products" table="ORDERSITEM">

      <key column="ORDER_ID"/>

      <many-to-many class="hibernate.example.vo.Product5" column="PRODUCT_ID"/>

  </set>

 </class>

</hibernate-mapping>

Product5.java

package hibernate.example.vo;

public class Product5 {

private Integer id;

private String name;

private Double price;

private String description;

public Product5() {

super();

}

 

public Integer getId() {

return id;

}

 

public void setId(Integer id) {

this.id = id;

}

 

public String getName() {

return name;

}

 

public void setName(String name) {

this.name = name;

}

 

public Double getPrice() {

return price;

}

 

public void setPrice(Double price) {

this.price = price;

}

 

public String getDescription() {

return description;

}

 

public void setDescription(String description) {

this.description = description;

}

 

}

 

Product5.hbm.java

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

    <class name="hibernate.example.vo.Product5" table="PRODUCT3">

        <id name="id" column="ID" type="java.lang.Integer">

            <generator class="increment" />

        </id>

        <property name="name" column="NAME" type="java.lang.String"/>

        <property name="price" column="PRICE" type="java.lang.Double"/>

        <property name="description" column="DESCRIPTION" type="java.lang.String"/>

    </class>

</hibernate-mapping>

Many2ManyMapping.java

package hibernate.example.test;

import org.hibernate.Session;

import hibernate.example.util.HibernateSessionFactoryUtil;

import hibernate.example.vo.Order;

public class Many2ManyMappingTest {

 

    public static void main(String[] args) {

        new Many2ManyMappingTest().add();

    }

 

    public void add(){

        Order o = new Order();

        o.setRealname("Xavier");

        o.setPhone("114");

        o.setAddress("Dalian");

        o.setPostcode("116023");

        o.setTotal(300.0);

        Session session = HibernateSessionFactoryUtil.getSessionFactory().getCurrentSession();

        session.beginTransaction();

        Product5 p1 = (Product5)session.get(Product5.class, new Integer(1));

        Product5 p2 = (Product5)session.get(Product5.class, new Integer(2));

        o.getProducts().add(p1);

        o.getProducts().add(p2);

        session.save(o);

        session.getTransaction().commit();

    }

}

 

多对多双向关联(需要设置中间表外键ordersitem这样可以更顺利的生成两个表的多对多的双向关联)只需要提供两个主表。

Order2.java

package hibernate.example.vo;

import java.util.HashSet;

import java.util.Set;

public class Order2 {

    private Integer id;

    private String realname;

    private Double total;

    private String address;

    private String postcode;

    private String phone;

    private Set<Product6> products = new HashSet<Product6>();

   

    public Order2(){

       

    }

 

    public Integer getId() {

        return id;

    }

 

    public void setId(Integer id) {

        this.id = id;

    }

 

    public String getRealname() {

        return realname;

    }

 

    public void setRealname(String realname) {

        this.realname = realname;

    }

 

    public Double getTotal() {

        return total;

    }

 

    public void setTotal(Double total) {

        this.total = total;

    }

 

    public String getAddress() {

        return address;

    }

 

    public void setAddress(String address) {

        this.address = address;

    }

 

    public String getPostcode() {

        return postcode;

    }

 

    public void setPostcode(String postcode) {

        this.postcode = postcode;

    }

 

    public String getPhone() {

        return phone;

    }

 

    public void setPhone(String phone) {

        this.phone = phone;

    }

 

    public Set<Product6> getProducts() {

        return products;

    }

 

    public void setProducts(Set<Product6> products) {

        this.products = products;

    }

}

 

Order2.hbm.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">

<hibernate-mapping>

 <class name="hibernate.example.vo.Order2" table="ORDERS">

  <id column="ID" name="id">

   <generator class="increment"/>

  </id>

  <property column="REALNAME" name="realname" type="java.lang.String"/>

  <property column="PHONE" name="phone" type="java.lang.String"/>

  <property column="ADDRESS" name="address" type="java.lang.String"/>

  <property column="POSTCODE" name="postcode" type="java.lang.String"/>

  <property column="TOTAL" name="total" type="java.lang.Double"/>

  <set name="products" table="ORDERSITEM" inverse="true">

      <key column="ORDER_ID"/>

      <many-to-many class="hibernate.example.vo.Product6" column="PRODUCT_ID"/>

  </set>

 </class>

</hibernate-mapping>

Product6.java

package hibernate.example.vo;

import java.util.HashSet;

import java.util.Set;

public class Product6 {

    private Integer id;

    private String name;

    private Double price;

    private String description;

    private Set<Order2> orders = new HashSet<Order2>();

   

    public Product6() {

        super();

    }

 

    public Integer getId() {

        return id;

    }

 

    public void setId(Integer id) {

        this.id = id;

    }

 

    public String getName() {

        return name;

    }

 

    public void setName(String name) {

        this.name = name;

    }

 

    public Double getPrice() {

        return price;

    }

 

    public void setPrice(Double price) {

        this.price = price;

    }

 

    public String getDescription() {

        return description;

    }

 

    public void setDescription(String description) {

        this.description = description;

    }

 

    public Set<Order2> getOrders() {

        return orders;

    }

 

    public void setOrders(Set<Order2> orders) {

        this.orders = orders;

    }

}

 

Product6.hbm.xml

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

    <class name="hibernate.example.vo.Product6" table="PRODUCT3">

        <id name="id" column="ID" type="java.lang.Integer">

            <generator class="increment" />

        </id>

        <property name="name" column="NAME" type="java.lang.String"/>

        <property name="price" column="PRICE" type="java.lang.Double"/>

        <property name="description" column="DESCRIPTION" type="java.lang.String"/>

        <set name="orders" table="ORDERSITEM">

            <key column="PRODUCT_ID"/>

            <many-to-many class="hibernate.example.vo.Order2" column="ORDER_ID"/>

        </set>

    </class>

</hibernate-mapping>

Many2ManyDuaDirectoryMapping.java

package hibernate.example.test;

import org.hibernate.Session;

import hibernate.example.util.HibernateSessionFactoryUtil;

import hibernate.example.vo.Order2;

import hibernate.example.vo.Product6;

public class Many2ManyDuaDirectionMappingTest {

 

    public static void main(String[] args) {

        new Many2ManyDuaDirectionMappingTest().add();

    }

 

    public void add(){

        Order2 o = new Order2();

        o.setRealname("Nena");

        o.setPhone("119");

        o.setAddress("Germany");

        o.setPostcode("116023");

       

        Session session = HibernateSessionFactoryUtil.getSessionFactory().getCurrentSession();

        session.beginTransaction();

       

        Product6 p1 = (Product6)session.get(Product6.class, new Integer(1));

        Product6 p2 = (Product6)session.get(Product6.class, new Integer(2));

       

        double total = p1.getPrice() + p1.getPrice();

        o.setTotal(total);

       

        o.getProducts().add(p1);

        o.getProducts().add(p2);

       

        p1.getOrders().add(o);

        p2.getOrders().add(o);

       

        session.save(o);

        session.getTransaction().commit();

    }

}

 

多对多关联分拆成两个一对多关联

将多对多关联分拆成两个一对多关联,需要提供中间表的POJO,相对应的hbm文件,是所有字段作为复合主键,因此要提供hashCode和equals方法

Order3.java

package hibernate.example.vo;

import java.util.HashSet;

import java.util.Set;

public class Order3 {

    private Integer id;

    private String realname;

    private Double total;

    private String address;

    private String postcode;

    private String phone;

    private Set<OrdersItem> orderItems = new HashSet<OrdersItem>();

   

    public Order3(){

       

    }

 

    public Integer getId() {

        return id;

    }

 

    public void setId(Integer id) {

        this.id = id;

    }

 

    public String getRealname() {

        return realname;

    }

 

    public void setRealname(String realname) {

        this.realname = realname;

    }

 

    public Double getTotal() {

        return total;

    }

 

    public void setTotal(Double total) {

        this.total = total;

    }

 

    public String getAddress() {

        return address;

    }

 

    public void setAddress(String address) {

        this.address = address;

    }

 

    public String getPostcode() {

        return postcode;

    }

 

    public void setPostcode(String postcode) {

        this.postcode = postcode;

    }

 

    public String getPhone() {

        return phone;

    }

 

    public void setPhone(String phone) {

        this.phone = phone;

    }

 

    public Set<OrdersItem> getOrderItems() {

        return orderItems;

    }

 

    public void setOrderItems(Set<OrdersItem> orderItems) {

        this.orderItems = orderItems;

    }

 

    @Override

    public int hashCode() {

        final int prime = 31;

        int result = 1;

        result = prime * result + ((id == null) ? 0 : id.hashCode());

        return result;

    }

 

    @Override

    public boolean equals(Object obj) {

        if (this == obj)

            return true;

        if (obj == null)

            return false;

        if (getClass() != obj.getClass())

            return false;

        Order3 other = (Order3) obj;

        if (id == null) {

            if (other.id != null)

                return false;

        } else if (!id.equals(other.id))

            return false;

        return true;

    }

}

 

Order3.hbm.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">

<hibernate-mapping>

<class name="hibernate.example.vo.Order3" table="ORDERS">

<id column="ID" name="id">

<generator class="increment" />

</id>

<property column="REALNAME" name="realname" type="java.lang.String" />

<property column="PHONE" name="phone" type="java.lang.String" />

<property column="ADDRESS" name="address" type="java.lang.String" />

<property column="POSTCODE" name="postcode" type="java.lang.String" />

<property column="TOTAL" name="total" type="java.lang.Double" />

<set name="orderItems" inverse="true" cascade="save-update">

<key column="ORDER_ID" />

<one-to-many class="hibernate.example.vo.OrdersItem" />

</set>

</class>

</hibernate-mapping>

 

Product7.java

package hibernate.example.vo;

public class Product7 {

    private Integer id;

    private String name;

    private Double price;

    private String description;

   

    public Product7() {

        super();

    }

 

    public Integer getId() {

        return id;

    }

 

    public void setId(Integer id) {

        this.id = id;

    }

 

    public String getName() {

        return name;

    }

 

    public void setName(String name) {

        this.name = name;

    }

 

    public Double getPrice() {

        return price;

    }

 

    public void setPrice(Double price) {

        this.price = price;

    }

 

    public String getDescription() {

        return description;

    }

 

    public void setDescription(String description) {

        this.description = description;

    }

 

    @Override

    public int hashCode() {

        final int prime = 31;

        int result = 1;

        result = prime * result + ((id == null) ? 0 : id.hashCode());

        return result;

    }

 

    @Override

    public boolean equals(Object obj) {

        if (this == obj)

            return true;

        if (obj == null)

            return false;

        if (getClass() != obj.getClass())

            return false;

        Product7 other = (Product7) obj;

        if (id == null) {

            if (other.id != null)

                return false;

        } else if (!id.equals(other.id))

            return false;

        return true;

    }

}

 

Product7.hbm.xml

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

    <class name="hibernate.example.vo.Product7" table="PRODUCT3">

        <id name="id" column="ID" type="java.lang.Integer">

            <generator class="increment" />

        </id>

        <property name="name" column="NAME" type="java.lang.String"/>

        <property name="price" column="PRICE" type="java.lang.Double"/>

        <property name="description" column="DESCRIPTION" type="java.lang.String"/>

    </class>

</hibernate-mapping>

OrdersItem.java

package hibernate.example.vo;

import java.io.Serializable;

public class OrdersItem implements Serializable{

    private static final long serialVersionUID = 1L;

    private Order3 order;

    private Product7 product;

    private Integer quantity;

    private Double purchasePrice;

   

    public OrdersItem() {

       

    }

   

    public OrdersItem(Order3 order, Product7 product, Integer quantity,

            Double purchasePrice) {

        super();

        this.order = order;

        this.product = product;

        this.quantity = quantity;

        this.purchasePrice = purchasePrice;

    }

 

    public Order3 getOrder() {

        return order;

    }

 

    public void setOrder(Order3 order) {

        this.order = order;

    }

 

    public Product7 getProduct() {

        return product;

    }

 

    public void setProduct(Product7 product) {

        this.product = product;

    }

 

    public Integer getQuantity() {

        return quantity;

    }

 

    public void setQuantity(Integer quantity) {

        this.quantity = quantity;

    }

 

    public Double getPurchasePrice() {

        return purchasePrice;

    }

 

    public void setPurchasePrice(Double purchasePrice) {

        this.purchasePrice = purchasePrice;

    }

 

    @Override

    public int hashCode() {

        final int prime = 31;

        int result = 1;

        result = prime * result + ((order == null) ? 0 : order.hashCode());

        result = prime * result + ((product == null) ? 0 : product.hashCode());

        result = prime * result

                + ((purchasePrice == null) ? 0 : purchasePrice.hashCode());

        result = prime * result

                + ((quantity == null) ? 0 : quantity.hashCode());

        return result;

    }

 

    @Override

    public boolean equals(Object obj) {

        if (this == obj)

            return true;

        if (obj == null)

            return false;

        if (getClass() != obj.getClass())

            return false;

        OrdersItem other = (OrdersItem) obj;

        if (order == null) {

            if (other.order != null)

                return false;

        } else if (!order.equals(other.order))

            return false;

        if (product == null) {

            if (other.product != null)

                return false;

        } else if (!product.equals(other.product))

            return false;

        if (purchasePrice == null) {

            if (other.purchasePrice != null)

                return false;

        } else if (!purchasePrice.equals(other.purchasePrice))

            return false;

        if (quantity == null) {

            if (other.quantity != null)

                return false;

        } else if (!quantity.equals(other.quantity))

            return false;

        return true;

    }

}

 

Many2Many2MultipleOne2ManyMappingTest.java

package hibernate.example.test;

import org.hibernate.Session;

import hibernate.example.util.HibernateSessionFactoryUtil;

import hibernate.example.vo.Order3;

import hibernate.example.vo.OrdersItem;

import hibernate.example.vo.Product7;

public class Many2Many2MultipleOne2ManyMappingTest {

 

    public static void main(String[] args) {

        new Many2Many2MultipleOne2ManyMappingTest().add();

    }

 

    public void add(){

        Order3 o = new Order3();

        o.setRealname("Christina");

        o.setPhone("111");

        o.setAddress("Xiamen");

        o.setPostcode("668866");

        Session session = HibernateSessionFactoryUtil.getSessionFactory().getCurrentSession();

        session.beginTransaction();

        Product7 p1 = (Product7)session.get(Product7.class, new Integer(1)); //100

        Product7 p2 = (Product7)session.get(Product7.class, new Integer(2)); //200

        OrdersItem item1 = new OrdersItem(o, p1, 100, 198.0);

        OrdersItem item2 = new OrdersItem(o, p2, 200, 90.0);

        o.getOrderItems().add(item1);

        o.getOrderItems().add(item2);

        double total = 100*198+200*90.0;

        o.setTotal(total);

        session.save(o);

        session.getTransaction().commit();

    }

}

 

自身映射 – Menu

Menu.java

package hibernate.example.vo;

import java.util.HashSet;

import java.util.Set;

public class Menu {

    private Integer id;

    private String name;

    private String description;

    private Integer levels;

    private Menu parent;

    private Set<Menu> children = new HashSet<Menu> ();

   

    public Menu() {

        super();

    }

 

    public Integer getId() {

        return id;

    }

 

    public void setId(Integer id) {

        this.id = id;

    }

 

    public String getName() {

        return name;

    }

 

    public void setName(String name) {

        this.name = name;

    }

 

    public String getDescription() {

        return description;

    }

 

    public void setDescription(String description) {

        this.description = description;

    }

 

    public Integer getLevels() {

        return levels;

    }

 

    public void setLevels(Integer levels) {

        this.levels = levels;

    }

 

    public Menu getParent() {

        return parent;

    }

 

    public void setParent(Menu parent) {

        this.parent = parent;

    }

 

    public Set<Menu> getChildren() {

        return children;

    }

 

    public void setChildren(Set<Menu> children) {

        this.children = children;

    }

}

 

Menu.hbm.xml

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

    <class name="hibernate.example.vo.Menu" table="CATEGORY2">

        <id name="id" column="ID" type="java.lang.Integer">

            <generator class="increment" />

        </id>

        <property name="name" column="NAME" type="java.lang.String"/>

        <property name="description" column="DESCRIPTION" type="java.lang.String"/>

        <property name="levels" column="LEVELS" type="java.lang.Integer"/>

        <many-to-one name="parent" class="hibernate.example.vo.Menu">

            <column name="PARENT_ID" />

        </many-to-one>

        <set name="children" table="CATEGORY2">

            <key>

                <column name="PARENT_ID" />

            </key>

            <one-to-many class="hibernate.example.vo.Menu" />

        </set>

    </class>

</hibernate-mapping>

 

SelfMappingTest.java

package hibernate.example.test;

import org.hibernate.Session;

import hibernate.example.util.HibernateSessionFactoryUtil;

import hibernate.example.vo.Menu;

public class SelfMappingTest {

 

    public static void main(String[] args) {

//      new SelfMappingTest().addLevel1Menu();

        new SelfMappingTest().addLevel2Menu();

    }

 

    public void addLevel1Menu(){

        Menu menu = new Menu();

        menu.setName("电脑产品");

        menu.setLevels(1);

        menu.setDescription("电脑产品");

        Session session = HibernateSessionFactoryUtil.getSessionFactory().getCurrentSession();

        session.beginTransaction();

        session.save(menu);

        session.getTransaction().commit();

    }

 

    public void addLevel2Menu(){

        Menu menu = new Menu();

        menu.setName("电脑整机");

        menu.setLevels(2);

        menu.setDescription("电脑整机");

        Session session = HibernateSessionFactoryUtil.getSessionFactory().getCurrentSession();

        session.beginTransaction();

        Menu parent = (Menu)session.get(Menu.class, new Integer(9));

        menu.setParent(parent);

        session.save(menu);

        session.getTransaction().commit();

    }

}

 

多表关联

category2(Menu2) – product4(Product8) – ordersitem2(OrdersItem2) – orders2(Order4) – users4(Users9)

Menu2.java

package hibernate.example.vo;

import java.util.HashSet;

import java.util.Set;

public class Menu2 {

    private Integer id;

    private String name;

    private String description;

    private Integer levels;

    private Menu2 parent;

    private Set<Menu2> children = new HashSet<Menu2> ();

    private Set<Product8> products = new HashSet<Product8>();

   

    public Menu2() {

        super();

    }

 

    public Integer getId() {

        return id;

    }

 

    public void setId(Integer id) {

        this.id = id;

    }

 

    public String getName() {

        return name;

    }

 

    public void setName(String name) {

        this.name = name;

    }

 

    public String getDescription() {

        return description;

    }

 

    public void setDescription(String description) {

        this.description = description;

    }

 

    public Integer getLevels() {

        return levels;

    }

 

    public void setLevels(Integer levels) {

        this.levels = levels;

    }

 

    public Menu2 getParent() {

        return parent;

    }

 

    public void setParent(Menu2 parent) {

        this.parent = parent;

    }

 

    public Set<Menu2> getChildren() {

        return children;

    }

 

    public void setChildren(Set<Menu2> children) {

        this.children = children;

    }

 

    public Set<Product8> getProducts() {

        return products;

    }

 

    public void setProducts(Set<Product8> products) {

        this.products = products;

    }

}

 

Menu2.hbm.xml

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

    <class name="hibernate.example.vo.Menu2" table="CATEGORY2">

        <id name="id" column="ID" type="java.lang.Integer">

            <generator class="increment" />

        </id>

        <property name="name" column="NAME" type="java.lang.String"/>

        <property name="description" column="DESCRIPTION" type="java.lang.String"/>

        <property name="levels" column="LEVELS" type="java.lang.Integer"/>

        <many-to-one name="parent" class="hibernate.example.vo.Menu2">

            <column name="PARENT_ID" />

        </many-to-one>

        <set name="children">

            <key column="PARENT_ID"/>

            <one-to-many class="hibernate.example.vo.Menu2" />

        </set>

        <set name="products" inverse="true">

            <key column="CATEGORY_ID"/>

            <one-to-many class="hibernate.example.vo.Product8"/>

        </set>

    </class>

</hibernate-mapping>

 

Product8.java

package hibernate.example.vo;

public class Product8 {

    private Integer id;

    private String name;

    private Double price;

    private String description;

    private Menu2 category;

   

    public Product8() {

        super();

    }

 

    public Integer getId() {

        return id;

    }

 

    public void setId(Integer id) {

        this.id = id;

    }

 

    public String getName() {

        return name;

    }

 

    public void setName(String name) {

        this.name = name;

    }

 

    public Double getPrice() {

        return price;

    }

 

    public void setPrice(Double price) {

        this.price = price;

    }

 

    public String getDescription() {

        return description;

    }

 

    public void setDescription(String description) {

        this.description = description;

    }

 

    public Menu2 getCategory() {

        return category;

    }

 

    public void setCategory(Menu2 category) {

        this.category = category;

    }

 

    @Override

    public int hashCode() {

        final int prime = 31;

        int result = 1;

        result = prime * result + ((id == null) ? 0 : id.hashCode());

        return result;

    }

 

    @Override

    public boolean equals(Object obj) {

        if (this == obj)

            return true;

        if (obj == null)

            return false;

        if (getClass() != obj.getClass())

            return false;

        Product8 other = (Product8) obj;

        if (id == null) {

            if (other.id != null)

                return false;

        } else if (!id.equals(other.id))

            return false;

        return true;

    }  

}

 

Product8.hbm.xml

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

    <class name="hibernate.example.vo.Product8" table="PRODUCT4">

        <id name="id" column="ID" type="java.lang.Integer">

            <generator class="increment" />

        </id>

        <property name="name" column="NAME" type="java.lang.String"/>

        <property name="price" column="PRICE" type="java.lang.Double"/>

        <property name="description" column="DESCRIPTION" type="java.lang.String"/>

        <many-to-one name="category" class="hibernate.example.vo.Menu2" column="CATEGORY_ID"/>

    </class>

</hibernate-mapping>

 

OrdersItem2.java

package hibernate.example.vo;

import java.io.Serializable;

public class OrdersItem2 implements Serializable{

    private static final long serialVersionUID = 1L;

    private Order4 order;

    private Product8 product;

    private Integer quantity;

    private Double purchasePrice;

   

    public OrdersItem2() {

       

    }

   

    public OrdersItem2(Order4 order, Product8 product, Integer quantity,

            Double purchasePrice) {

        super();

        this.order = order;

        this.product = product;

        this.quantity = quantity;

        this.purchasePrice = purchasePrice;

    }

 

    public Order4 getOrder() {

        return order;

    }

 

    public void setOrder(Order4 order) {

        this.order = order;

    }

 

    public Product8 getProduct() {

        return product;

    }

 

    public void setProduct(Product8 product) {

        this.product = product;

    }

 

    public Integer getQuantity() {

        return quantity;

    }

 

    public void setQuantity(Integer quantity) {

        this.quantity = quantity;

    }

 

    public Double getPurchasePrice() {

        return purchasePrice;

    }

 

    public void setPurchasePrice(Double purchasePrice) {

        this.purchasePrice = purchasePrice;

    }

 

    @Override

    public int hashCode() {

        final int prime = 31;

        int result = 1;

        result = prime * result + ((order == null) ? 0 : order.hashCode());

        result = prime * result + ((product == null) ? 0 : product.hashCode());

        result = prime * result

                + ((purchasePrice == null) ? 0 : purchasePrice.hashCode());

        result = prime * result

                + ((quantity == null) ? 0 : quantity.hashCode());

        return result;

    }

 

    @Override

    public boolean equals(Object obj) {

        if (this == obj)

            return true;

        if (obj == null)

            return false;

        if (getClass() != obj.getClass())

            return false;

        OrdersItem2 other = (OrdersItem2) obj;

        if (order == null) {

            if (other.order != null)

                return false;

        } else if (!order.equals(other.order))

            return false;

        if (product == null) {

            if (other.product != null)

                return false;

        } else if (!product.equals(other.product))

            return false;

        if (purchasePrice == null) {

            if (other.purchasePrice != null)

                return false;

        } else if (!purchasePrice.equals(other.purchasePrice))

            return false;

        if (quantity == null) {

            if (other.quantity != null)

                return false;

        } else if (!quantity.equals(other.quantity))

            return false;

        return true;

    }

}

 

OrdersItem2.hbm.xml

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

                                   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

 <class name="hibernate.example.vo.OrdersItem2" table="ORDERSITEM2">

  <composite-id>

   <key-many-to-one class="hibernate.example.vo.Order4" column="ORDER_ID" name="order"/>

   <key-many-to-one class="hibernate.example.vo.Product8" column="PRODUCT_ID" name="product"/>

   <key-property type="java.lang.Integer" column="QUANTITY" name="quantity" />

   <key-property type="java.lang.Double" column="PURCHASEPRICE" name="purchasePrice"/>

  </composite-id>

 </class>

</hibernate-mapping>

 

Order4.java

package hibernate.example.vo;

import java.util.HashSet;

import java.util.Set;

public class Order4 {

    private Integer id;

    private String realname;

    private String phone;

    private String address;

    private Integer postcode;

    private Double total;

    private Users9 user;

    private Set<OrdersItem2> orderItems = new HashSet<OrdersItem2>();

   

    public Order4(){

       

    }

 

    public Integer getId() {

        return id;

    }

 

    public void setId(Integer id) {

        this.id = id;

    }

 

    public String getRealname() {

        return realname;

    }

 

    public void setRealname(String realname) {

        this.realname = realname;

    }

 

    public Double getTotal() {

        return total;

    }

 

    public void setTotal(Double total) {

        this.total = total;

    }

 

    public String getAddress() {

        return address;

    }

 

    public void setAddress(String address) {

        this.address = address;

    }

 

    public Integer getPostcode() {

        return postcode;

    }

 

    public void setPostcode(Integer postcode) {

        this.postcode = postcode;

    }

 

    public String getPhone() {

        return phone;

    }

 

    public void setPhone(String phone) {

        this.phone = phone;

    }

 

    public Users9 getUser() {

        return user;

    }

 

    public void setUser(Users9 user) {

        this.user = user;

    }

 

    public Set<OrdersItem2> getOrderItems() {

        return orderItems;

    }

 

    public void setOrderItems(Set<OrdersItem2> orderItems) {

        this.orderItems = orderItems;

    }

 

    @Override

    public int hashCode() {

        final int prime = 31;

        int result = 1;

        result = prime * result + ((id == null) ? 0 : id.hashCode());

        return result;

    }

 

    @Override

    public boolean equals(Object obj) {

        if (this == obj)

            return true;

        if (obj == null)

            return false;

        if (getClass() != obj.getClass())

            return false;

        Order4 other = (Order4) obj;

        if (id == null) {

            if (other.id != null)

                return false;

        } else if (!id.equals(other.id))

            return false;

        return true;

    }

}

 

Order4.hbm.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">

<hibernate-mapping>

<class name="hibernate.example.vo.Order4" table="ORDERS2">

<id column="ID" name="id">

<generator class="increment" />

</id>

<property column="REALNAME" name="realname" type="java.lang.String" />

<property column="PHONE" name="phone" type="java.lang.String" />

<property column="ADDRESS" name="address" type="java.lang.String" />

<property column="POSTCODE" name="postcode" type="java.lang.Integer" />

<property column="TOTAL" name="total" type="java.lang.Double" />

<many-to-one name="user" class="hibernate.example.vo.Users9">

<column name="USER_ID" />

</many-to-one>

<set name="orderItems" inverse="true">

<key column="ORDER_ID" />

<one-to-many class="hibernate.example.vo.OrdersItem2" />

</set>

</class>

</hibernate-mapping>

 

Users9.java

package hibernate.example.vo;

import java.util.HashSet;

import java.util.Set;

public class Users9 implements java.io.Serializable{

 

    private static final long serialVersionUID = 6334478284382766267L;

    private Integer id;

    private String username;

    private String password;

    private String address;

    private String phone;

    private Integer postcode;

    private Set<Order4> orders = new HashSet<Order4>();

   

    public Users9() {

 

    }

   

    public Integer getId() {

        return id;

    }

 

    public void setId(Integer id) {

        this.id = id;

    }

 

 

    public String getUsername() {

        return username;

    }

 

    public void setUsername(String username) {

        this.username = username;

    }

 

    public String getPassword() {

        return password;

    }

 

    public void setPassword(String password) {

        this.password = password;

    }

 

    public String getAddress() {

        return address;

    }

 

    public void setAddress(String address) {

        this.address = address;

    }

 

    public String getPhone() {

        return phone;

    }

 

    public void setPhone(String phone) {

        this.phone = phone;

    }

 

    public Integer getPostcode() {

        return postcode;

    }

 

    public void setPostcode(Integer postcode) {

        this.postcode = postcode;

    }

 

    public Set<Order4> getOrders() {

        return orders;

    }

 

    public void setOrders(Set<Order4> orders) {

        this.orders = orders;

    }

}

 

Users9.hbm.xml

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

    <class name="hibernate.example.vo.Users9" table="USERS4">

        <id name="id" type="java.lang.Integer" column="id">

            <generator class="increment" />

        </id>

<property name="username" column="USERNAME" type="java.lang.String"/>

<property name="password" column="PASSWORD" type="java.lang.String"/>

<property name="address" column="ADDRESS" type="java.lang.String"/>

<property name="phone" column="PHONE" type="java.lang.String"/>

<property name="postcode" column="POSTCODE" type="java.lang.Integer"/>

<set name="orders" inverse="true">

<key column="USER_ID"/>

<one-to-many class="hibernate.example.Order4"/>

</set>

    </class>

</hibernate-mapping>

Advertisements
 
Leave a comment

Posted by on 04/05/2011 in HIBERNATE

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

 
%d bloggers like this: