Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7969155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:11:32+00:00 2026-06-04T07:11:32+00:00

I know how to log the SQL to log4net/NLog/trace window at runtime with the

  • 0

I know how to log the SQL to log4net/NLog/trace window at runtime with the show_sql configuration option.

What I’m looking for is a way to give a Query<T>() to NHibernate retrieve the generated SQL.

I’ve looked through the Persister class, the Drivers, different Interceptors and Events. There are so many places to look, even narrowing down my search would be of great help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T07:11:33+00:00Added an answer on June 4, 2026 at 7:11 am

    You can get the generated sql queries without execution with the following methods:

    For the NHibernate.Linq queries:

    public String GetGeneratedSql(System.Linq.IQueryable queryable, ISession session)
    {
        var sessionImp = (ISessionImplementor) session;
        var nhLinqExpression = new NhLinqExpression(queryable.Expression, sessionImp.Factory);
        var translatorFactory = new ASTQueryTranslatorFactory();
        var translators = translatorFactory.CreateQueryTranslators(nhLinqExpression, null, false, sessionImp.EnabledFilters, sessionImp.Factory);
    
        return translators[0].SQLString;
    }
    

    For Criteria queries:

    public String GetGeneratedSql(ICriteria criteria)
    {
        var criteriaImpl = (CriteriaImpl) criteria;
        var sessionImpl = (SessionImpl) criteriaImpl.Session;
        var factory = (SessionFactoryImpl) sessionImpl.SessionFactory;
        var implementors = factory.GetImplementors(criteriaImpl.EntityOrClassName);
        var loader = new CriteriaLoader((IOuterJoinLoadable) factory.GetEntityPersister(implementors[0]), factory, criteriaImpl, implementors[0], sessionImpl.EnabledFilters);
    
        return loader.SqlString.ToString();
    }
    

    For QueryOver queries:

    public String GetGeneratedSql(IQueryOver queryOver)
    {
        return GetGeneratedSql(queryOver.UnderlyingCriteria);
    }
    

    For Hql queries:

    public String GetGeneratedSql(IQuery query, ISession session)
    {
        var sessionImp = (ISessionImplementor)session;
        var translatorFactory = new ASTQueryTranslatorFactory();
        var translators = translatorFactory.CreateQueryTranslators(query.QueryString, null, false, sessionImp.EnabledFilters, sessionImp.Factory);
    
        return translators[0].SQLString;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to tell SQL Server 2008 Express to log every query
I wanted to know if there is way to log the mysql queries in
Does anyone know of a way to save the console.log output in Chrome to
I'm using Doctrine and want to log all generated SQL queries. I know that
When you log LINQ-to-SQL's query output via the Log property on the DataContext object,
anyone know how to optimize SQL query for inserting data from select statement and
I'm creating a log shipping configuration between two sql server 2005 instances. I'm shipping
I know this is possible in Linq-to-Sql, is it possible to automatically log all
I just want to know: How can I delete 'log.txt' if the last modification
I know I can do this... RegExp.prototype.xtend = function(){ console.log(123) } y = new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.