Nhibernate : QueryOver equivalent for ICriteria's SetComment? - nhibernate

Is is possible to add a comment to generated by queryover query in Visual Studio's output?
Previously when we were using ICriteria, there was simple SetComment method and we could set the query name so it was much easier to find specific query in output full of long (almost the same) queries. If it is possible we would prefere to add such comments without converting query to ICriteria.

I don't think there is a direct way of doinf it, but you can try with:
QueryOver<Entity>()
.Where(...
.UnderlyingCriteria.SetComment("....")

Related

Keyword based JPA query with statuses as Enums and with NOT clause [Kotlin]

I have a Keyword based JPA query I need to modify in order to exclude records with a particular status. Currently, I have the following:
findAllByLatestVersion_Entity_DataFieldGreaterThanEqualAndLatestVersion_AnotherFieldNull(datefield: Istant, pageable: Pageable)
I do not want to parameterise, therefore I would like to have the query to work as there was a WHERE clause stating that the status IS NOT C, for example. I am struggling to find clear documentation on how to go about. Is it possible to write something along these lines:
findAllByLatestVersion_Entity_DataFieldGreaterThanEqualAndLatestVersion_AnotherFieldNullAndLatestVersion_StatusCNot(datefield: Istant, pageable: Pageable)
Thank you
No this is not possible with query derivation, i.e. the feature you are using here. And even if it were possible you shouldn't do it.
Query derivation is intended for simple queries where the name of the repository method that you would choose anyway perfectly expresses everything one needs to know about the query to generate it.
It is not intended as a replacement for JPQL or SQL.
It should never be used when the resulting method name isn't a good method name.
So just formulate the query as a JPQL query and use a #Query annotation to specify it.

IQueryable Extension to Generate Temporal Table "AS OF" Queries

Having failed to find a satisfying solution, let me post this here:
We're using NHibernate as our ORM and are just beginning to use Sql Server temporal tables. We therefore need some kind of extension to IQueryable (or the HQL Builder or an InterceptingProvider or something) that will allow us to add the "AS OF" clause to our queries, something like
var results = session.Query<Company>
.Where(c => c.Name == "FogCreek")
.AsOf(DateTime.Today.AddYears(-1));
I've been struggling with it too and gave up as I lost too much time trying to find better approach...
So far I've injected FOR SYSTEM_TIME AS OF into SQL using custom HqlGeneratorForMethod but this gave me invalid SQL expression. So I had to fix it in OnPrepareStatement. This is hacky and not elegant solution but works for most simple cases.
Please look at my solution here and feel free to reply if you find better solution

Hibernate and dynamic SQL

do you know anything about Hibernate and its ability to generate dynamic sql queries with HQL?
I you have any links I would appreciate posting, I can't find nothing about it in Hibernate`s documentation.
Best regards
Gabe
//edit
so maybe I will precise what I mean. I am wondering if some HQL code generates SQL queries which uses something like EXECUTE (for postgres)
Not sure what you're aiming for here? HQL in Hibernate will always generate SQL, and you can put your HQL together differently based on input. It will always generate new SQL for each permutation and run. You can make Hibernate precompile/cache queries but that's just a performance optimization and shouldn't be your first concern.
I would also consider looking into the Criteria API which lets you stay a lot more object oriented instead of working with tons of strings.
If you're talking about static queries using dynamic arguments, the syntax is
select f from Foo f where f.bar = ? and f.zim = ?
or, with named parameters
select f from Foo f where f.bar = :bar and f.zim = :zim
If you're talking about completely dynamically created queries based on a set of criteria, then the API to use is... the Criteria API.
Both are largely covered in the Hibernate reference documentation.

Transforming SQL into Linq To Objects expressions at runtime

Does anyone know of any libraries that one can use to parse a SQL query and build up a linq expression which can then be compiled and used against linq to objects?
This approach only needs to work for simple queries, for complex ones I can write a hardcoded custom query
UPDATE: I've found something called NQuery which should do the trick for me http://nquery.codeplex.com/ so don't waste your time answering!
There is a project call [SqlLinq] (https://github.com/dkackman/SqlLinq) which converts SQL into c# Expression Trees, and evaluates them.

Is there a way to combine results from two IQueryable<T> using NHibernate & Linq?

I have two separate queries that both return the same IQueryable, and I'd like to combine them prior to projection. It looks like neither Union or Concat are implemented in Linq to NHibernate? Does anyone know how I might go about achieving this?
It's not possible. You'll have to do it on the client.
Example:
var allItems = queryable1.AsEnumerable().Concat(queryable2)
#Diago Mijelshon gives a good answer, but I would like to add that depending on what you're doing with the data, you may need to first cast it to an array or list so that NHibernate doesn't try to do any funny stuff with your operations.
I've used Entity Framework for years and I'm well familiar with this, and I've only used NHibernate a little bit, but the two tools appear to be similar in this regard.