-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Before Save
PostPosted: Sat Oct 07, 2006 8:15 pm 
Newbie

Joined: Thu Feb 23, 2006 1:03 am
Posts: 7
Location: Los Angeles, CA
I have a question, i have all tables setup with CreateDate and UpdateDate,
CreateDate currently get populated using MS SQL GetDate() as default value.
When object got updated and persist,i want to update UpdateDate. Is it possible with nHibernate nice way??? Currently i always have to do object.UpdateDate = DateTime.Now. Is better solution exist???


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 07, 2006 11:57 pm 
Regular
Regular

Joined: Tue Feb 21, 2006 9:50 am
Posts: 107
in the NHibernate documentation you will find a chapter about interceptors. See

http://www.hibernate.org/hib_docs/nhibernate/html/manipulatingdata.html#manipulatingdata-interceptors

Regards
Klaus


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 08, 2006 2:09 am 
Senior
Senior

Joined: Mon Aug 21, 2006 9:18 am
Posts: 179
Yah, or you if your entities all inherit from a superclass, you can implement the ILifecycle interface and be able to do exactly what you are looking for . Here's a snippet from my DomainObject superclass from which all persisted entities derive:
Code:
        private static string GetCurrentUserName()
        {
            if (Thread.CurrentPrincipal == null ||
                Thread.CurrentPrincipal.Identity == null ||
                Thread.CurrentPrincipal.Identity.Name == null)
            {
                return WindowsIdentity.GetCurrent().Name;
            }
            else
            {
                return Thread.CurrentPrincipal.Identity.Name;
            }
        }
       
        #region NHibernate Interceptor Methods
        public LifecycleVeto OnSave(ISession s)
        {
            if (_createdBy == null)
            {
                _createdBy = GetCurrentUserName();
            }

            if (_lastModifiedBy == null)
            {
                _lastModifiedBy = GetCurrentUserName();
            }

            if (!_dateCreated.HasValue)
            {
                _dateCreated = DateTime.Now;
            }

            return LifecycleVeto.NoVeto;
        }

        public LifecycleVeto OnUpdate(ISession s)
        {
            _lastModifiedBy = GetCurrentUserName();

            _lastModified = DateTime.Now;
            return LifecycleVeto.NoVeto;
        }

        public LifecycleVeto OnDelete(ISession s)
        {
            return LifecycleVeto.NoVeto;
        }

        public void OnLoad(ISession s, object id)
        {
            //Do Nothing
        }
        #endregion

_________________
If this helped...please remember to rate it!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 24, 2006 3:59 pm 
Beginner
Beginner

Joined: Sat Sep 09, 2006 5:55 am
Posts: 23
Does the ILifeCycle approach work for you?

The documentation clearly states:
Quote:
LifecycleVeto OnUpdate( ISession s ):
Called when an entity is passed to ISession.Update().
This method is not called every time the object's state is persisted during a flush.


I've done some tests and indeed the OnUpdate method is not always called, rendering the values in the date_modified database field useless as they do not necessarily represent the correct situation.

_________________
Cheers,

Guy Mahieu


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 24, 2006 11:15 pm 
Senior
Senior

Joined: Sat Mar 25, 2006 9:16 am
Posts: 150
The easiest way is simply to use a <timestamp...> mapping in your class file (or <version type="timestamp"...>). This then gives you concurrency checking for free, as well.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.