无法正确理解ORM技术中的延迟加载 [英] Cannot understand lazy loading properly in ORM technology

查看:78
本文介绍了无法正确理解ORM技术中的延迟加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是NHibernate的新手.我已经在SQL Server中创建了两个表,如UserDetails及其对应的Products表.映射就像! 我的实体课

I am new to NHibernate. I have created two tables in sql server like UserDetails and its corresponding Products table. The mapping are like! My entity classes

公共类UserDetails {

public class UserDetails {

    [Required(ErrorMessage = "Please enter User Id")]
    public virtual string UserId { get; set; }
    [Required(ErrorMessage = "Please enter User Name")]
    public virtual string UserName { get; set; }
    [Required(ErrorMessage = "Please enter Password")]
    public virtual string Password { get; set; }
    [Required(ErrorMessage = "Please enter Email Id")]
    [EmailAddress(ErrorMessage = "Please enter proper Email Id")]
    public virtual string EmailId { get; set; }
    [Required(ErrorMessage = "Please enter Address")]
    [DataType(DataType.MultilineText)]
    public virtual string Address { get; set; }
    [Required(ErrorMessage = "Please enter Phone No")]
    public virtual string PhoneNo { get; set; }

    public virtual ISet<Product> Products { get; set; }
}

公共类产品 {

public class Product {

    [HiddenInput(DisplayValue=false)]
    public virtual int ProductID { get; set; }
    [Required(ErrorMessage="Please enter a product name")]
    public virtual string Name { get; set; }
    [DataType(DataType.MultilineText)]
    [Required(ErrorMessage = "Please enter a description")]
    public virtual string Description { get; set; }
    [Required]
    [Range(0.01,double.MaxValue,ErrorMessage="Please enter a positive price")]
    public virtual decimal Price { get; set; }
    [Required(ErrorMessage = "Please specify a category")]
    public virtual string Category { get; set; }
    public virtual byte[] ImageData { get; set; }
    [HiddenInput(DisplayValue=false)]
    public virtual string ImageMimeType { get; set; }
    public virtual UserDetails UserDetail { get; set; }
}

映射 UserDetails.hbm.xml

Mapping UserDetails.hbm.xml

<class name="SportsStore.Domain.Entities.UserDetails,SportsStore.Domain" table="UserDetails" lazy="true" >
<id name="UserId" column="UserId" access="property" type="String" >
  <generator class="assigned"></generator>
</id>
<property name="UserName" column="UserName" access="property" type="String" ></property>
<property name="Password" column="Password" access="property" type="String"></property>
<property name="EmailId" column="EmailId" access="property" type="String" ></property>
<property name="Address" column="Address" access="property" type="String" ></property>
<property name="PhoneNo" column="PhoneNo" access="property" type="String"  ></property>
<set name="Products" lazy="true" cascade="all-delete-orphan" inverse="true" >
  <key column="UserId" ></key>
  <one-to-many class="SportsStore.Domain.Entities.Product"></one-to-many>
</set>

Products.hbm.xml

Products.hbm.xml

  <class name="SportsStore.Domain.Entities.Product,SportsStore.Domain" table="Products">
<id name="ProductID" column="ProductID" access="property" type="Int32" >
  <generator class="native"></generator>
</id>
<property name="Name" column="Name" access="property" type="String">
</property>
<property name="Description" column="Description" access="property" type="String"></property>
<property name="Price" column="Price" access="property" type="decimal"></property>
<property name="Category" column="Category" access="property" type="String"></property>
<property name="ImageData" column="ImageData" access="property" type="BinaryBlob" length="2147483647"></property>
<property name ="ImageMimeType" column="ImageMimeType" access="property" type="String" ></property>
<many-to-one name="UserDetail" column="UserId" class="SportsStore.Domain.Entities.UserDetails" access="property" cascade="all"></many-to-one>

var user = (from userDetails in _session.Query<UserDetails>()
                    where userDetails.UserId == userId && userDetails.Password == password
                    select userDetails);

我以为,由于将延迟加载应用于用户详细信息,因此只能从用户详细信息中获取数据,但是在调试模式下,我也可以从产品表中查看数据.为什么?我是否理解以错误的方式进行的延迟加载,或者映射中是否存在任何错误?据我了解,延迟加载仅按需加载数据,这意味着如果我们进行迭代,则会从产品表中加载数据.

I thought I will get data from only userdetails as lazy loading is applied to it, but on debug mode I can see data from products table too. why? am I understood lazy loading in wrong way or is there any bug in my mapping? According to my knowledge lazy loading loads the data only on demand means if we iterate then the data gets loaded from products table.

推荐答案

您的假设:

...据我所知,延迟加载仅按需加载数据,这意味着如果我们进行迭代,则数据将从产品表中加载

是绝对正确的.没有.仅按需提供,例如,如果我们要对其进行迭代.而这正是在调试窗口中发生的.

Is absolutely correct. There is no but. Only on demand, for example if we want to iterate them. And exactly that is happening in the debug window.

调试窗口是与其他winform,exe,wpf一样的应用程序...它只是简单地在UI中内置/内置以观察我们的目标.一旦我们开始观察/迭代它们-他们意识到有需求-从产品表中加载数据.

Debug window is application as some other winform, exe, wpf... It simply prvides native/built in UI to observe our objcets. And once we start to observe/iterate them - they realize that there is demand - data from products table is loaded.

如何确定?

没那么复杂.在开始观察调试"窗口中的任何对象之前,请致电: session.Clear() .从那时起,以后将仅提供已加载的内容.

Not so complicated. Just before starting to observe any object in the Debug window, call: session.Clear(). From that moment, only stuff already loaded will be available later.

因此,在调试窗口中,我们现在应该看到有关延迟加载失败 ...

这篇关于无法正确理解ORM技术中的延迟加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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