Nhibernate queryover union - nhibernate

I have two queries based in session.QueryOver. The queries are different, but returns a IList the same type object.
The number of result of the two query, is the total object that i need.
How can I combine both queryover?
Is possible to do a union in NHibernate?

You could consider using CreateSQLQuery instead. Form your SQL query syntax with the UNION keyword and use CreateSQLQuery to execute your query and get your result.
ISessionFactory.OpenSession().CreateSQLQuery("SELECT * FROM A UNION SELECT * FROM B")
.SetResultTransformer(Transformers.AliasToBean(typeof(YourClass)))
.List<YourClass>();

Related

Oracle case statement not returning values for no row results

I have a simple case statement as follows:
select
case WHEN upper(VALUE) is null then 'A_VALUE_ANYWAY' end test
FROM
V$SYSTEM_PARAMETER
WHERE
UPPER(VALUE)= 'NO_VALUE_IS_HERE'
This code is designed to return 'A_VALUE_ANYWAY' because there is no output from the SQL.
However, it does not return anything at all.
Essentially, what I would like is a value being forced to return from the case statement instead of just no rows.
Am I able to do that with the case statement? Is there some form of no data found handler I should be using instead?
I have examined this similar question but this is very complex and does not seem possible with this more simple statement
SQL CASE Statement for no data
Also, this, which uses a union to get a value from dual:
Select Case, when no data return
Which seems like a "Fudge" I feel like there must be some designed way to handle no data being found in a case statement.
From Oracle 12, you can use the FETCH syntax so that you do not have to query the table multiple times:
SELECT value
FROM (
SELECT value,
1 AS priority
FROM V$SYSTEM_PARAMETER
WHERE UPPER(VALUE)= 'NO_VALUE_IS_HERE'
UNION ALL
SELECT 'A_VALUE_ANYWAY',
2
FROM DUAL
ORDER BY priority
FETCH FIRST ROW WITH TIES
)
db<>fiddle here
What you are asking is: "if my query returns no rows, I want to see a value". That cannot be solved with a case expression. A case expression transforms the results of your query. If there are no results, nothing can be transformed. Instead you could could modify your query and union it with another select from dual that returns a string if the query itself returns no results. That way either part of the UNION ALL will return something.
SELECT
VALUE
FROM
V$SYSTEM_PARAMETER
WHERE
UPPER(VALUE)= 'NO_VALUE_IS_HERE'
UNION ALL
SELECT 'A_VALUE_ANYWAY'
FROM
DUAL
WHERE NOT EXISTS (SELECT 1
FROM
V$SYSTEM_PARAMETER
WHERE UPPER(VALUE)= 'NO_VALUE_IS_HERE'
This is the same technique as in the SQL Case statement for no data question.

HQL count from multiple tables

I would like to query my database using a HQL query to retrieve the total number of rows having a MY_DATE greater than SOME_DATE.
So far, I have come up with a native Oracle query to get that result, but I am stuck when writing in HQL:
SELECT
(
SELECT COUNT(MY_DATE)
FROM Table1
WHERE MY_DATE >= TO_DATE('2011-09-07','yyyy-MM-dd')
)
+
(
SELECT COUNT(MY_DATE)
FROM Table2
WHERE MY_DATE >= TO_DATE('2011-09-07','yyyy-MM-dd')
)
AS total
I actually have more than 2 tables but I keep having an IllegalArgumentException (unexpected end of subtree).
The working native Oracle basically ends with FROM dual.
What HQL query should I use to get the total number of rows I want?
First of, if you have a working SQL query, why not just use that instead of trying to translate it to HQL? Since you're returning a single scalar in the first place, it's not like you need anything HQL provides (e.g. dependent entities, etc...)
Secondly, do you have 'dual' mapped in Hibernate? :-) If not, how exactly are you planning on translating that?
That said, "unexpected end of subtree" error is usually caused by idiosyncrasies of Hibernate's AST parser. A commonly used workaround is to prefix the expression with '0 +':
select 0 + (
... nested select #1 ...
) + (
... nested select #2 ...
) as total
from <from what exactly?>

NHibernate use Criteria for Count(),First()

I have a question about Criteria method, one-to-many relation to the database, 'one' is "account", 'many' is "sites", when I use CreateCriteria() something is not right.
Like this: SessionFactory.OpenSession().CreateCriteria(typeof(Account)).List().Count();
Before it's run, I think the SQL should be SELECT COUNT(*) FROM table, but the SQL is SELECT id, siteurl...FROM table. So what's wrong with this? How can I solve it?
And First() method should be SELECT TOP1 ...FROM table, but it is SELECT ...FROM table
I'm an Nhiberate rookie, Please help me.
This happens because the Count method you are calling at the end executes after the query has run and outside of the database. You are only counting the elements in the list in memory. To achieve what you are looking for you could use a projection:
var count = session
.CreateCriteria<Account>()
.SetProjection(
Projections.Count(Projections.Id())
)
.UniqueResult<long>();

Distinct elements in django model

I'm a Django newbie and I wonder if there is a more efficient way (at a database level) of doing the following.
I have the model:
class Foo(models.Model):
item=models.IntegerField()
another_item=models.IntegerField()
And want to get an iterable of all the distinct values of "item".
This is what I have so far:
distinct=set([row.item for row in Foo.objects.all()])
This is easy to understand. But if I'm understanding how Django works then the SQL query is not very efficient because it is something like:
SELECT * FROM DB
when I only need:
SELECT DISTINCT item FROM DB
Any way of doing this more efficiently?
You want to use the distinct clause in combination with the values or values_list clauses.
Doc starts here. distinct, values and values_list are all in there.
So you could do:
Foo.objects.values_list('item', flat=True)
And that would return a list of item - matching your SELECT DISTINCT item FROM DB query.

Could you do a query with subqueries like this with nhibernate?

If you had a query that looks like this, could it be converted into a nhibernate query?
SELECT ....
FROM
(
SELECT ...
FROM ...
GROUP BY...
ORDER BY ...
UNION
SELECT ..
FROM ...
)
AS ASDF
GROUP BY ...
ORDER BY ...
With hibernate you can do native SQL queries. You just need to create a named Sql query mapping, and map it to a class.
However, you will not get the benefit of writing to the named query. [Which is to be expected]
if by "nhibernate query" you mean to use any non-sql method to express an sql query, like using HQL or the ICriteria api, no you wouldn't be able to use the union or to express a result set using select * FROM ( select ... ) as FOO
The named query that monksy refers to is writing an SQL-query that you bind to the mapping but you cannot manipulate that query in many ways