Thanks very much Fabio, it's good to know the nh masters will lend a
helping hand thx..

I had to figure it out myself in the end - that documentation is a bit
out of date,
and the blog post i found (at that stage) a bit too un-newbie for me,
(although now it would be fine)
as i've been playing with configuration a bit.  I did have trouble
with assemblies loading, in various situations  which i'm still not
sure why (i'll have another look and clarify that) but
thanks very much!

for any nhibernate configuration newbies in a similar position, am
posting a method that I'm certain works
to help you get moving.

what i've learned is that it's not as complicated as it looks, and is
actually designed to be flexible
and easy.

It is 3 steps:

1. setup configuration (db conection settings etc)
2. setup mappings (table column to class fields)
3. create a SessionFactory Object that contains your mapping
information.


configuration can be done 3 different ways:

1.  programatic  - using nibernate configutation classes in code.
2.  nhibernate xml file  (hibernate.cfg.xml)
3.  .net xml file (web.config, app.config)

note: when using a .net xml file (opt 3) the xml in the nhibernate
section can take two forms, a standard .net config form (<section
name="nhibernate"), or hibernate xml form (<section name="nhibernate-
configuration") where the markup used in the section matches that in
the hibernate.cfg.xml file in (opt 2).

In a standard nhibernate setup Mapping table columns to class fields
can be declared either by xml
mapping files class.hbm.xml, or by applying tags to class fields to
persist.

the exampe below works in nhibernate 2.1.2 with sql server 2005  (it
uses a hbm file for mapping)

NOTE: as of nhibrnate 2.0 you need to include a value for the
proxyfactory.factory_class property, there are three IoC libraries
included when you download nhibernate, whichever one you select you
have to also reference that dll in your project they are in the
'Required_For_LazyLoading' folder beneath the nhibernate folder.


1. CONFIGURATION

my example is a web.config with all but the nhibernate stuff removed.

-section name = hibernate-configuration, means the xml in the
nhibernate section is the hibernate format, not the .net config file
format.
- I've read (authoritatively) that connection.connection_string can
contain any standard ado.net connection string but i had to fiddle a
bit (though you do with ADO sometimes anyway :) )
- because I set the proxyfactory.factory_class property to
NHibernate.ByteCode.Castle.ProxyFactoryFactory,
NHibernate.ByteCode.Castle  I needed to add a reference to
Required_For_LazyLoading\Castle\NHibernate.ByteCode.Castle.dll

web.config

<?xml version="1.0"?>
<configuration>
        <configSections>
         <section name="hibernate-configuration"
 
type="NHibernate.Cfg.ConfigurationSectionHandler,NHibernate" />
        </configSections>

    <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
      <session-factory>
        <property name="connection.provider">
          NHibernate.Connection.DriverConnectionProvider
        </property>
        <property name="dialect">
          NHibernate.Dialect.MsSql2005Dialect
        </property>
        <property name="connection.driver_class">
          NHibernate.Driver.SqlClientDriver
        </property>
        <property name="connection.connection_string">
          Data Source=JS-MACBOOK\SQLEXPRESS;Initial Catalog=test;User
Id=sa;Password=notrealpassword;
        </property>
        <property name='proxyfactory.factory_class'>
            NHibernate.ByteCode.Castle.ProxyFactoryFactory,
NHibernate.ByteCode.Castle
        </property>
      </session-factory>
    </hibernate-configuration>

    <!-- log4net configuration settings not included but you should
use them asap -->


</configuration>


2.  MAPPING

a ridiculously simple mapping just to make it work.

USE [test]
GO
CREATE TABLE [dbo].[customer](
        [id] [int] IDENTITY(1,1) NOT NULL,
        [name] [nvarchar](50) COLLATE Latin1_General_CI_AS NULL
) ON [PRIMARY]


    Customer.cs

    public class Customer
    {
        private long id;
        private string name;

        public Customer()   {}  //required by nhibernate
        public virtual long Id
        {
            get { return id; }
            set { id = value; }
        }
        public virtual string Name
        {
            get { return name; }
            set { name = value; }
        }
    }

Customer.hbm.xml

(NOTE: place in same locatinn as Customer.cs, and via properties set
build action to 'emded as resource', and ammend namespace & assemply
name where indicated in capitals below')

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-
import="true">
  <class name="YOURNAMESPACE.Customer, YOURASSEMBLYNAME"
table="customer">
    <id name="Id"
         type="long"
         column="ID">
      <generator class="native"/>
    </id>
    <property name="Name">
      <column name="Name" length="255" not-null="true" />
    </property>
  </class>
</hibernate-mapping>


3. SESSIONFACTORY

theres a static property you can add to a class that can be referenced
from your test code,
and example usage of it.

using NHibernate;
using NHibernate.Cfg;
using System.Reflection;

 private static ISessionFactory sessionFactory = null;
        public static ISessionFactory MySessionFactory {
            get {
                if (sessionFactory == null) {
                    Configuration cfg = new Configuration();
                    cfg.Configure();

                    cfg.AddAssembly(Assembly.GetExecutingAssembly());
                    sessionFactory = cfg.BuildSessionFactory();
                }
                return sessionFactory;
            }
        }


        //somewhere else in same file/scope

        ISession session = MySessionFactory.OpenSession();

        Customer newcust = new Customer();
        newcust.Name = "Mr Smith";
        session.Save(newcust);


            string s = "";
            IQuery query = session.CreateQuery("from Customer as c");
            foreach ( Customer c in query.Enumerable()) {
                s+= " customer name: " + c.Name;
            }


     One your table and connection string have been created, and yor
namespace and assembly name
     added to the mapping file, you should be able to step through the
above and see it save data to the database and read it back from the
database.

     after that there's LOTS to play with.  hope that helps someone.




On Nov 29, 12:09 pm, Fabio Maulo <[email protected]> wrote:
> That is not a NH configuration... perhaps something else but not a NH
> configuration for 
> sure.http://fabiomaulo.blogspot.com/2009/06/nhibernate-configuration.html
>
> <http://fabiomaulo.blogspot.com/2009/06/nhibernate-configuration.html>http://nhforge.org/doc/nh/en/index.html#quickstart-intro
>
> 2009/11/28 jay <[email protected]>
>
>
>
> > My Error: The ProxyFactoryFactory was not configured.
> > Initialize 'proxyfactory.factory_class' property of the session-
> > factory configuration section with one of the available
> > NHibernate.ByteCode providers.
> > Example:
> > <property
>
> > I've seen other people report this error where they were getting
> > examples (propertys) in the error text , i dont.?
>
> > I've added a reference to NHibernate.ByteCode.Castle
>
> > and added the following line to app.config:
>
> >  <add key="proxyfactory.factory_class"
> > value="NHibernate.ByteCode.Castle.ProxyFactoryFactory,NHibernate.ByteCode.Castle"
> > /
>
> > I've spent ages trying to figure this out..if anyone has any ideas/
> > sugestions/help, it would be v helpful...
>
> > thx..
>
> > J .
>
> > p.s.    entire app.config =
>
> > <?xml version="1.0" encoding="utf-8" ?>
> > <configuration>
> >  <configSections>
> >    <section name="nhibernate"
> >    type="System.Configuration.NameValueSectionHandler,
> > System, Version=1.0.3300.0,Culture=neutral,
> > PublicKeyToken=b77a5c561934e089"
> > />
> >  </configSections>
> >  <nhibernate>
> >    <add key="hibernate.show_sql"
> >    value="false" />
> >    <add key="proxyfactory.factory_class"
> > value="NHibernate.ByteCode.Castle.ProxyFactoryFactory,NHibernate.ByteCode.Castle"
> > /
>
> >    <!-- <add key="proxyfactory.factory_class"
> > value="NHibernate.ByteCode.Castle.ProxyFactoryFactory,
> > NHibernate.ByteCode.Castle"/> -->
> >    <add key="hibernate.connection.provider"
> >    value="NHibernate.Connection.DriverConnectionProvider" />
> >    <add key="hibernate.dialect"
> >    value="NHibernate.Dialect.SQLiteDialect" />
> >    <add key="hibernate.connection.driver_class"
> >    value="NHibernate.Driver.SQLite20Driver" />
> >    <add key="hibernate.connection.connection_string"
> >    value="Data Source=C:\Users\Jerome\Desktop\SqlLiteBrowser
> > \comments;Version=3;New=False;Compress=True;" />
> >  </nhibernate>
> > </configuration>
>
> > --
>
> > You received this message because you are subscribed to the Google Groups
> > "nhusers" group.
> > To post to this group, send email to [email protected].
> > To unsubscribe from this group, send email to
> > [email protected]<nhusers%[email protected]>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/nhusers?hl=en.
>
> --
> Fabio Maulo

--

You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.


Reply via email to