【问题标题】:Cannot understand lazy loading properly in ORM technology无法正确理解 ORM 技术中的延迟加载
【发布时间】:2014-11-18 16:09:23
【问题描述】:

我是 NHibernate 的新手。我在 sql server 中创建了两个表,例如 UserDetails 及其对应的 Products 表。映射就像! 我的实体类

公共类 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; }
}

公开课产品 {

    [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

<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

  <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);

我以为我只会从 userdetails 获取数据,因为它应用了延迟加载,但在调试模式下我也可以从 products 表中看到数据。为什么?我是否以错误的方式理解延迟加载,或者我的映射中是否有任何错误?据我所知,延迟加载仅按需加载数据意味着如果我们进行迭代,则数据将从产品表中加载。

【问题讨论】:

  • 通过访问调试器中的属性,NHibernate 可能会为您从数据库中获取信息。你应该附上一个分析器来看看到底发生了什么

标签: nhibernate lazy-loading


【解决方案1】:

你的假设:

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

绝对正确。没有但是。仅在需要时,例如如果我们想要迭代它们。而这正是在调试窗口中发生的。

调试窗口是应用程序,就像其他一些 winform、exe、wpf... 它只是提供原生/内置 UI 来观察我们的对象。一旦我们开始观察/迭代它们 - 他们意识到有需求 - 来自 products 表的数据被加载。

如何确定?

没那么复杂。在开始观察调试窗口中的任何对象之前,请调用:session.Clear()。从那一刻起,只有已经加载的内容才会在以后可用。

所以,在调试窗口中,我们现在应该会看到一些关于延迟加载失败的异常...

【讨论】:

  • 非常感谢。你救了我。
猜你喜欢
  • 2016-05-23
  • 2014-01-21
  • 2016-02-26
  • 1970-01-01
  • 1970-01-01
  • 2012-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多