1

I persist my objects using NHibernate in the database

the App object have a property defined:

public virtual DateTime ReleaseDate { get; set; }

in the mappingClass:

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

which in the sqlServer 2008 its dataType is dateTime and is not nullable.

for the first Time it saves to database with no error. but after updating app info I encounter SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.

but the app release date is a valid dateTime : 2/16/2014 2:21:58 AM and it's not null.

so Im confused why this exception raise?

ist<App> apps = session.QueryOver<Data.Model.App>()
            .List()
            .ToList();
.
.
.
.
for (int i = 0; i < apps.Count(); i++)
        {
            App appWithOldInfo = apps[i];

                using (ITransaction transaction = session.BeginTransaction())
                {
                    try
                    {
                        //updating app info
                        appWithOldInfo = UpdateAppInfo(appWithOldInfo, appWithNewInfo);

                        session.Update(appWithOldInfo);
                        transaction.Commit();
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(ex.Message);
                    }
                }

see the screenshots: enter image description here enter image description here enter image description here

9
  • 4
    Are you sure that's the only date and time in your database table? Perhaps there's another one that you've forgotten about, which is defaulting to 1/1/0001? Commented Mar 8, 2014 at 17:22
  • yeah I'm sure. see updated post Commented Mar 8, 2014 at 17:35
  • I have another field of type timeStamp for tracking version changing of entity. Commented Mar 8, 2014 at 17:42
  • And what's the value of that? Commented Mar 8, 2014 at 17:46
  • 1
    The answer here could be in a different instance having the default DateTime - beeing loaded by Session. The suspected to me could be appWithNewInfo. If this (other instance) is just loaded from DB, i.e. kept in a Session, during the session.Flush() - even this instance is persisted. Commented Mar 8, 2014 at 17:53

2 Answers 2

2

thanks guys for your helpful comments.

The problem wast That I was fetching device object from DB which has a property LastActivityDate

List<Device> devices = session.QueryOver<Data.Model.Device>().List().ToList();

I had added this property to model after addding a device object with some info to the DB. this property was null, but I haden't defined LastActivityDate as a nullable property.

so this object was in the same session of app object. when I flushed the session, because LastActivityDate was null, SqlDateTime exception rised!

it's simple. but I spend hours to find it!!

Sign up to request clarification or add additional context in comments.

1 Comment

So the issue is that another object is being added to the NHibernate session. On the flush (or in my case the commit) NHibernate throws up on itself if all the objects in session dont have all of the right data. To prevent this add a .readonly() extension to the Query<> or QueryOver<> that way NHibernate doesnt track it. In Hashems example he could have done a List<Device> devices = session.QueryOver<Data.Model.Device>().Readonly().List().ToList()
0

If you are facing this problem, here are the steps to resolve it:

  • If you are saving an instance of class A, it doesn't necessarily mean the issue lies within that class alone. Hibernate manages the session, which includes all entities used within that session. It's possible that one of the entities is causing the issue. Essentially, this error indicates that you are trying to pass a null value into a not-null date field.

1 Comment

You are not wrong, you are just 10 years late... The author himself gave the same solution 10 years ago!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.