开发者社区> 问答> 正文

Hibernate one-to-many 怎么取消代理

Hibernate中默认对one-to-many 的set集合采用的是lazy="true",这样当没有使用到该集合数据时,它只是一个代理。正是因为这个原因,当我的后台将这个对象使用JSON发送到前台时,因为使用的是struts2的自带的JSON,所以会出现session close的错误,当然我可以配置一个session view来强制打开session,但这样性能明显下降,因为那个代理对象中的数据我根本没用。如果我手动把代理对象设置成null是可以解决的,不过这样太过繁琐,求有什么办法,可以取消set的代理

展开
收起
云栖技术 2016-06-02 15:12:22 2229 0
1 条回答
写回答
取消 提交回答
  • 社区爱好者,专为云栖社区服务!

    1数据库开发中经常会遇到一对多的关系,例如客户与订单的关系,一个客户对应多个订单。
    开发一个customer与orders的demo

    (1)领域模型
    customer
    [java] view plain copy
    package com.songxu.domain;  
      
    import java.io.Serializable;  
    import java.util.Set;  
      
    public class Customer implements Serializable  
    {  
        private int id;  
        private String name;  
        private Set orders;  
        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 getOrders()  
        {  
            return orders;  
        }  
        public void setOrders(Set orders)  
        {  
            this.orders = orders;  
        }  
      
    }  
    
    orders
    [java] view plain copy
    package com.songxu.domain;  
      
    import java.io.Serializable;  
    import java.sql.Timestamp;  
      
    public class Order implements Serializable  
    {  
        private int id;  
        private Timestamp time;  
          
        private Customer customer;  
      
        public int getId()  
        {  
            return id;  
        }  
      
        public void setId(int id)  
        {  
            this.id = id;  
        }  
      
        public Timestamp getTime()  
        {  
            return time;  
        }  
      
        public void setTime(Timestamp time)  
        {  
            this.time = time;  
        }  
      
        public Customer getCustomer()  
        {  
            return customer;  
        }  
      
        public void setCustomer(Customer customer)  
        {  
            this.customer = customer;  
        }  
      
    }  
    
    (2) hbm 映射文件
    
    
    customer
    [html] view plain copy
    <?xml version="1.0" encoding="UTF-8"?>  
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
                                       "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">  
    <hibernate-mapping>  
     <class name="com.songxu.domain.Customer" table="customer">  
      <id column="id" name="id" type="int">  
       <generator class="increment"/>  
      </id>  
      <!-- column同名时可以省略 -->  
      <property column="name" generated="never" lazy="false" name="name" type="string"/>  
      <set name="orders" cascade="save-update" inverse="true" >  
        
      <key column="customer" ></key>  
      <one-to-many class="com.songxu.domain.Order"/>  
      </set>  
     </class>  
    </hibernate-mapping>  
    
    order
    
    [html] view plain copy
    <?xml version="1.0" encoding="UTF-8"?>  
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
                                       "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">  
    <hibernate-mapping>  
     <class name="com.songxu.domain.Order" table="orders">  
      <id column="id" name="id" type="int">  
       <generator class="increment"/>  
      </id>  
      <!-- column同名时可以省略 -->  
      <property column="occurTime" generated="never" lazy="false" name="time" type="timestamp"/>  
      <many-to-one name="customer" class="com.songxu.domain.Customer" column="customer_id"></many-to-one>  
          
     </class>  
    </hibernate-mapping>  
    
    2 自身一对多的例子,如层次关系
    (1) 领域模型
    [java] view plain copy
    package com.songxu.domain;  
      
    import java.util.Set;  
      
    public class Food  
    {  
        private int id;  
        private String name ;  
        private Food parentFood;  
        private Set<Food> childFood;  
        public int getId()  
        {  
            return id;  
        }  
        public Food()  
        {  
              
        }  
        public Food(String name, Food parentFood, Set<Food> childFood)  
        {  
          
            this.name = name;  
            this.parentFood = parentFood;  
            this.childFood = childFood;  
        }  
        public void setId(int id)  
        {  
            this.id = id;  
        }  
        public String getName()  
        {  
            return name;  
        }  
        public void setName(String name)  
        {  
            this.name = name;  
        }  
        public Food getParentFood()  
        {  
            return parentFood;  
        }  
        public void setParentFood(Food parentFood)  
        {  
            this.parentFood = parentFood;  
        }  
        public Set<Food> getChildFood()  
        {  
            return childFood;  
        }  
        public void setChildFood(Set<Food> childFood)  
        {  
            this.childFood = childFood;  
        }  
      
    }  
    
    (2) 映射文件
    [html] view plain copy
    <?xml version="1.0" encoding="UTF-8"?>  
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
                                       "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">  
    <hibernate-mapping>  
        <class name="com.songxu.domain.Food" table="food">  
            <id column="id" name="id" type="int">  
                <generator class="increment" />  
            </id>  
            <!-- column同名时可以省略 -->  
            <property column="name" generated="never" lazy="false" name="name"  
                type="string" />  
      
            <many-to-one name="parentFood" class="com.songxu.domain.Food"  
                column="foodId"></many-to-one>  
            <set name="childFood" cascade="all">  
                <key column="foodId"></key>  
                <one-to-many class="com.songxu.domain.Food" />  
      
      
            </set>  
      
      
        </class>  
    </hibernate-mapping>  
    
    (3) 测试代码
    [java] view plain copy
    package com.songxu.test;  
      
    import java.util.HashSet;  
      
    import org.hibernate.Session;  
    import org.hibernate.SessionFactory;  
    import org.hibernate.Transaction;  
    import org.hibernate.cfg.Configuration;  
      
    import com.songxu.domain.Food;  
      
    public class FoodTest  
    {  
        static Configuration configuration;  
        static SessionFactory sessionFactory;  
        static  
        {  
            configuration=new Configuration().configure();  
            sessionFactory=configuration.buildSessionFactory();  
        }  
        public static void main(String[] args)  
        {  
            Session session=sessionFactory.openSession();  
            Transaction tx=null;  
            try  
            {  
                tx=session.beginTransaction();  
                  
                  
                Food food=new Food("食品",null,new HashSet<Food>());  
                  
                Food fruit=new Food("水果",null,new HashSet<Food>());  
                Food vegetables=new Food("蔬菜",null,new HashSet<Food>());  
                  
                food.getChildFood().add(fruit);  
                food.getChildFood().add(vegetables);  
                fruit.setParentFood(food);  
                vegetables.setParentFood(food);  
                  
                  
                Food fruit1=new Food("苹果",null,new HashSet<Food>());  
                Food fruit2=new Food("香蕉",null,new HashSet<Food>());  
                Food v1=new Food("黄瓜",null,new HashSet<Food>());  
                Food v2=new Food("茄子",null,new HashSet<Food>());  
                  
                fruit.getChildFood().add(fruit1);  
                fruit.getChildFood().add(fruit2);  
                fruit1.setParentFood(fruit);  
                fruit2.setParentFood(fruit);  
                  
                  
                  
                vegetables.getChildFood().add(v1);  
                vegetables.getChildFood().add(v2);  
                v1.setParentFood(vegetables);  
                v2.setParentFood(vegetables);  
                  
                //仅保存根对象就可以了  
                session.save(food);  
                  
                  
                //session.get(Food.class,1);  
                tx.commit();  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
            }  
            catch (Exception e)  
            {  
                  
                if(null!=tx)  
                {  
                    tx.rollback();  
                }  
                e.printStackTrace();  
              
            }  
              
            finally  
            {  
                session.close();  
            }  
        }  
      
    2019-07-17 19:24:58
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载