senzacionale
senzacionale

Reputation: 20916

nhibernate session problem when reading from DB

I commit some data in DB with nhibernate.

TimeSlices newTimeSlices = new TimeSlices(timeSliceStartTime, timeSliceEndTime, schedule, newScheduleDay);
                        tsDao.Save(newTimeSlices);
                        tsDao.CommitChanges();

but then i try to read this data

public ScheduleDays GetByDate(DateTime date, Schedules schedule)
        {
            NHibernateSession.Refresh(schedule);

            DateTime tmpDate = new DateTime(date.Year, date.Month, date.Day, 0, 0, 0);

            return NHibernateSession.CreateCriteria(typeof (ScheduleDays))
                .Add(Restrictions.Eq(ScheduleDaysProperties.Date.ToString(), tmpDate))
                .Add(Restrictions.Eq(ScheduleDaysProperties.Schedule.ToString(), schedule))
                .UniqueResult<ScheduleDays>();
        }

but without succeed. I look in DB and it is there but i can not read it from DB. How can i refresh session or what i must do that i can read this new data after i save it. Problem is only when i am working with ajax components.

public ScheduleDaysMap()
        {
            Id(x => x.ScheduleDayId);

            Map(x => x.Date)
                .Not.Nullable();

            Map(x => x.MinutesOrderInterval)
                .Not.Nullable();

            References(x => x.Schedule)
                .Column(TableNames.ScheduleId)
                .Not.Nullable()
                .LazyLoad();

            HasMany(x => x.Orders)
                .KeyColumn(TableNames.ScheduleDayId)
                .Inverse()
                .LazyLoad()
                .AsBag();

            HasMany(x => x.TimeSlices)
                .KeyColumn(TableNames.ScheduleDayId)
                .Inverse()
                .LazyLoad()
                .AsBag();

            Version(x => x.Timestamp);
        }

timeSliceStartTime, timeSliceEndTime and newTimeSlices are type od DateTime

schedule is DB object type (Table Schedules)

newScheduleDay is DB object type (Table ScheduleDays)


I am using C# + fluent nhibernate 1.1

Can be problem with mapping and lazy load or asbag? Or what can be wrong?

Upvotes: 1

Views: 837

Answers (2)

Baz1nga
Baz1nga

Reputation: 15579

Hey Can you run nhibernate profiler and see what query is getting fired. The link to the same is nhprof.com

Google to see how to use nhprof its very simple. Hope that helps

Upvotes: 0

Iain
Iain

Reputation: 11244

I don't know the problem, but here is how I would debug it:

  1. Check that it is saved correctly in the database. Check that the relationships are there.
  2. Turn on SQL output as per here
  3. Check what SQL your query generates. Try running the SQL yourself.
  4. Change the mapping or query using intuition.
  5. Try writing the query in other ways, say using nHibernate.LINQ.

nHibernate does not cache queries unless you tell it to.

Upvotes: 1

Related Questions