Generic ADO.Exception NHibernate

I have this table:

TABLE "Set": [PK: IdSet (int), IdProject (int), IdSetState (int), IdPriority (int), NumSet (int), Unit (nchar), NumDisc (int)]

So, I am doing Test NUnit to insert the value into this table. This is my testing method:

[Test] public void Can_add_Set() { var set = new Set { IdProject = 2, IdSetState = 2, NumDisc = 1, IdPriority = 3, NumSet = 100}; setRepository.AddSet(set); } 

And this is my insert method:

 public void AddSet(Set set) { using (ISession session = NHibernateSessionBuilder.OpenSession()) using (ITransaction transaction = session.BeginTransaction()) { session.Save(set); transaction.Commit(); } } 

This is Set.hbm.xml:

 <?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="AdminProject" namespace="AdminProject.Business.Entity"> <class name="Set"> <id name="IdSet"> <generator class="identity"/> </id> <property name="IdProject" /> <property name="IdSetState" /> <property name="IdPriority" /> <property name="Unit" /> <property name="NumDisc" /> <property name="NumSet" /> </class> </hibernate-mapping> 

When session.save (set); which show this error:

General ADO.Exception

"Could not load an object: [AdminProject.Business.Entity.Set # 5] [SQL: SELECT set0_.IdSet how IdSet2_0_, set0_.IdProject how IdProject2_0_, set0_.IdSetState how IdSetState2_0_, set0_.IdPriority how IdPriority2_0_, set0_.Unit how Unit2_0_ , set0_.NumDisc as NumDisc2_0_, set0_.NumSet as NumSet2_0_ FROM Set set0_ WHERE set0_.IdSet =?] "

+4
source share
1 answer

When NHibernate throws an exception like this, it includes an internal exception thrown by the ADO provider, which in 99% of cases will tell you exactly what was wrong with the SQL statement that failed. Internal exception is much more useful; NH just wraps it in something more general, so you can catch a generic exception instead of all the possible provider-specific exceptions that are thrown by the actual ADO.NET level that NH uses to pass SQL.

From what you showed, my number one suspicion is that the columns in the database are not named exactly like your properties, and you do not specify different column names in the mapping. If you specify only a property, the names of the properties and columns must match according to the sorting of the database (basically this means that the only possible difference can be random and only in a case-insensitive database).

Another thing not related to the error: your AddSet () function does not allow you to control external transactions, because each operation is not only its own transaction, but also its own session. This is usually a bad idea, because if you need to add both 1 and 2, set 1, then set 2, set 1 anyway in the database. Generally, you should be able to tell NHibernate that several operations should be β€œall or nothing”, allowing the code to get some reference to a transaction or UnitOfWork so that it can go to different repository methods to determine the transactional context.

+8
source

All Articles