Hibernate table relationship design

A Product corresponds to a Category 

One Category corresponds to multiple Products 

First set up a many-to-one relationship , first prepare Category.java, Category.hbm.xml and Category, and add Category mapping in hibernate.cfg.xml .

Add Category property to Product.java:

package com.how2java.pojo;  
public class Product {  
    int id;  
    String name;  
    float price;  
    Category category;//Add category attribute and add get and set methods  
    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;  
    }     
}

Set up the Category many-to-one relationship in 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" /><!--Use the many-to-one tag to set a many-to-one relationship name="category" corresponds to the category in the Product class The attribute class="Category" represents the corresponding Category class column="cid" represents the foreign key to the category_ table -->  
    </class>  
      
</hibernate-mapping>  

TestHibernate tests many-to-one relationships:

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();  
        //The core code, in this test example, adds a new Category object "c1" and sets it to the category of the product with id=8  
        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();  
    }  
}  

Then implement a one-to-many relationship: Category and Product are one-to-many relationships.

Add a Set collection to Category:

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;//Core code, add a set collection  
    public Set<Product> getProducts() {  
        return products;  
    }  
    public void setProducts(Set<Product> products) {  
        this.products = products;  
    }  
}  

Add one-to-many mapping to Category.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="Category" table="category_">  
        <id name="id" column="id">  
            <generator class="native">  
            </generator>  
        </id>  
        <property name="name" />  
                <!--Core code-->  
        <set name="products" lazy="false"><!--set is used to set a one-to-many (many-to-many) relationship. You can also use a list. The setting is a little more complicated. Here, a simple set is used to get started. . name="products" corresponds to the products attribute in the Category class. lazy="false" means not to use lazy loading. -->  
            <key column="cid" not-null="false" /><!-- Indicates that the foreign key is cid and can be empty -->  
            <one-to-many class="Product" /><!-- Indicates that the class corresponding to one-to-many is Product-->  
        </set>  
                  
    </class>  
      
</hibernate-mapping>  

TestHibernate tests one-to-many relationships

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();  
        //core code  
        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();  
    }  
}  

Settings for many-to-many relationships :

A Product can be purchased by multiple Users 

A User can purchase multiple Products 

So the relationship between Product and User is many-to-many many-to-many 

To achieve a many-to-many relationship, there must be an intermediate table user_product to maintain the relationship between User and Product

Prepare User.java and User.hbm.xml first.

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" />  
                <!-- The core code, products are provided by USer, and then in the user_product table, user corresponds to uid, product corresponds to 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 adds a collection of corresponding Users

package com.how2java.pojo;  
import java.util.Set;  
public class Product {  
    int id;  
    String name;  
    float price;  
    Category category;  
    Set<User> users;//Add the corresponding Users and set methods and get methods  
    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;  
    }  
      
}  

The settings of Product.hbm.xml are the same:

<?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" />  
        <!-- Core code-->  
        <set name="users" table="user_product" lazy="false">  
            <key column="pid" />  
            <many-to-many column="uid" class="User" />  
        </set>                          
    </class>  
</hibernate-mapping>  

Add the mapping of User in hibernate.cfg.xml.

TestHibernate tests many-to-many relationships

First add 3 users

Then demo product 1 is purchased by users 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();  
           
        //add 3 users  
        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);  
        }  
        // Product 1 was purchased by users 1, 2, 3  
        Product p1 = (Product) s.get(Product.class, 1);  
        p1.setUsers(users);  
        s.save(p1);  
        s.getTransaction().commit();  
        s.close();  
        sf.close();  
    }  
}  




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324408614&siteId=291194637