views:

322

answers:

2

Before stating the problem, please look at the code

Database(Oracle) SQL:

create table test_tab(
 id number,
 Name varchar2(50)
);

Corresponding Class in C#:

public class TestTable
    {
        private long id;
        public virtual long Id {
            get {
                return id;
            }
            set {
                id = value;
            }
        }

        private string name;
        public virtual string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }
    }

The mapping file for this:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="DataTransfer" namespace="DataTransfer">
  <class name="DataTransfer.Models.TestTable, DataTransfer" table="TEST_TAB">
    <id name="Id" column="ID" type="long" unsaved-value="0">
      <generator class="sequence">
        <param name="sequence">
          seq_test
        </param>
      </generator>
    </id>
    <property name="Name" column="NAME" type="string" not-null="false"/> 
  </class>
</hibernate-mapping>

The "TestTable" class is inside the Models folder under DataTransfer project

hibernate configuration figuration file:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory>
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="connection.driver_class">NHibernate.Driver.OracleClientDriver</property>
    <property name="connection.connection_string">Data Source=xe;Persist Security Info=True;User ID=hr;Password=hr;Unicode=True</property>
    <property name="show_sql">false</property>
    <property name="dialect">NHibernate.Dialect.Oracle9Dialect</property>
    <!-- mapping files -->
    <mapping assembly="DataTransfer" />
  </session-factory>
</hibernate-configuration>

And here is my DataAccessLayer Code;

public void AddToTestTable(Test_Tab user)
        {
            using (ISession session = GetSession())
            {
                using (ITransaction tx = session.BeginTransaction())
                {
                    try
                    {
                        session.Save(user);
                        session.Flush();
                    }
                    catch (NHibernate.HibernateException)
                    {
                        tx.Rollback();
                        throw;
                    }
                }
            }
        }

Now the problem is when I insert any value to the database(using simple ASP.NET form) nothing happens(even no exceptions!). But it worked perfectly when i did not use "Models" folder for TestTable Class and "Mappings" folder for mapping files. Please help me out.

+1  A: 

Make sure that the .hbm.xml mapping file is embedded as a resource in the assembly DataTransfer.

Darin Dimitrov
When the mapping is not embedded as a resource, NH will throw an exception since it will treat the class as a 'non persitent' class. In other words: it will throw an exception since it does not know how to persist the entity.
Frederik Gheysels
No that was not my problem. But thanks for your response
Mr. Flint
+1  A: 

How do you insert / add an entity to the DB ? Can you show some code please ? Do you flush the session for instance ? Or, how did you confiugre the flushmode ?

Frederik Gheysels
I edited my question. Please take a look.
Mr. Flint
Did this actually answer your question? I don't think so :)
Jeffrey Kemp