Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 make the Test NUnit for insert a value in this table. This is my Test 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 the 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); that show this error:

Generic ADO.Exception

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

like image 850
ale Avatar asked Jan 20 '23 11:01

ale


1 Answers

When NHibernate throws an exception like this, it includes the inner exception thrown by the ADO provider, which will, 99% of the time, say exactly what was wrong with the SQL statement that failed. The inner exception is MUCH more helpful; NH just wraps it in something more generic so you can catch the generic exception instead of all possible provider-specific exceptions thrown by the actual ADO.NET layer NH uses to transmit SQL.

From what you've shown, my number-one suspicion is that the columns in the DB are not named exactly like your properties, and you are not specifying different column names in the mapping. If you only specify the property, the property and column names must be a match according to the DB's collation (basically meaning the only possible difference could be case and only in a case-insensitive DB).

One other thing, not related to the error: Your AddSet() function doesn't allow for external transaction control, because each operation is not just its own transaction, but its own session. This is generally a bad idea, because if Set 1 and Set 2 must both be added, Set 1 is inserted, then Set 2 fails, Set 1 will still be in the DB. Usually, you have to be able to tell NHibernate that multiple operations should be all-or-nothing, by allowing the code to obtain some Transaction or UnitOfWork reference that it can pass into various repository methods to identify a transactional context.

like image 170
KeithS Avatar answered Jan 28 '23 07:01

KeithS