标签:导致 双向 多对多关系映射 构造方法 comm 删除 ring gen 父类
一对多关系映射大家都明白,关系双方都一个含有对方多个引用,但自身一对多很多同学都不明白什么意思,那么首先我就说明一下什么是自身一对多,其实也很好理解,自身一对多就是自身含有本身的多个引用,例如新闻类别,新闻包含体育新闻和政治新闻,体育新闻内有含有足球新闻和篮球新闻,其实他们都属于新闻,只是名字不同而已,下面我们就以新闻类别为例来具体说明一下:
首先我们来看一下新闻类别的类图:
类图:category
从上面的图我们可以看出:每一个新闻类别都有一个父类别和一个孩子类别的set集合,这个父类别和孩子类别里面都是自身的引用,这样就行了自身一对多的对象关系
下面看一下具体的新闻实体类:Category.Java
- public class Category
-
- {
-
- private Long id;
-
- private String name;
-
- private Category parentCategory;
-
- private Set<Category> childCategories;
-
- public Category(String name, Category parentCategory,
-
- Set<Category> childCategories)
-
- {
-
- this.name = name;
-
- this.parentCategory = parentCategory;
-
- this.childCategories = childCategories;
-
- }
-
- public Category()
-
- {
-
- }
-
- *******set、get方法省略
-
- }
看完具体的实体类之后我们下面看一下其具体的配置,其实他的配置中没什么特别的地方,仅仅只是他的配置中包含了一对多和多对一的共同标签存在而已:他即含有多的一方的<set>标签。也含有一的一方的<many-to-one>标签:
Category.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="com.shengsiyuan.hibernate.Category" table="categories">
-
- <id name="id" column="id" type="long">
-
- <generator class="increment"></generator>
-
- </id>
-
- <property name="name" type="string" >
-
- <column name="name" length="50" ></column>
-
- </property>
-
- <set name="childCategories" cascade="all" inverse="true">
-
- <key column="category_id"></key>
-
- <one-to-many class="com.shengsiyuan.hibernate.Category"/>
-
- </set>
-
- <many-to-one name="parentCategory" column="category_id" class="com.shengsiyuan.hibernate.Category">
-
- </many-to-one>
-
- </class>
-
- </hibernate-mapping>
下面我们来看一下在自身一对多的关系下进行增删改查的示例:
- import java.util.HashSet;
-
- import org.hibernate.Session;
-
- import org.hibernate.SessionFactory;
-
- import org.hibernate.Transaction;
-
- import org.hibernate.cfg.Configuration;
-
- public class HibernateTest2
-
- {
-
- private static SessionFactory sessionFactory;
-
- static
-
- {
-
- try
-
- {
-
- sessionFactory = new Configuration().configure()
-
- .buildSessionFactory();
-
- }
-
- catch (Exception ex)
-
- {
-
- ex.printStackTrace();
-
- }
-
- }
-
- public static void main(String[] args)
-
- {
-
- Session session = sessionFactory.openSession();
-
- Transaction tx = null;
-
- try
-
- {
-
- tx = session.beginTransaction();
-
- Category category1 = new Category("level1", null, new HashSet<Category>());
-
- Category category2 = new Category("level2", null, new HashSet<Category>());
-
- Category category3 = new Category("level2", null, new HashSet<Category>());
-
- Category category4 = new Category("level3", null, new HashSet<Category>());
-
- Category category5 = new Category("level3", null, new HashSet<Category>());
-
- Category category6 = new Category("level3", null, new HashSet<Category>());
-
- Category category7 = new Category("level3", null, new HashSet<Category>());
-
- category2.setParentCategory(category1);
-
- category3.setParentCategory(category1);
-
- category1.getChildCategories().add(category2);
-
- category1.getChildCategories().add(category3);
-
- category4.setParentCategory(category2);
-
- category5.setParentCategory(category2);
-
- category2.getChildCategories().add(category4);
-
- category2.getChildCategories().add(category5);
-
- category6.setParentCategory(category3);
-
- category7.setParentCategory(category3);
-
- category3.getChildCategories().add(category6);
-
- category3.getChildCategories().add(category7);
-
- Category category = (Category)session.get(Category.class, new Long(1));
-
- System.out.println(category.getChildCategories().iterator().next().getName());
-
- session.delete(category);
-
- tx.commit();
-
- }
-
- catch(Exception ex)
-
- {
-
- if(null != tx)
-
- {
-
- tx.rollback();
-
- }
-
- }
-
- finally
-
- {
-
- session.close();
-
- }
-
- }
-
- }
-
-
Hibernate自身一对多和多对多关系映射
标签:导致 双向 多对多关系映射 构造方法 comm 删除 ring gen 父类
原文地址:http://www.cnblogs.com/mannixiang/p/6665813.html