linqoracle-databasenhibernateodp.netnhibernate-3

NHib 3 Configuration & Mapping returning empty results?


Note: I'm specifically not using Fluent NHibernate but am using 3.x's built-in mapping style. However, I am getting a blank recordset when I think I should be getting records returned.

I'm sure I'm doing something wrong and it's driving me up a wall. :)

Background / Setup

Things I've Tried

The Code

POCO: WorkorderBriefBrief.cs

namespace PEApps.Model.WorkorderQuery
{
    public class WorkorderBriefBrief
    {
        public virtual string WorkorderNumber { get; set; }

    }
}

Mapping: WorkorderBriefBriefMap.cs

using NHibernate.Mapping.ByCode;
using NHibernate.Mapping.ByCode.Conformist;
using PEApps.Model.WorkorderQuery;

namespace ConsoleTests
{
    public class WorkorderBriefBriefMap : ClassMapping<WorkorderBriefBrief>
    {
        public WorkorderBriefBriefMap()
        {
            Schema("MAXIMO");
            Table("WORKORDER");


            Property(x=>x.WorkorderNumber, m =>
                {
                    m.Access(Accessor.ReadOnly);
                    m.Column("WONUM");
                });
          }
    }
}

Putting it Together: Program.cs

namespace ConsoleTests
{
    class Program
    {
        static void Main(string[] args)
        {
            NHibernateProfiler.Initialize();
            try
            {
                var cfg = new Configuration();
                cfg
                   .DataBaseIntegration(db =>
                   {
                       db.ConnectionString = "[Redacted]";
                       db.Dialect<Oracle10gDialect>();
                       db.Driver<OracleManagedDataClientDriver>();
                       db.KeywordsAutoImport = Hbm2DDLKeyWords.AutoQuote;
                       db.BatchSize = 500;
                       db.LogSqlInConsole = true;
                   })
                   .AddAssembly(typeof(WorkorderBriefBriefMap).Assembly)
                   .SessionFactory().GenerateStatistics();

                var factory = cfg.BuildSessionFactory();

                List<WorkorderBriefBrief> query;

                using (var session = factory.OpenSession())
                {
                    Console.WriteLine("session opened");
                    Console.ReadLine();
                    using (var transaction = session.BeginTransaction())
                    {
                        Console.WriteLine("transaction opened");
                        Console.ReadLine();
                        query =
                            (from workorderbriefbrief in session.Query<WorkorderBriefBrief>() select workorderbriefbrief)
                                .ToList();
                        transaction.Commit();
                        Console.WriteLine("Transaction Committed");
                    }
                }

                Console.WriteLine("result length is {0}", query.Count);
                Console.WriteLine("about to write WOs");

                foreach (WorkorderBriefBrief wo in query)                
                {
                    Console.WriteLine("{0}", wo.WorkorderNumber);
                }

                Console.WriteLine("DONE!");
                Console.ReadLine();

                // Test a standard connection below
                string constr = "[Redacted]";

                OracleConnection con = new OracleConnection(constr);
                con.Open();
                Console.WriteLine("Connected to Oracle Database {0}, {1}", con.ServerVersion, con.DatabaseName.ToString());
                con.Dispose();

                Console.WriteLine("Press RETURN to exit.");
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error : {0}", ex);
                Console.ReadLine();
            }
        }
    }
}

Thanks in advance for any help you can give!

Update

The following code (standard ADO.NET with OracleDataReader) works fine, returning the 16 workorder numbers that it should. To me, this points to my use of NHibernate more than the Oracle Managed ODP.NET. So I'm hoping it's just something stupid that I did above in the mapping or configuration.

   // Test a standard connection below
            string constr = "[Redacted]";

            OracleConnection con = new Oracle.ManagedDataAccess.Client.OracleConnection(constr);
            con.Open();
            Console.WriteLine("Connected to Oracle Database {0}, {1}", con.ServerVersion, con.DatabaseName);
            var cmd = new OracleCommand();
            cmd.Connection = con;
            cmd.CommandText = "select wonum from maximo.workorder where upper(reportedby) = 'MAXADMIN'";
            cmd.CommandType = CommandType.Text;
            Oracle.ManagedDataAccess.Client.OracleDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                Console.WriteLine(reader.GetString(0));
            }

            con.Dispose();

Solution

  • I found the answer -- thanks to Oskar's initial suggestion, I realized it wasn't just that I hadn't added the assembly, I also needed to create a new mapper.

    to do this, I added the following code to the configuration before building my session factory:

    var mapper = new ModelMapper();
    
    //define mappingType(s) -- could be an array; in my case it was just 1
    var mappingType = typeof (WorkorderBriefBriefMap);
    
    //use AddMappings instead if you're mapping an array
    mapper.AddMapping(mappingType);
    
    //add the compiled results of the mapper to the configuration
    cfg.AddMapping(mapper.CompileMappingForAllExplicitlyAddedEntities());
    var factory = cfg.BuildSessionFactory();