How do you do a union of two tables in NHibernate? - nhibernate

I need to do a union of two tables using NHibernate and HQL. I have found very little help online, and I want to know if it is possible and if so how?

Found my answer:
http://www.hibernate.org/117.html#A21
It doesn't currently support union or intersect.

You could use a named sql-query and do the union in raw SQL. NHibernate will be able to populate entity instances from the sql-query and return those as the query result. See here and here.

I don't believe HQL supports unions, but you can write your own record transformer and author the sql by hand.

Related

How to do 2 select queries?

I am using Firebird and what I want to do is display 2 different select queries. Here is an example
select * from tblStates;
select * from tblTeachers;
This are two tables with 2 completely different columns. When I use the code above firebird will only display tblTeachers. What I want is to see both tblStates and tblTeachers as two different tables. I was told to use suspend but I don't know the syntax and when I just type suspend there is a unknown token error.
I am unfamiliar with the details of Firebird. However, in doing some research, I came across this post that might help.
What you're looking for is considered a batch separator statement. In SQL Server, it would be something like:
SELECT * from myTable1
GO
SELECT * from myTable2
GO
This would return two tables in a table or database studio viewer. I did not see something similar for Firebird other than what is linked above.
However, the next question is why are you wanting this functionality? Are you sure there is not a relationship between States and Teachers, as per your example? If there is not, then a common practice would be to run your unrelated SQL statements and save the returned tables in memory for use in your application.
Sometimes, if you cannot figure out a way to do what you want, its a good idea to look back at exactly what your goal is and wonder if there might be a better way :)
Hope this helps.

How do I query across many tables in redshift without UNION ALL?

I'm looking for a dumb way to write the same select query across all tables. For example in Google Bigquery I can query like this using wild cards
select COMPLICATED QUERY HERE from `myproject:mytable_2017_1_*`;
How can I do the equivalent in redshift?
The wildcard syntax is not available for Amazon Redshift. Each query must specifically reference the table(s) it wishes to use.
You could create a VIEW that does the UNION ALL for you, and then you could just query the view.

Questions about query several tables through JPA

Recently, I need to use JPA in my project.
However, I encountered some difficulties. JPA seemes to be harder than I thought.
So, I post my problem here, hoping that, some guys may help me.
There are two tables operation and analysis, the sql is like the following:
select op_id from operation where flag = 1
union
select an_id from analysis where on_type = "processed";
op_id and an_id are two column names actually means the same.
Can anyone help me writing a JPA version of this?
I'm very grateful of that.
Thank you very much.
JPQL does not support unions.
You have four options:
You can create a View in your DB that would do the unioning, then map to that view
Write a native SQL that does the union (since you only need the ID, this is a valid option)
If the IDs are distinct, you can run two queries, and concatenate the result list (if not distinct, you could use Sets to filter duplications)
If you are using the latest (at least >=2.4) EclipseLink as a JPA provider, you can use UNION
See this question here.

Does NHibernate support subqueries in the `from` clause?

Does NHibernate support subqueries in the from clause? For example I have sql queries looking like this:
SELECT subquery.Id, sum(subquery.Value) ...
FROM (SELECT DISTINCT Id, Value, ... FROM table1) as subquery
WHERE ...
GROUP BY subquery.Id
I've only found this info:
HQL does not have subquery in from clause support. I've been consider
how to add this, but I think it's going to be a substantial effort.
It's coming, but unless somebody can provide more development time,
it's going to take while to implement.
Patrick Earl
from Google Groups (17.08.2011). Is it still relevant? Maybe I can use Linq to NHibernate?
If this functionality is not provided you could create a database view that does this and map your nhibernate entity to the database view.

NHibernate problem choosing between CreateSql and CreateCriteria

I have a very silly doubt in NHibernate. There are two or three entities of which two are related and one is not related to other two entities. I have to fetch some selected columns from these three tables by joining them. Is it a good idea to use session.CreateSql() or we have to use session.CreateCriteria(). I am really confused here as I could not write the Criteria queries here and forced to use CreateSql. Please advise.
in general you should avoid writing SQL whenever possible;
one of the advantages of using an ORM is that it's implementation-agnostic.
that means that you don't know (and don't care) what the underlying database is, and you can actually switch DB providers or tweak with the DB structure very easily.
If you write your own SQL statements you run the risk of them not working on other providers, and also you have to maintain them yourself (for example- if you change the name of the underlying column for the Id property from 'Id' to 'Employee_Id', you'd have to change your SQL query, whereas with Criteria no change would be necessary).
Having said that- there's nothing stopping you from writing a Criteria / HQL that pulls data from more than one table. for example (with HQL):
select emp.Id, dep.Name, po.Id
from Employee emp, Department dep, Posts po
where emp.Name like 'snake' //etc...
There are multiple ways to make queries with NH.
HQL, the classic way, a powerful object oriented query language. Disadvantage: appears in strings in the code (actually: there is no editor support).
Criteria, a classic way to create dynamic queries without string manipulations. Disadvantages: not as powerful as HQL and not as typesafe as its successors.
QueryOver, a successor of Criteria, which has a nicer syntax and is more type safe.
LINQ, now based on HQL, is more integrated then HQL and typesafe and generally a matter of taste.
SQL as a fallback for cases where you need something you can't get the object oriented way.
I would recommend HQL or LINQ for regular queries, QueryOver (resp. Criteria) for dynamic queries and SQL only if there isn't any other way.
To answer your specific problem, which I don't know: If all information you need for the query is available in the object oriented model, you should be able to solve it by the use of HQL.