Hi there !
I am in the process of converting a simple java tutorial from plain JDBC to Hibernate. This is some introductory material for some students. Everything worked fine until now, where I have the problem with a simple SQL statement that I just cannot convert to HQL.
Code:
SELECT w2b.ware AS "Ware", w2b.anzahl AS "Anzahl",w.preis AS "Preis", (w2b.anzahl * w.preis) AS "Gesamt"
FROM ware2bestellung w2b, waren w
WHERE w.name = w2b.ware"
The problem lies in the mathematical operation in the select statement, where amount ('anzahl') and price ('price') are multiplied. I tried a lot of things, but it seems like mathematical expressions are only allowed in the where clause (see
Expressions). I could not find anything about that. I even tried with
() or
[] or
{}. Or is there perhaps a mapping to SQL functions (like
sum or
count)?
So I decided to use
named queries and put the SQL statement into the mapping files. I called the query directly by the name referenced in the mapping file and the query is read correctly. See the output:
Query1
Code:
Hibernate:
SELECT ware.name as "Ware", w2b.anzahl as "Anzahl", ware.preis as "Preis", w2b.anzahl * ware.preis as "Gesamt"
FROM Ware2bestellung w2b, Waren ware
WHERE w2b.ware = ware.name;
The query was executed without errors, but a call to
Query.list() or
Query.uniqueResult() only returned an empty Object[] (
Object[0]). Putting the same string into my SQL client returned a list of results.
I tried the same thing with simpler queries, but I always get an empty Object[]
Query2
Code:
Hibernate:
SELECT *
FROM Ware2bestellung
Query3
Code:
Hibernate:
SELECT *
FROM Waren
Could someone help me with that? Is there a possiblitiy to put arithmetic operations into the SELECT part of the query?
Any help is welcome :)
Kariem
Versions: Hibernate 2.1.8, PostgreSQL 7.2.1