These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Lazy loading not working
PostPosted: Sat Dec 19, 2009 12:55 am 
Newbie

Joined: Sat Dec 19, 2009 12:34 am
Posts: 2
Hi,
I have 3 projects (meaning 3 different assembly) like below
Symbonix.Web ---> The main asp.net website
Symbonix.Library ---> My code library
Symbonix.BLL -----> My project business logic classes

I put a configuration file called "nhibernate.cfg.xml" in the web root meaning ~/nhibernate.cfg.xml ( i customized the name from hibernate.cfg.xml )

I have some code in the Library to create the session like below
Code:
        public static ISessionFactory CurrentSessionFactory
        {
            get
            {
                if (HttpContext.Current.Items[CurrentSessionFactoryKey] == null)
                {
                    Configuration config = new Configuration().Configure(HttpContext.Current.Server.MapPath("~/nhibernate.cfg.xml")).SetNamingStrategy(ImprovedNamingStrategy.Instance);
                    config = AddAssemblies(config);
                    HttpContext.Current.Items[CurrentSessionFactoryKey] = config.BuildSessionFactory();
                }
                return (ISessionFactory)HttpContext.Current.Items[CurrentSessionFactoryKey];
            }
            private set
            {
                HttpContext.Current.Items[CurrentSessionFactoryKey] = value;
            }
        }
        public static ISession CurrentSession
        {
            get
            {
                if (HttpContext.Current.Items[CurrentSessionKey] == null)
                {
                    HttpContext.Current.Items[CurrentSessionKey] = CurrentSessionFactory.OpenSession();
                }
                return (ISession)HttpContext.Current.Items[CurrentSessionKey];
            }
            private set
            {
                HttpContext.Current.Items[CurrentSessionKey] = value;
            }
        }
        private static Configuration AddAssemblies(Configuration config)
        {
            foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
            {
                foreach (object attribute in assembly.GetCustomAttributes(true))
                {
                    if (attribute is NHibernateModule)
                    {
                        config.AddAssembly(assembly);
                        break;
                    }
                }
            }
            return config;
        }

Config file (bolded line is the direct mapping i used to use before, which is now removed as i am using lazy loading)
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory>
    <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="connection.connection_string_name">Default</property>
    <property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
    [b]<mapping assembly="Symbonix.BLL" />[/b]
  </session-factory>
</hibernate-configuration>


I have business classes that use the session to call simple operations, but when i used to map the assemblies directly in the config files, they worked fine, but when i used to lazy load, it seems the session is created properly as i can see the loaded classes list in it, but when i try to insert/load i get an error saying "Invalid object name '_role'." where Role is my table name i tried with many different tables and they give the same error.

the main exception is "could not load an entity: [Symbonix.BLL.Entities.Role#1][SQL: SELECT role0_.role_id as role1_97_0_, role0_.name as name97_0_ FROM [_role] role0_ WHERE role0_.role_id=?]"

the inner exception is "Invalid object name '_role'."

i tried a lot but not sure what is wrong.


Top
 Profile  
 
 Post subject: Re: Lazy loading not working
PostPosted: Sun Dec 20, 2009 5:40 am 
Newbie

Joined: Sat Dec 19, 2009 12:34 am
Posts: 2
Ok, i solved my problem,

what i was doing is,
Code:
Configuration config = new Configuration().Configure(HttpContext.Current.Server.MapPath("~/nhibernate.cfg.xml"))
.SetNamingStrategy(ImprovedNamingStrategy.Instance);
                   
config = config.AddAssembly(assembly);
config.BuildSessionFactory();


this creates problem, but it is solved in the following way,
Code:
Configuration config = new Configuration().Configure(HttpContext.Current.Server.MapPath("~/nhibernate.cfg.xml"));
config = config.AddAssembly(assembly);
config.SetNamingStrategy(ImprovedNamingStrategy.Instance).BuildSessionFactory();


so Adding Assembly after declaring SetNamingStrategy(ImprovedNamingStrategy.Instance) was the main problem according to my observation.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.