加入NHibernate的集合,而不初始化集合 [英] Add to Nhibernate collection without initializing the collection

查看:146
本文介绍了加入NHibernate的集合,而不初始化集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实体,称为表(想想一个电表)。

I have an entity called a Meter (Think an electric meter).

该仪器具有数据点的集合。每隔15分钟左右一新数据点被添加到米。该仪器具有这些数据点的集合。

The meter has a collection of Data Points. Every 15 minutes or so a new data point is added to the meter. The meter has a collection of these data points.

我想code这件事,如仪表myMeter = session.get<仪表>(ID)然后 myMeter。 AddDataPoint(点)然后 session.Update(myMeter)。内部仪表类有一个集合( IESI.ISet<数据点> )。但是加入到这个集合的时候我在想,NHibernate的是要首先要初始化集合,并从数据库中加载它。这个问题是存在的将是项目数十万在此集合,它只是不会是高性能。那么,是否可以code这件事让NHiberante会盲目添加到集合中,而无需加载所有的价值。

I would like to code this up such as Meter myMeter = session.get<Meter>(id) and then myMeter.AddDataPoint(point) then session.Update(myMeter). Internally the Meter class has a collection (IESI.ISet<DataPoint>). However when adding to this collection I am thinking that NHibernate is going to first have to initialize the collection and load it from the database. The problem is there are going to be hundreds of thousands of items in this collection and it just won't be performant. So is it possible to code this up so that NHiberante will blindly add to the collection, without loading all of its values.

该数据点类有一个多到一的映射到表,并在仪表上的数据点的一组映射。

The DataPoint class has a many-to-one mapping to the Meter, and the Meter has a set mapping of DataPoints.

推荐答案

1) 您可以从仪表类删除集映射。这样,您将永远不会被意外地加载成千上万个数据点命中。后来,当你需要知道所有的数据点为计,你可以动态加载。看起来你需要的数据点,大部分时间的子集(数据点米×为8月)。

1) You can remove set mapping from Meter class. This way you will never be hit with accidentally loading thousands data points. Later when you need to know all DataPoints for Meter you can load it dynamically. Looks like you will need subset of data points most of the time (data points for Meter X for August).

List<DataPoint> dp = DataPointsRepository.FindByMeter(Meter meter)
List<DataPoint> dp = DataPointsRepository.FindByMeter(Meter meter, DateRange range)

数据点仍需要多对之一,以关系表进行适当相关联并保存。

DataPoint will still need many-to-one relationship to meter in order to be properly associated and saved.

2)你可以尝试使用'袋',而不是'设置',但要小心,不要叫像obj.MyBag.Size方法() 相关的问题: <一href="http://stackoverflow.com/questions/1409523/hibernate-how-to-persist-a-new-item-in-a-collection-without-loading-the-entire">Hibernate - 如何坚持一个集合一个新的项目,而无需加载整个Collection

2) You can try to use 'bag' instead of 'set' but be careful to not call methods like obj.MyBag.Size() Related question: Hibernate - How to persist a new item in a Collection without loading the entire Collection

这篇关于加入NHibernate的集合,而不初始化集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆