Recently contacted a project with Spring.net combined NHibernate, the first use, there are many configurations, data operation once the problem, it is difficult to find the reason, then how to see NHibernate sent to the database SQL statement?
Of course we can look at it through the log4net log, but it's a little cumbersome, so we find another way to see it.
The onpreparestatement(SqlString sql) defined in the Iinterceptor interface in NHibernate can observe the generated SQL, Emptyinterceptor to achieve.
[Serializable] Public class iinterceptor{
Other methods omit public virtual SqlString onpreparestatement (SqlString sql); }
All we have to do is inherit the emptyinterceptor and rewrite the onpreparestatement method.
Public class sqlwatcheremptyinterceptor{ publicoverride SqlCommand . SqlString Onpreparestatement (sqlcommand.sqlstring sql) { returnbase. Onpreparestatement (SQL);} }
In the overridden method, you can use any method to monitor the SQL statements you want to monitor.
Finally, you can add the interception class you created in the place where you created the ISession.
Public Static ISession opensession () { return sessionfactory.opensession (new sqlwatcher ());}
The results are as follows:
[NHibernate] Viewing SQL statements generated by NHibernate