NHibernate Interview Questions & Answers

NHibernate Interview Questions

Are you an expert in computer applications and to ready to create a path in the software industry? Have a view in our wisdom jobs for searching and for finding solutions to walk for a place where the offer was. NHibernate jobs were one of the solutions who are experts in software field. NHibernate gives mapping information in XML format. This file is used to map the entities to the corresponding database tables. It enables you to use LINQ to query data on top of the NHibernate engine. Explore your needs by visiting NHibernate jobs interview questions and answers page to get more information and to get strong for face an interview. Log on to our site to get more details and offers to your standards.


NHibernate Interview Questions And Answers

NHibernate Interview Questions
    1. Question 1. Define Cascade In Nhibernate Mapping File?

      Answer :

      Cascade:  enable operations to cascade to child entities.

      cascade=”all | none | save-update | delete | all-delete orphan”

      all: when an object is save/update/delete, check the associations  and save/update/delete all objects found.

      none: do not do any cascades, let user handles them by themselves.

      save-update: when the object is saved/updated, check the associations and save/update any object that require it.

      delete: when the object is deleted, delete all the objects in the association.

      all-delete-orphan: when an object is save/update/delete, check the associations and save/update/delete all objects found. in addition to this, when an object is removed from the association and not associated with another object, delete the orphan object also.

    2. Question 2. Explain About Nhibernate Mapping Files?

      Answer :

      Mapping files form core of any database. These files contain field to field mapping, usually this mapping occurs between classes and attributes.

      Sample mapping file:

      <?xml version="1.0" encoding="utf-8" ?>
      <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" 
        <class name="FirstSample">
          <id name="Id">
            <generator class="native" />
          <property name="Name" />
          <property name="Category" />

      No need to specify column name if database column name is same as property .

      <property name="Name" column="database-column-name" />

      Also, right-click on "FirstSample.hbm.xml", select "Properties" and change the "Build Action" to "Embedded Resource". 

      The mapping file will now be a part of the Asssembly.

    3. Question 3. How Do You Switch Between Relational Databases Without Code Changes?

      Answer :

      By using hibernate SQL dialects, we can switch databases. Hibernate will generate appropriate hql queries based on the dialect defined.

    4. Question 4. What Is Difference Between Session.get() And Session.load()?

      Answer :

      Both Session.Get() and Session.Load() method create a persistent object by loading the required object from the database. But if there was not an object in database, then ‘Load’ method throws an exception where as Get method returns null.

    5. Question 5. How To Configure Nhibernate?

      Answer :

      Configuration files and mapping files(.hbm.xml) are used by Configuration class to create  the SessionFactory, which in turn creates the Session instance . Session instance are primary interface for persistence service.

      config file is used to specify connection details and mapping files used to map persistant objects to a relational database. It is best practice to store each object in an individual mapping file.

      SessionFactory is nhibernate concept of a single datastore and is threadsafe so that many threads can access it concurrently, SessionFactory is usually built only once at startup

    6. Question 6. What Is Nhibernate.burrow?

      Answer :

      Burrow is a light weight middleware developed to support .Net applications using NHibernate (maybe also referred as NH in this article) as ORM framework. Using Asp.net with NHibernate could be a challenge because of the fact that NHibernate is a stateful environment while Asp.net is a stateless framework. Burrow can help solve this conflict by providing advanced and smart session/transaction management and other facilitates.

    7. Question 7. What's The Major Benefits Offered By Nhibernate.burrow?

      Answer :

      • Burrow Conversation with which you can easily write business transaction
      • GenericDAO with which ISession can be seldom touched most of the time
      • Stateful field attributes with which marked fields of Asp.net UserControl and Page will be stateful over multiple http requests. It helps UserControls or Pages remember entity without causing lazy load exception.
      • Multiple databases support multiple databases becomes easy, you don't need a SessionFactoryPath, you simply give Burrow an entity type, and Burrow will find the right ISession for you. If you are using GenericDAO, the multiple DB support can be very transparent - your code does not need to know there are multiple databases.

    8. Question 8. How Do I Learn More About Nhibernate.burrow?

      Answer :

      • Introduction - a brief introduction.
      • Get Started - tells how to setup the Burrow framework.
      • Burrow Conversation Explained - gives more detail in long conversation.
      • StatefulField Attributes - explains how Burrow.WebUtil attributes can simplify states maintenance for ASP.NET controls and pages

    9. Question 9. Why Did I Get This "cannot Find Entity With Id X" Error With My [entityfield] After I Deleted It?

      Answer :

      That's because by default EntityField is using Session.Load() to load the entity for you. If you delete the entity, you need to reset that field to null, if you want to save this trouble, you can use [EntityFieldDeletionSafe] instead, it's basically the same except that it's using Session.Get().

    10. Question 10. How Do I Store Data In Httpsession?

      Answer :

      You might already know that you can not store entity in HttpSession in an OpenSessionPerView mode which is what NHibernate.Burrow is using most of the cases. Instead, you can store entity id in session and load the entity with Id every time you use it. Thanks to NHibernate's cache, this won't bring any performance issue to your system.

    11. Question 11. How Do I Do Unit Test Under Nhibernate.burrow?

      Answer :


      public void Initialize() {
        new BurrowFramework().InitWorkSpace();
      public void Close() {
        new BurrowFramework().CloseWorkSpace();

      Or you can use NHibernate.Burrow.TestUtil.TestBase as your NUnit unit test base class and override TearDowan() and SetUp() method to add your own TearDown and SetUp logic.

    12. Question 12. Is Nhibernate.burrow Compatible With Ajax?

      Answer :

      NHibernate.Burrow is compatible with Ajax.Asp.net - the ajax framework provided by Microsoft.

    13. Question 13. Why I Couldn't Finish A Long Conversation In An Updatepanel In An Ajax.asp.net Project?

      Answer :

      If you keep getting a javascript alert window giving the error message "Sys.WebForms.PageReqeustManagerParserErrorExcpetion: The message received from the server could not be parsed. Common causes for this error are when the response is modified by calls to Response.Write(), response filters, HttpModules, or server trace is enabled." you need to add the following setting into you system.web section in your web.config

      <pages enableViewStateMac="false" viewStateEncryptionMode="Never" enableEventValidation="false">

    14. Question 14. What Version Of Nhibernate Is Nhibernate.burrow Using?

      Answer :

      Burrow 1.0 alpha 1 is using NHibernate 2.0 Alpha 1.

    15. Question 15. What Version Of .net Framework Does Nhibernate.burrow Support?

      Answer :

      NHibernate.Burrow currently only works in .Net Framework 2.0

    16. Question 16. Does Nhibernate.burrow Create Session And Transaction For Every Http Request?

      Answer :

      Some existing OpenSessionPerView supporting HttpModule solutions create unnecessary session and transaction for every http request including requests for digital assets such as picture and css file. NHibernate.Burrow only creates session and transaction for http request handler that needs.

    17. Question 17. How Do I Change Nhibernate Configuration Within Burrow On The Fly?

      Answer :

      you can use the following code:

      IFrameworkEnvironment fe = new BurrowFramework().BurrowEnvironment; 

      NHibernate.Cfg.Configuration cfg =  fe.GetNHConfig("PersistenceUnit1"); 


      //do whatever change you need to cfg 


    18. Question 18. What Version Of Hibernate Is Nhibernate Ported From?

      Answer :

      NHibernate 1.0 is a port of Hibernate 2.1. Some obscure features of Hibernate 2.1 are missing from NHibernate and a few simple features from Hibernate 3 are present. 


    19. Question 19. I Get An Error Saying No Persisters Found For Myclass?

      Answer :

      This often happens when there is a typo in a mapping file, or when you forget to set the build action for the file to Embedded Resource in Visual Studio .NET.

    20. Question 20. I've Changed The Mapping File, But Still Get The Same Error I Was Getting Before - Why Isn't Nhibernate Reading The New Mapping File?

      Answer :

      If you compile the mapping files into an assembly as an /Embedded Resource (my personal recommendation) and you use Visual Studio .NET 2003 for development, then you need to rebuild the project, not just build it if all that was changed is a hbm.xml file. VS.NET will not rebuild a project if only an embedded resource has changed - so the assembly still contains the old hbm.xml file.

    21. Question 21. Why Doesn't My Proxy Work?

      Answer :

      To be able to create a proxy for a class, the class should have its properties, methods and events declared as virtual. This is not required if the class is proxied through an interface it implements (specified using proxy="SomeInterface"). There is no code in NHibernate 1.0 to validate your proxy classes, but that will be added in NHibernate 1.1.

      NHibernate uses the library Castle.DynamicProxy that is part of the Castle Project. DynamicProxy generates IL - not modifying your IL - at runtime for a subclass of your class, or the IL to implement an interface.

    22. Question 22. Does Nhibernate Modify My Il? If Not, How Do You Know What Has Changed?

      Answer :

      No, it does not modify your IL in anyway. Your IL remains exactly as it was when you compiled it.

      When an object is loaded by NHibernate the ISession keeps a snapshot of the state of your object. When you Flush() the ISession NHibernate compares that snapshot to the current state of the object. The appropriate changes are written to the database.

    23. Question 23. How Do I Translate The Nhibernate Collections To .net Collections?

      Answer :

      The names of the collection mappings is one obvious place where the differences between Java and .NET are shown. Java's collection library has many more options than System.Collection does.

      • The <list> maps directly to an IList.
      • The <map> maps directly to an IDictionary.
      • The <bag> maps to an IList. A <bag> does not completely comply with the IList interface because the Add() method is not guaranteed to return the correct index. An object can be added to a <bag> without initializing the IList. Make sure to either hide the IList from the consumers of your API or make it well documented.
      • The <set> maps to an Iesi.Collections.ISet. That interface is part of the Iesi.Collections assembly distributed with NHibernate.

      If an order-by attribute is added to the collection mapping element then the concrete collection type will change to one that supports maintaining the order an element was added. For a <bag> it still uses an ArrayList. For the <map> and <set> that have an order-by attribute the concrete collection class changes to a Specialized.ListDictionary and Iesi.Collections.ListSet, respectively. This should be a non-issue since the consumers of your API are using the interfaces IList and Iesi.Collections.ISet.

      If a sort attribute is added to the collection mapping element then the concrete collection type will change to one that supports ordering. For the <map> and <set> that have a sort attribute the concrete collection class changes to a SortedList or Iesi.Collections.SortedSet, respectively, that uses the IComparer provided in the sort attribute. This should be a non-issue to the consumers of your API since they are using the interfaces IList and Iesi.Collections.ISet.

      Using an order-by or sort attribute does have implications on performance for maps and sets as the size of the collections grows. Please read the MSDN documentation about the ListDictionary and SortedList performance implications. There are also some good articles on The Code Project about collections.

    24. Question 24. What Is A Persistent Collection?

      Answer :

      This term is used in NHibernate to refer to a collection of its internal class (NHibernate.Collections.Set, List, Bag, etc.) which wraps a user's collection. All collections belonging to entities returned by NHibernate are persistent (= wrapped). Entities passed to Save also have their collections wrapped and replaced by the wrappers as a part of the saving process. Persistent collections have a snapshot of their original state and can therefore be updated more efficiently.

      Collections containing query results are not persistent, they are plain .NET collections (=ArrayList=s).

    25. Question 25. Nhibernate Is Deleting My Entire Collection And Recreating It Instead Of Updating The Table?

      Answer :

      This generally happens when NHibernate can't figure out which items changed in the collection. Common causes are:

      • replacing a persistent collection entirely with a new collection instance
      • passing NHibernate a manually constructed object and calling Update on it.
      • serializing/deserializing a persistent collection apparently also causes this problem.
      • updating a <bag> with inverse="false" - in this case, NHibernate can't construct SQL to update an individual collection item.

      Thus, to avoid the problem:

      • pass the same collection instance that you got from NHibernate back to it (not necessarily in the same session),
      • try using some other collection instead of <bag> (<idbag> or <set>), or
      • try using inverse="true" attribute for <bag>.

Popular Interview Questions

All Interview Questions

All Practice Tests

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

NHibernate Tutorial