NHibernate and ASP.NET Problems
I mentioned before that I was considering different ORM frameworks and finally decided on NHibernate. I made that decision mostly on the fact that I know of a fair number of developers who I respect that have used it and they still say nice things about it.
I knew going in there would be a substantial learning curve and some issues that were going to cost me a lot of time I don't necessarily have. My mental cost/benefit analysis included not only the cost to what I'm working on now, but how I will get payout (in theory) going forward.
Unfortunately I've run into a serious problem long before I think I should have.
I have 4 classes defined: Pricing, Coupons, ErrorCode and Service. All four of these classes represent what I would call reference data. They will not be written to by the application. The application needs to be able to get a specific item out the database and ideally get the entire collection.
I defined my mapping file:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
schema="Test.dbo"
default-access="field.camelcase-underscore"
namespace="Ecom.ReferenceData" >
<class name="Ecom.ReferenceData.Service, Ecom" table="Services">
<id name="ServiceID" column="ServiceID"
type="Int32" unsaved-value="null">
<generator class="native" />
</id>
<property name="ServiceID" column="ServiceID" type="Int32" />
<property name="Description" column="Description" type="String" length="50" />
<property name="Detail" column="Detail" type="String" length="80" />
</class>
</hibernate-mapping>
And then created a unit test (you do unit test right?)
Pricing pricing = _session.Load<Pricing>(1);
Assert.AreEqual(1, pricing.priceID);
Assert.AreEqual(3, pricing.CouponID);
Assert.AreEqual(1, pricing.ServiceID);
Assert.AreEqual(29.95, pricing.Price);
Not exactly a robust test but it did prove that the load worked.
Then I added a test to get the collection:
IList<Pricing> pricingList = _session.CreateQuery("from Pricing").List<Pricing>();
Assert.IsTrue(pricingList.Count > 0);
It also worked exactly as expected. Time to do the rest...
I added the other 3 classes exactly the same way. All 4 correctly do the initial unit test with _session.Load<T>(1);
However, 2 of the 4 fail on the second CreateQuery() test. The error results:
NHibernate.QueryException: in expected: <end-of-text> (possibly an invalid or unmapped class name was used in the query) [from coupons]
Someone please save me from my own stupidity here, but what the heck does that mean?
Remember, this test works fine loading each individual item. There are no nulls in the database. The only thing that is "different" is the use of the schema attribute in my mapping file. I did that because I need to talk to more than one database. All tables are in this example are in the same DB (not that it matters because this query never gets to that point). The Log4Net files are useless since it dies on Compiling Query.... well duh.
After literally a couple of hours of messing with this, I changed my code to this:
ISQLQuery query = _session.CreateSQLQuery("select * from test.dbo.coupons").AddEntity(typeof(Coupon));
IList<Coupon> coupons = query.List<Coupon>();
This code works fine, which once again leads me to believe my mapping file is correct. In fact, all four mapping files are almost identical in that each was created starting from the other. Each table in the DB has a single column primary key (int) defined.
I am hoping that as I move forward things get easier, because if I sink this much time into the simple stuff, this work will never pay for itself.