谈eager跟lazy标签

今天又遇到了一个问题。这个项目是学长那边接手过来的,一开始学长也没想那么多


在所有的OnetoMany和ManytoOne里都加了 fetch = FetchType.EAGER


这也就导致了,我在找Plan的时候,自动级联查找了PlanPoint又级联查找了PlanPointService


这也间接导致了,越来越多的查找Plan,最后java heap space(Out Of Memory)


最后把fetch = FetchType.EAGER标签改成了fetch = FetchType.LAZY


但是这样的话,一些已经从session里面Plan,当session关闭的时候,就不能再使用Plan.getPlanPoints来获得Plan的子PlanPoint了。


所以要把方法都写到session里面去。


而且一个对象他的sessionfactory关闭之后,就不能再开启了,(一个BLL层不能调用两个同时都用到会关闭sf的方法,可能getCurrentSession可以,明天试试)


EAGER标签:当你进行 session.createQuery(sql);
查找一个主键别其他表作为外键的实体时候,会自动去查其他相关的表,相关表中又有其他有联系的则会继续寻找下去。
LAZY标签:查这张表就是查这张表,不会查他实体里set集合里的其他实体


当封装成json的时候,拥有set集合等属性的实体类,可能会造成死循环,原因跟上类似,不断的查找。


这时候我们一般就会用过滤器(还是很方便的),或者在获得要封装的类时候,把他的set集合设置成null


例如:
student.setTeachers(null);


这样封装的时候就不会造成死循环了。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
In this article I want to discuss the lazy loading mechanism provided by NHibernate. It is recommended for maximum flexibility to define all relations in your domain as lazy loadable. This is the default behavior of NHibernate since version 1.2. But this can lead to some undesired effects if querying your data. Let's discuss these effects and how to avoid them. Term Paper AND Research Paper In my previous posts I showed how to prepare your system for NHibernate and how to implement a first NHibernate base application. This post is based on those two articles. Thesis AND Dissertation AND Essay The Domain Let's first define a simple domain. It shows part of an order entry system. I keep this model as simple as possible (a real domain model would be more complex) but it contains all aspects we want to discuss in this post. Below is the class diagram of our model We have an order entity which can be placed by a customer entity. Each order can have many order line entities. Each of the three entity types is uniquely identified by a property Id (surrogate key). The Mapping Files We have to write one mapping file per entity. It is recommended that you always have one mapping per file. Don't forget to set the Build Action of each mapping file to Embedded Resource. People often tend to forget it and the subsequent errors raised by NHibernate are not always obvious. Also do not forget to give the mapping files the correct name, that is *.hbm.xml where * denotes the placeholder for the entity name. The mapping for the Order entity might be implemented as follows <?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="LazyLoadEagerLoad" namespace="LazyLoadEagerLoad.Domain"> <class name="Order" table="Orders"> <id name="Id"> <generator class="guid"/> </id> <property name="OrderNumber"/> <property name="OrderDate"/> <many-to-one name="Customer" /> <set name="OrderLines" cascade="all-delete-orphan" > <key column="OrderId"/> <one-to-many class="OrderLine"/> </set> </class> </hibernate-mapping>Analogous you can implement the mappings for the Customer entity <?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="LazyLoadEagerLoad" namespace="LazyLoadEagerLoad.Domain"> <class name="Customer"> <id name="Id"> <generator class="guid"/> </id> <property name="CompanyName"/> </class> </hibernate-mapping>and finally the mapping for the OrderLine entity. <?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="LazyLoadEagerLoad" namespace="LazyLoadEagerLoad.Domain"> <class name="OrderLine"> <id name="Id"> <generator class="guid"/> </id> <property name="Amount"/> <property name="ProductName"/> </class> </hibernate-mapping>

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值