As I make mistakes in the example, I will put the full source. In that
case, the data was populated in the database but the result o the
WriteLine is 0.
This is just a sample to learn about NHibernate (the coded
configuration is for that reason).

****************************** Main program:

            Configuration configuration = new Configuration();
            configuration.SetProperty("connection.driver_class",
"NHibernate.Driver.SqlClientDriver");
            configuration.SetProperty("dialect",
"NHibernate.Dialect.MsSql2005Dialect");
            configuration.SetProperty("connection.connection_string",
"Server=localhost;initial catalog=TesteNHibernate;Integrated
Security=True");
            configuration.SetProperty("proxyfactory.factory_class",
"NHibernate.ByteCode.Castle.ProxyFactoryFactory,
NHibernate.ByteCode.Castle");
            configuration.AddAssembly("Data");

            ISessionFactory sessionFactory =
configuration.BuildSessionFactory();

            ISession session = sessionFactory.OpenSession();

            try
            {
                Client client = new Client();
                client.Name = "Client 1";

                Contact contact1 = new Contact();
                contact1.Name = "Contact 1";
                contact1.Client = client;

                Contact contact2 = new Contact();
                contact2.Name = "Contact 1";
                contact2.Client = client;

                session.SaveOrUpdate(client);
                session.SaveOrUpdate(contact1);
                session.SaveOrUpdate(contact2);
                session.Flush();

                Client client1 = (from c in
session.QueryOver<Client>() where c.ClientID == client.ClientID select
c).List()[0];

                Console.WriteLine(client1.Contacts.Count);
            }
            finally
            {
                session.Close();
                sessionFactory.Close();
                Console.ReadLine();
            }

****************************** Class

    public class Client
    {
        public virtual Int64 ClientID { get; set; }
        public virtual String Name { get; set; }
        public virtual IList<Contact> Contacts { get; set; }

        public Client()
        {
            Contacts = new List<Contact>();
        }
    }

    public class Contact
    {
        public virtual Int64 ContactID { get; set; }
        public virtual String Name { get; set; }
        public virtual Client Client { get; set; }
    }

****************************** Map

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Dados"
namespace="Dados">
  <class name="Client" table="Clients">
    <id name="ClientID" type="Int64">
      <generator class="native"/>
    </id>
    <property name="Name" type="String" length="50" not-null="true"/>
    <bag name="Contacts" cascade="all" inverse="true">
      <key column="ClientID"/>
      <one-to-many class="Contact"/>
    </bag>
  </class>
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Dados"
namespace="Dados">
  <class name="Contact" table="Contacts">
    <id name="ContactID" type="Int64">
      <generator class="native"/>
    </id>
    <property name="Name" type="String" length="50" not-null="true"/>
    <many-to-one name="Client" class="Client" column="ClientID" not-
null="true"/>
  </class>
</hibernate-mapping>

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