Nhib1_trap: The Ghost object in the sequence-to-one Association

Source: Internet
Author: User
ArticleDirectory
    • Problem
    • Cause
    • Solution
Problem

The sequence-to-one association of nhibana supports polymorphism (should be), however,CodeIt falls into the trap. Considering this class chart, orderline has a product attribute, and product has two subclasses.

 
Ing of this association:
 
 
<Subtitle-to-one name = "product" class = "product" column = "productid"/>
 
 

Next, we need to execute a special business logic for the food order line:

 
Orderline line =...; If (line. Product is food) {// it will never be executed here
 
}
 

Even if the product of line is food, this judgment will not be true, and the business logic for food will never be executed.

Cause

The cause of this problem is that nhib.pdf uses the proxy object by default to implement delayed loading of slave-to-one,ProgramAfter running, the class structure is actually like this (the Purple class is the proxy class generated by nhib.pdf, the actual proxy class name is different from the figure ):

After the line object is constructed, its product property is not initialized. In fact, it is a productproxy instance (ghost object), while productproxy and food have no conversion relationship. The expression (line. product is food) is always false.

Solution

To avoid ghost object problems, you need to configure a little bit for the associated delayed loading. If you do not need to delay loading, you can disable:

<Subtitle-to-one name = "product" class = "product" column = "productid" lazy = "false"/>

Alternatively, if you still need to delay loading, you can use the no-proxy option to tell Nhibernate to still delay loading the Product Association, but do not use a proxy object.

 
<Your-to-one name = "product" class = "product" column = "productid" lazy = "no-proxy"/>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.