zoukankan      html  css  js  c++  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="com.bjpowernode.hibernate.Category" table="t_category">
    		<id name="id">
    			<generator class="native"/>
    		</id>
    		<property name="name"/>
    		<many-to-one name="parentCategory" column="category_id" cascade="save-update"/>
    		<set name="childCategory" cascade="save-update">
    		    <key column="category_id"/>
    		    <one-to-many class="com.bjpowernode.hibernate.Category"/>
    		</set>
    	</class>
    </hibernate-mapping>
    package com.bjpowernode.hibernate;
    import java.io.Serializable;
    import java.util.HashSet;
    import java.util.Set;
    public class Category implements Serializable{
        private Integer id;
        private String name;
        private Category parentCategory;
        private Set childCategory = new HashSet();
    	public Integer getId() {
    		return id;
    	}
    	private void setId(Integer 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 getChildCategory() {
    		return childCategory;
    	}
    	public void setChildCategory(Set childCategory) {
    		this.childCategory = childCategory;
    	}   
    }


    工具类

    package com.bjpowernode.hibernate;
    
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.Configuration;
    
    public class HibernateUtils {
    
    	private static SessionFactory factory;
    	
    	static {
    		try {
    			//读取hibernate.cfg.xml文件
    			Configuration cfg = new Configuration().configure();
    			
    			//建立SessionFactory
    			factory = cfg.buildSessionFactory();
    		}catch(Exception e) {
    			e.printStackTrace();
    		}
    	}
    	
    	public static Session getSession() {
    		return factory.openSession();
    	} 
    	
    	public static void closeSession(Session session) {
    		if (session != null) {
    			if (session.isOpen()) {
    				session.close();
    			}
    		}
    	}
    	
    	public static SessionFactory getSessionFactory() {
    		return factory;
    	}
    }


    测试类:

    package com.bjpowernode.hibernate;
    
    import java.util.HashSet;
    
    import org.hibernate.Session;
    import junit.framework.TestCase;
    
    public class Test extends TestCase {
       public void addCategory(){
    	   //创建session
    	   Session session = null;
    	   try{
    		  session = HibernateUtils.getSession();
    		  session.beginTransaction();
    		  
    		  Category food = new Category();
    		  food.setName("food");
    		 		  
    		  Category vegetable = new Category();
    		  vegetable.setName("vegetable");
    		  
    		  Category fruit = new Category();
    		  fruit.setName("fruit");
    		
    		  Category tomato = new Category();
    		  tomato.setName("tomato");
    	
    		  Category apple = new Category();
    		  apple.setName("apple");
    	
    		  Category orange = new Category();
    		  orange.setName("orange");
    		  
    		  food.getChildCategory().add(vegetable);
    		  vegetable.setParentCategory(food);
    		  
    		  food.getChildCategory().add(fruit);
    		  fruit.setParentCategory(food);
    		  
    		  fruit.getChildCategory().add(apple);
    		  apple.setParentCategory(fruit);
    		  
    		  fruit.getChildCategory().add(orange);
    		  orange.setParentCategory(fruit);
    		  
    		  fruit.getChildCategory().add(tomato);
    		  tomato.setParentCategory(fruit);
    		  
    		  session.save(food);	  
    		  session.beginTransaction().commit();
    	   } catch(Exception e){
    		   session.beginTransaction().rollback();
    		   e.printStackTrace();
    	   } finally{
    		   HibernateUtils.closeSession(session);
    	   }
       }
       public void modifyCategory(){
    	   //创建session
    	   Session session = null;
    	   try{
    		  session = HibernateUtils.getSession();
    		  session.beginTransaction();
    		  
    		  Category tomato = (Category)session.get(Category.class, 5);
    		  Category vegetable = (Category)session.get(Category.class, 6);
    		  Category fruit = (Category)session.get(Category.class, 2);
     
    		  fruit.getChildCategory().remove(tomato);
    		  vegetable.getChildCategory().add(tomato);
    		  tomato.setParentCategory(vegetable);
    		  
    		  session.beginTransaction().commit();
    	   } catch(Exception e){
    		   session.beginTransaction().rollback();
    		   e.printStackTrace();
    	   } finally{
    		   HibernateUtils.closeSession(session);
    	   }
       }
    }
    


     

  • 相关阅读:
    华硕路由器修改 Hosts 以达到局域网内自定义解析
    一款开源、高颜值的终端terminus,支持Windows、MacOS
    Windows 10启用Linux子系统(WSL)
    一款全能的下载工具Motrix,支持BT、磁力链、百度网盘等资源
    ubuntu 14.04 和16.04 快速下载
    CentOS 7一键安装Seafile搭建私有云存储
    background背景色
    3d爱心代码
    Mac Mini(late 2014) 添加NVMe固态组Fusion Drive
    member access within misaligned address 0x0000002c3931 for type 'struct ListNode‘
  • 原文地址:https://www.cnblogs.com/dyllove98/p/3141287.html
Copyright © 2011-2022 走看看