Hibernate 的表关系设计
一个Product对应一个Category 

一个Category对应多个Product 

先设置多对一的关系,先准备Category.java和Category.hbm.xml和Category和在hibernate.cfg.xml中增加Category的映射

为Product.java增加Category属性:

package com.how2java.pojo;  
public class Product {  
    int id;  
    String name;  
    float price;  
    Category category;//增加category属性并增加get和set方法  
    public Category getCategory() {  
        return category;  
    }  
    public void setCategory(Category category) {  
        this.category = category;  
    }  
    public int getId() {  
        return id;  
    }  
    public void setId(int id) {  
        this.id = id;  
    }  
    public String getName() {  
        return name;  
    }  
    public void setName(String name) {  
        this.name = name;  
    }  
    public float getPrice() {  
        return price;  
    }  
    public void setPrice(float price) {  
        this.price = price;  
    }     
}

在Product.hbm.xml中设置Category 多对一关系:

<?xml version="1.0"?>  
<!DOCTYPE hibernate-mapping PUBLIC  
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">  
  
<hibernate-mapping package="com.how2java.pojo">  
    <class name="Product" table="product_">  
        <id name="id" column="id">  
            <generator class="native">  
            </generator>  
        </id>  
        <property name="name" />  
        <property name="price" />  
        <many-to-one name="category" class="Category" column="cid" /><!--使用many-to-one 标签设置多对一关系 name="category" 对应Product类中的category属性 class="Category" 表示对应Category类 column="cid" 表示指向 category_表的外键 -->  
    </class>  
      
</hibernate-mapping>  

TestHibernate 测试many-to-one关系:

package com.how2java.test;  
import org.hibernate.Session;  
import org.hibernate.SessionFactory;  
import org.hibernate.cfg.Configuration;  
import com.how2java.pojo.Category;  
import com.how2java.pojo.Product;  
public class TestHibernate {  
    public static void main(String[] args) {  
        SessionFactory sf = new Configuration().configure().buildSessionFactory();  
        Session s = sf.openSession();  
        s.beginTransaction();  
        //核心代码,在这个测试例子中,增加了一个新的Category对象"c1" 并将其设置为id=8的product的category  
        Category c =new Category();  
        c.setName("c1");  
        s.save(c);  
        Product p = (Product) s.get(Product.class, 8);  
        p.setCategory(c);  
        s.update(p);  
          
        s.getTransaction().commit();  
        s.close();  
        sf.close();  
    }  
}  

接着实现一对多的关系:Category和Product是一对多的关系。

为Category增加一个Set集合:

package com.how2java.pojo;  
import java.util.Set;  
public class Category {  
    public int getId() {  
        return id;  
    }  
    public void setId(int id) {  
        this.id = id;  
    }  
    public String getName() {  
        return name;  
    }  
    public void setName(String name) {  
        this.name = name;  
    }  
    int id;  
    String name;  
    Set<Product> products;//核心代码,增加一个set集合  
    public Set<Product> getProducts() {  
        return products;  
    }  
    public void setProducts(Set<Product> products) {  
        this.products = products;  
    }  
}  

为Category.hbm.xml增加one-to-many映射:

<?xml version="1.0"?>  
<!DOCTYPE hibernate-mapping PUBLIC  
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">  
  
<hibernate-mapping package="com.how2java.pojo">  
    <class name="Category" table="category_">  
        <id name="id" column="id">  
            <generator class="native">  
            </generator>  
        </id>  
        <property name="name" />  
                <!--核心代码-->  
        <set name="products" lazy="false"><!--set 用于设置一对多(多对多也是他)关系,也可以用list,设置稍复杂点,这里使用简单的set来入门。name="products" 对应 Category类中的products属性。lazy="false" 表示不使用延迟加载。 -->  
            <key column="cid" not-null="false" /><!-- 表示外键是cid,可以为空-->  
            <one-to-many class="Product" /><!-- 表示一对多所对应的类是Product-->  
        </set>  
                  
    </class>  
      
</hibernate-mapping>  

TestHibernate 测试one-to-many关系

package com.how2java.test;  
import java.util.Set;  
import org.hibernate.Session;  
import org.hibernate.SessionFactory;  
import org.hibernate.cfg.Configuration;  
import com.how2java.pojo.Category;  
import com.how2java.pojo.Product;  
   
public class TestHibernate {  
    public static void main(String[] args) {  
        SessionFactory sf = new Configuration().configure().buildSessionFactory();   
        Session s = sf.openSession();  
        s.beginTransaction();  
        //核心代码  
        Category c = (Category) s.get(Category.class, 1);  
        Set<Product> ps = c.getProducts();  
        for (Product p : ps) {  
            System.out.println(p.getName());  
        }  
  
        s.getTransaction().commit();  
        s.close();  
        sf.close();  
    }  
}  

多对多关系的设置:

一种Product可以被多个User购买 

一个User可以购买多种Product 

所以Product和User之间的关系是多对多 many-to-many 

要实现多对多关系,必须有一张中间表 user_product 用于维护 User和Product之间的关系

先准备User.java和User.hbm.xml。

package com.how2java.pojo;  
import java.util.Set;  
public class User {  
    int id;  
    String name;  
    Set<Product> products;  
    public int getId() {  
        return id;  
    }  
    public void setId(int id) {  
        this.id = id;  
    }  
    public String getName() {  
        return name;  
    }  
    public void setName(String name) {  
        this.name = name;  
    }  
    public Set<Product> getProducts() {  
        return products;  
    }  
    public void setProducts(Set<Product> products) {  
        this.products = products;  
    }  
}  
<?xml version="1.0"?>  
<!DOCTYPE hibernate-mapping PUBLIC  
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">  
  
<hibernate-mapping package="com.how2java.pojo">  
    <class name="User" table="user_">  
        <id name="id" column="id">  
            <generator class="native">  
            </generator>  
        </id>  
        <property name="name" />  
                <!-- 核心代码,products是USer自带,然后再user_product表中,user对应uid,product对应pid-->  
        <set name="products" table="user_product" lazy="false">  
            <key column="uid" />  
            <many-to-many column="pid" class="Product" />  
        </set>          
    </class>    
</hibernate-mapping>  

Product.java增加了对应Users的集合

package com.how2java.pojo;  
import java.util.Set;  
public class Product {  
    int id;  
    String name;  
    float price;  
    Category category;  
    Set<User> users;//增加对应的Users和set方法和get方法  
    public Set<User> getUsers() {  
        return users;  
    }  
    public void setUsers(Set<User> users) {  
        this.users = users;  
    }  
    public Category getCategory() {  
        return category;  
    }  
    public void setCategory(Category category) {  
        this.category = category;  
    }  
    public int getId() {  
        return id;  
    }  
    public void setId(int id) {  
        this.id = id;  
    }  
    public String getName() {  
        return name;  
    }  
    public void setName(String name) {  
        this.name = name;  
    }  
    public float getPrice() {  
        return price;  
    }  
    public void setPrice(float price) {  
        this.price = price;  
    }  
      
}  

Product.hbm.xml的设置同理:

<?xml version="1.0"?>  
<!DOCTYPE hibernate-mapping PUBLIC  
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">  
<hibernate-mapping package="com.how2java.pojo">  
    <class name="Product" table="product_">  
        <id name="id" column="id">  
            <generator class="native">  
            </generator>  
        </id>  
        <property name="name" />  
        <property name="price" />  
        <many-to-one name="category" class="Category" column="cid" />  
        <!-- 核心代码-->  
        <set name="users" table="user_product" lazy="false">  
            <key column="pid" />  
            <many-to-many column="uid" class="User" />  
        </set>                          
    </class>  
</hibernate-mapping>  

在hibernate.cfg.xml中增加User的映射。

TestHibernate 测试many-to-many关系

首先增加3个用户

然后演示产品1被用户1,2,3购买。

package com.how2java.test;  
import java.util.HashSet;  
import java.util.Set;  
import org.hibernate.Session;  
import org.hibernate.SessionFactory;  
import org.hibernate.cfg.Configuration;  
import com.how2java.pojo.Product;  
import com.how2java.pojo.User;  
    
public class TestHibernate {  
    public static void main(String[] args) {  
        SessionFactory sf = new Configuration().configure().buildSessionFactory();  
        Session s = sf.openSession();  
        s.beginTransaction();  
           
        //增加3个用户  
        Set<User> users = new HashSet();  
        for (int i = 0; i < 3; i++) {  
            User u =new User();  
            u.setName("user"+i);  
            users.add(u);  
            s.save(u);  
        }  
        //产品1被用户1,2,3购买  
        Product p1 = (Product) s.get(Product.class, 1);  
        p1.setUsers(users);  
        s.save(p1);  
        s.getTransaction().commit();  
        s.close();  
        sf.close();  
    }  
}  




阅读更多
文章标签: Hibernate
个人分类: Hibernate
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

Hibernate 的表关系设计

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭