Domanda

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

  • I have an Oracle 11g database for a product by IBM called Maximo
  • This product has a table called workorder which lists workorders; that table has a field called "wonum" which represents a unique work order number.
  • I have a "reporting" user which can access the table via the maximo schema
    • e.g. "select * from maximo.workorder"
  • I am using Oracle's Managed ODP.NET DLL to accomplish data tasks, and using it for the first time.

Things I've Tried

  • I created a basic console application to test this
  • I added the OracleManagedClientDriver.cs from the NHibernate.Driver on the master branch (it is not officially in the release I'm using).
  • I created a POCO called WorkorderBriefBrief, which only has a WorkorderNumber field.
  • I created a class map, WorkorderBriefBriefMap, which maps only that value as a read-only value.
  • I created a console application with console output to attempt to write the lines of work orders.
  • The session and transaction appear to open correct,
  • I tested a standard ODP.NET OracleConnection to my connection string

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();
È stato utile?

Soluzione 2

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();

Altri suggerimenti

When configuring NHibernate, you need to tell it about your mappings.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top