Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
Hi,
I have a block of 50k data. I use NHibernate to retrieve all (retrieving all is necessary). But as having large dataset which is created by joining 5-7 tables NHibernate takes around one minute.
The main cause of slow fetching might be joining of tables for which NHibernate creates queryfor each row from each table. I understand it is necessary as NHibernate needs to map each row to an object, but this overhead must be removed.
 
Is there any way to fetch data in BLOCK and then creating objects using NHibernate.
I am including my mapping file as well as code -
Form Code
<pre lang="c#">
namespace NHibernateSample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            ConfigureNHibernate();
            LoadData();
        }
 
        static ISessionFactory SessionFactory;
        System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
 

        private void LoadData()
        {
            sw.Start();
 
            using (ISession session = SessionFactory.OpenSession())
            {
                long b = sw.ElapsedMilliseconds;
                try
                {
 
                    if (session.IsConnected)
                    { 
                        // as br order by br.BranchCode asc
                        IQuery query = session.CreateQuery("from Branch");
                        IList<Branch> iList = query.List<Branch>();
                        dvData.DataSource = iList;
                        int a = 0;
                        foreach (Branch br in iList)
                        {
                            a++;
                        }
                        MessageBox.Show(((sw.ElapsedMilliseconds - b)) + " - MilliSeconds to fetch " + System.Environment.NewLine + a.ToString() + " - Rows");
                    }
                   
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
 
                }
            }
        }
 

        private void ConfigureNHibernate()
        {
            try
            {
                Configuration cfg = new Configuration();
                cfg.Configure();
 
                Assembly allocationAssembly = typeof(Branch).Assembly;
                cfg.AddAssembly(allocationAssembly);
 
                SessionFactory = cfg.BuildSessionFactory();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
 
    }
}
 

Branch Class
using System.Collections.Generic;
using System.Text;
using System;
 

namespace NHibernateSample
{
    [Serializable]
    public class Branch
    {
        private String branchCode;
        private String branchName;
        private IList<Employee> employeeList = new List<Employee>();
 
        public virtual IList<Employee> EmployeeList
        {
            get { return employeeList; }
            set { employeeList = value; }
        }
        public virtual String BranchCode
        {
            get { return branchCode; }
            set { branchCode = value; }
        }
 
        public virtual String BranchName
        {
            get { return branchName; }
            set { branchName = value; }
        }
 
        public Branch() { }
    }
}
 


 
Employee Class
using System;
using System.Collections.Generic;
using System.Text;
 
namespace NHibernateSample
{
    public class Employee
    {
        String employeeId;
        String firstName;
        String lastName;
        String branchCode;
 
        public virtual String EmployeeId
        {
            get { return employeeId; }
            set { employeeId = value; }
        }
 
        public virtual String FirstName
        {
            get { return firstName; }
            set { firstName = value; }
        }
 
        public virtual String LastName
        {
            get { return lastName; }
            set { lastName = value; }
        }
 
        public virtual String BranchCode
        {
            get { return branchCode; }
            set { branchCode = value; }
        }
 
        public Employee()
        { }
    }
}
 
 
Employee.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernateSample" namespace="NHibernateSample">
  <class name="Employee" table="Employee">
	  <id name="EmployeeId"/>
	  <property name="EmployeeId"/>
	  <property name="FirstName"/>
	  <property name="LastName"/>
	  <property name="BranchCode"/>
  </class>
</hibernate-mapping>
 
Branch.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernateSample" namespace="NHibernateSample">
  <class name="Branch" table="Branch">
	  <id name="BranchCode"/>
	  <property name="BranchCode"/>
	  <property name="BranchName"/>
	  <bag name="EmployeeList" cascade="all-delete-orphan" inverse="false"  fetch="join" lazy="false">
		  <key column="BranchCode"/>
		  <one-to-many class="Employee" />
	  </bag>
  </class>
	
</hibernate-mapping>
 
App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
	<configSections>
		<section name="hibernate-configuration"	type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
	</configSections>
	<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
 
		<bytecode-provider type="lcg"/>
		<reflection-optimizer use="true"/>
		<session-factory>
			<property name="connection.provider" >
				NHibernate.Connection.DriverConnectionProvider
			</property>
			<property name="connection.driver_class">
				NHibernate.Driver.SqlClientDriver
			</property>
			<property name="connection.connection_string">
				Data Source=MyPC;Initial Catalog=NHibernateTest;Integrated Security=True;
			</property>
			<property name="dialect">
				NHibernate.Dialect.MsSql2005Dialect
			</property>
			<property name="show_sql">
				false
			</property>
			<property name='proxyfactory.factory_class'>NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
			
		</session-factory>
	</hibernate-configuration>
</configuration>
Posted 25-Jul-12 22:12pm
Edited 25-Jul-12 22:17pm
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Usually when you are doing such bulk transaction, it is advised to use stateless sessions. Here are two good links which might be helpful in your case.
 
http://darioquintana.com.ar/blogging/2007/10/08/statelesssession-nhibernate-without-first-level-cache/[^]
 
http://ayende.com/blog/4137/nhibernate-perf-tricks[^]
 

 
Good Luck!!
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 575
1 Kornfeld Eliyahu Peter 409
2 Maciej Los 369
3 DamithSL 196
4 OriginalGriff 188
0 OriginalGriff 6,353
1 DamithSL 4,854
2 Maciej Los 4,476
3 Kornfeld Eliyahu Peter 4,058
4 Sergey Alexandrovich Kryukov 3,917


Advertise | Privacy | Mobile
Web02 | 2.8.141223.1 | Last Updated 3 Aug 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100