Eatdoku
Eatdoku

Reputation: 6931

NHibernate CreateSQLQuery treats entities updated

i am trying to retrieve data from database via nhibernate's CreateSQLQuery on a stored proceduure. Something like the following code.

then I am basically doing a session transaction commit, however the commit throws an "cannot update" exception. It is trying to execute a update statement on CustomEntityDao.

        const string selectSQL = "EXEC GetDataSP @Id = :Id";
        var query = Session.CreateSQLQuery(selectSQL);
        query.SetString("Id", "10");
        query.AddEntity(typeof (CustomEntityDao));

        var entityList = query.List<CustomEntityDao>();

        try
        {
            Session.Transaction.Commit();
        }
        catch (Exception ex)
        {
            throw ex;
        }

My question is why are the entities been treated as modified, as you can see in the code I am only doing a query.

Upvotes: 0

Views: 881

Answers (1)

Baz1nga
Baz1nga

Reputation: 15579

May be there is something else going on your code, what I can suggest is use NHibernate Profiler a trial version of which can be downloaded from www.nhprof.com and monitor the SQL commands that are being fired and notice what objects are being retrieved.

Also I dont understand why are you committing the transaction in the first place.

To solve this particular problem you could always use NHibernates StatelessSession which doesnt track entities or you can also use Session.Evict and ask Nhibernate to stop tracking particular objects.

Upvotes: 2

Related Questions