Set the lazyload for the same object in nhib.pdf to be consistent.

Source: Internet
Author: User

In nhibload, lazyload is often used to load associated objects for performance considerations. For more information about lazyload and how to use it, seeArticle, Such as:

We often use the Nhibernate. bytecode. Castle dynamic proxy to implement lazyload.ProgramThe Assembly creates the proxy class corresponding to the object at runtime. If the object uses an inheritance relationship and ing through nhib, the dynamic proxy class may have problems during type conversion.

For example, a node class is defined in C #, and a State class is inherited from the node class, followed by flow. node references this class, and I am still in the task. node references this class. Lazyload is enabled by default. When a Node object is obtained through the flow object, the 'castle. proxies. nodeproxy' object is returned, and this object cannot be converted to a State object. Therefore, the following exception is thrown:

Unable to cast object of Type 'castle. proxies. nodeproxy' to type 'xxx. Core. model. Definition. state '.

So what should we do? Only lazyload is not used, so I changed the Loading Method of flow. node to immediate loading, and then this exception disappeared. However, we are calling another method today and then calling flow. node, this exception occurs again, which makes me very depressed. I explicitly specified flow. if node is loaded immediately, how can it be converted to nodeproxy by the system?

After research, it is found that task. node is not set to load immediately. I'm calling flow. node previously called the task object, so Nhibernate sets node as a dynamic proxy class based on the mapping settings of the task, caches node into the session, and then calls flow. when a node is created, the system first checks whether there is a corresponding Node object in the cache, and the result is the cache of nodeproxy. Therefore, nodeproxy is directly returned to the flow. node. The system does not check the flow at all. is node Loaded Immediately or lazy.

There are two ways to solve this problem. One is to set task. node to load immediately, so no nodeproxy object is in the cache. The other method is not to reference the Node object in the task. I use the second method. In task, I only need the task. nodeid is enough. You do not need to load the Node object.

In general, if the inheritance class is used in Nhibernate and lazyload is used at the same time, an exception may be thrown during type conversion. The solution is to cancel lazyload and load it immediately. If multiple objects reference this object, you need to set the lazyload method referenced by this object to the same one. If you do not use lazyload or reduce references to the object.

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: 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.