How can I have NHibernate only generate the SQL without executing it?

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 … Read more

Persist Data by Programming Against Interface

Your repository should accept BankAccount – not IBankAccount because Linq-to-sql doesn’t know what is IBankAccount and compiler doesn’t allow you to store it without casting it first to BankAccount (that can obviously fail at runtime if IBankAccount instance is not a BankAccount). Once you have BankAccount you simply call: Context.BankAccounts.Add(account); Context.SubmitChanges();

What’s the difference between session.Merge and session.SaveOrUpdate?

This is from section 10.7. Automatic state detection of the Hibernate Reference Documentation: saveOrUpdate() does the following: if the object is already persistent in this session, do nothing if another object associated with the session has the same identifier, throw an exception if the object has no identifier property, save() it if the object’s identifier … Read more

many-to-many with extra columns nhibernate

The many-to-many, without the explicit mapping of the pairing table as an entity – is in NHibernate of course suported. So, in case, that the Date column is autogenerated, or nullable (does not have to be inserted by app/NHiberante), we can do it like here: 6.8. Bidirectional Associations <class name=”User”> <id name=”Id” column=”Uid”/> … <bag … Read more

Criteria.DISTINCT_ROOT_ENTITY vs Projections.distinct

While similar names, the usage is different. I. Projections.distinct(Projections.property(“id”)); this statement would be translated into SQL Statement. It will be passed to DB Engine and executed as a SQL DISTINCT. See: 17.9. Projections, aggregation and grouping so e.g. this example: List results = session.createCriteria(Cat.class) .setProjection( Projections.projectionList() .add( Projections.distinct(Projections.property(“id”)) ) ) .list(); would seems like: SELECT … Read more