-->
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.  [ 3 posts ] 
Author Message
 Post subject: Intercepting the database calls from NHibernate
PostPosted: Thu Feb 15, 2007 12:32 pm 
Beginner
Beginner

Joined: Thu Dec 21, 2006 11:38 am
Posts: 30
Original post: http://forum.hibernate.org/viewtopic.php?t=969429

Quote:
Is there any way to do this?

I need to be able to inject Thread.CurrentThread.CurrentPrincipal.Identity.Name to a param on a stored procedure called when NHibernate builds the IDbCommand to perform an update.

Any current way to do this, and if not, where's a good place to start... SingleTableEntityPersister?


This has come back up.

How do you map a UserId so that hydrating doesn't fail? I dont have UserId as a field in my db tables, and its a piece of info only truly useful to a stored procedure which is responsible for auditing. Some of the tables have a "CREATED_BY/CREATED_DATE/MODIFIED_BY/MODIFIED_DATE" strategy, whereas some have a shadowed history table backing them. The procedures to update/insert into these tables have a :USER_ID parameter right at the end of the parameter list.

This type of deal is best served via Database command interception, and IMO there should always be a way to manipulate the final database calls from any ORM.

Just like with any SOAP messaging infrastructure, sometimes you have to directly manipulate the message... I'm not saying it's "best practice" and probably should be avoided, however this is a more common situation.

The total amount of code was minimal as well, there's simply a check to see if the entity itself implements an interface called "NHibernate.IDbInterceptor". If it does, the entity object has the ability to tailor the final update/insert/select output directly, if need be.

I truly want this to be useful to more than just myself, and I would really like to know the plans of the NHibernate "steering committee". To them, I would recommend having some kind of interception ability, lower than the domain object mapping level, originally proposed by Ayende(?).


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 15, 2007 12:38 pm 
Beginner
Beginner

Joined: Thu Dec 21, 2006 11:38 am
Posts: 30
one another front, I tried using META under PROPERTY mapping to get NHibernate just to acknowledge the existence:
Code:
      <property name="_userId" type="string" access="field" formula="null">
         <meta attribute="">UserId</meta>
      </property>


Unfortunately, this mapping doesnt work: its not included during the update (not sure yet about the insert)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 15, 2007 3:57 pm 
Senior
Senior

Joined: Sat Sep 10, 2005 3:46 pm
Posts: 178
Ive done auditing with an Interceptor before. I apply the same strategy for OnFlushDirty and OnSave. Here is a sample.

Code:
public bool OnFlushDirty(object entity, object id, object[] currentState, object[] previousState, string[] propertyNames, NHibernate.Type.IType[] types) {
         //indicates whether we made changes to the entity or not
         bool changesMade = false;

                  //is the entity auditable
         if(entity is IAuditable){         
            //record entity modifiaction
            IAuditable auditableEntity = entity as IAuditable;
            auditableEntity.Audit.RecordModification(Thread.CurrentThread.CurrentPrincipal.Identity.Name);

            //turn the flag on that lets us know we have made changes
            changesMade = true;
         }

         return changesMade;
      }


Is that what you are looking for?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.