8、自身双向一对多关联关系

发布时间:2016-12-11 17:57:03 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"8、自身双向一对多关联关系",主要涉及到8、自身双向一对多关联关系方面的内容,对于8、自身双向一对多关联关系感兴趣的同学可以参考一下。

映射Category一对多双向自身关联 Category类的类图: categories表结构   创建表: CREATE TABLE `categories` ( `id` bigint(20) NOT NULL, `name` varchar(15) default NULL, `category_id` bigint(20) default NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; 创建Category类: import java.util.Set; public class Category { private Long id; private String name; private Category parentCategory; private Set<Category> childCategory; public Category(String name, Category parentCategory, Set<Category> childCategory) { this.name = name; this.parentCategory = parentCategory; this.childCategory = childCategory; } public Category() { super(); } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Category getParentCategory() { return parentCategory; } public void setParentCategory(Category parentCategory) { this.parentCategory = parentCategory; } public Set<Category> getChildCategory() { return childCategory; } public void setChildCategory(Set<Category> childCategory) { this.childCategory = childCategory; } } 创建Category.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="com.cdtax.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="childCategory" cascade="all" inverse="true"> <key column="category_id"></key> <one-to-many class="com.cdtax.hibernate.Category"/> </set> <many-to-one name="parentCategory" column="category_id" class="com.cdtax.hibernate.Category"></many-to-one> </class> </hibernate-mapping> 因为是自身一对多,所以,配置表中就有set又有many-to-one 测试类,保存操作: 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) throws Exception { Session session = sessionFactory.openSession(); Transaction tx = null; try { tx = session.beginTransaction(); Category category1 = new Category("leval1",null,new HashSet()); Category category2 = new Category("leval2",null,new HashSet()); Category category3 = new Category("leval2",null,new HashSet()); Category category4 = new Category("leval3",null,new HashSet()); Category category5 = new Category("leval3",null,new HashSet()); Category category6 = new Category("leval3",null,new HashSet()); Category category7 = new Category("leval3",null,new HashSet()); category2.setParentCategory(category1); category3.setParentCategory(category1); category1.getChildCategory().add(category2); category1.getChildCategory().add(category3); category3.getChildCategory().add(category4); category3.getChildCategory().add(category5); category4.setParentCategory(category3); category5.setParentCategory(category3); category2.getChildCategory().add(category6); category2.getChildCategory().add(category7); category6.setParentCategory(category2); category7.setParentCategory(category2); session.save(category1); tx.commit(); } catch(Exception ex) { if(null != tx) { tx.rollback(); } ex.printStackTrace(); } finally { session.close(); } } } 执行结果: Hibernate: select max(id) from categories Hibernate: insert into categories (name, category_id, id) values (?, ?, ?) Hibernate: insert into categories (name, category_id, id) values (?, ?, ?) Hibernate: insert into categories (name, category_id, id) values (?, ?, ?) Hibernate: insert into categories (name, category_id, id) values (?, ?, ?) Hibernate: insert into categories (name, category_id, id) values (?, ?, ?) Hibernate: insert into categories (name, category_id, id) values (?, ?, ?) Hibernate: insert into categories (name, category_id, id) values (?, ?, ?)   查询: public static void main(String[] args) throws Exception { Session session = sessionFactory.openSession(); Transaction tx = null; try { tx = session.beginTransaction(); Category category = (Category)session.get(Category.class, new Long(1)); System.out.println(category.getChildCategory().iterator().next().getName()); tx.commit(); } catch(Exception ex) { if(null != tx) { tx.rollback(); } ex.printStackTrace(); } finally { session.close(); } } 执行结果: Hibernate: select category0_.id as id2_0_, category0_.name as name2_0_, category0_.category_id as category3_2_0_ from categories category0_ where category0_.id=? Hibernate: select childcateg0_.category_id as category3_1_, childcateg0_.id as id1_, childcateg0_.id as id2_0_, childcateg0_.name as name2_0_, childcateg0_.category_id as category3_2_0_ from categories childcateg0_ where childcateg0_.category_id=? leval2 删除操作: public static void main(String[] args) throws Exception { Session session = sessionFactory.openSession(); Transaction tx = null; try { tx = session.beginTransaction(); Category category = (Category)session.get(Category.class, new Long(1)); // System.out.println(category.getChildCategory().iterator().next().getName()); session.delete(category); tx.commit(); } catch(Exception ex) { if(null != tx) { tx.rollback(); } ex.printStackTrace(); } finally { session.close(); } } 执行结果: Hibernate: select category0_.id as id2_0_, category0_.name as name2_0_, category0_.category_id as category3_2_0_ from categories category0_ where category0_.id=? Hibernate: select childcateg0_.category_id as category3_1_, childcateg0_.id as id1_, childcateg0_.id as id2_0_, childcateg0_.name as name2_0_, childcateg0_.category_id as category3_2_0_ from categories childcateg0_ where childcateg0_.category_id=? Hibernate: select childcateg0_.category_id as category3_1_, childcateg0_.id as id1_, childcateg0_.id as id2_0_, childcateg0_.name as name2_0_, childcateg0_.category_id as category3_2_0_ from categories childcateg0_ where childcateg0_.category_id=? Hibernate: select childcateg0_.category_id as category3_1_, childcateg0_.id as id1_, childcateg0_.id as id2_0_, childcateg0_.name as name2_0_, childcateg0_.category_id as category3_2_0_ from categories childcateg0_ where childcateg0_.category_id=? Hibernate: select childcateg0_.category_id as category3_1_, childcateg0_.id as id1_, childcateg0_.id as id2_0_, childcateg0_.name as name2_0_, childcateg0_.category_id as category3_2_0_ from categories childcateg0_ where childcateg0_.category_id=? Hibernate: select childcateg0_.category_id as category3_1_, childcateg0_.id as id1_, childcateg0_.id as id2_0_, childcateg0_.name as name2_0_, childcateg0_.category_id as category3_2_0_ from categories childcateg0_ where childcateg0_.category_id=? Hibernate: select childcateg0_.category_id as category3_1_, childcateg0_.id as id1_, childcateg0_.id as id2_0_, childcateg0_.name as name2_0_, childcateg0_.category_id as category3_2_0_ from categories childcateg0_ where childcateg0_.category_id=? Hibernate: select childcateg0_.category_id as category3_1_, childcateg0_.id as id1_, childcateg0_.id as id2_0_, childcateg0_.name as name2_0_, childcateg0_.category_id as category3_2_0_ from categories childcateg0_ where childcateg0_.category_id=? Hibernate: delete from categories where id=? Hibernate: delete from categories where id=? Hibernate: delete from categories where id=? Hibernate: delete from categories where id=? Hibernate: delete from categories where id=? Hibernate: delete from categories where id=? Hibernate: delete from categories where id=?  

上一篇:内存管理阅读
下一篇:开发笔记 那些年追过的图片(六):压缩图片并保存到沙盒里

相关文章

相关评论