ArticleDirectory
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"/>