How to put a comment into HQL (Hibernate Query Language)? - database

Is it possible to put comments into Hibernate Query Language? If so, how?

Make sure your session is configured with:
<property name="hibernate.use_sql_comments">true</property>
Then do:
Query query = ...;
query.setComment("Some comment here");
and you will see something like the following in your MySQL log file (if you're using MySQL):
5998 Query /* Some comment here */ select .....

AFAIK, HQL does not support comments.

If it helps your development, Hibernate Tools (Eclipse) supports double hyphens as single-line comments in their HQL editor. Helps me a lot. I've just tried the JPQL statement
SELECT pro --ro.id, cl.name, te.ordinalNbr, tt.code, se.startYear, pro.id, pcl.name, pte.ordinalNbr, ptt.code, pse.startYear
FROM Roster ro
JOIN ro.season se
JOIN ro.team te
JOIN te.club cl
JOIN te.teamType tt
JOIN te.rosters pro
JOIN pro.season pse
JOIN pro.team pte
JOIN pte.club pcl
JOIN pte.teamType ptt
WHERE ro.id = 32
ORDER BY pse.startYear
and it returned the pro instances.
Also not quite to the point, but it might be useful nontheless.

Related

Why Hibernate HSQL Concat is not working for MSSQL?

So, I have Hibernate 5.3.1 in a project which connects to different enginees (MySql, Oracle, PostgreSQL and MS SQL), so I can't use native queries.
Let's say I have 3 records in a table, which all of them have the same datetime, but I need to group them only by date (not time). For example, 2019-12-04;
I execute this query:
SELECT
CONCAT(year(tx.date_), month(tx.date_), day(tx.date_)),
iss.code,
COUNT(tx.id)
FROM
tx_ tx
JOIN
issuer_ iss
ON
tx.id_issuer = iss.id
GROUP BY
CONCAT(year(tx.date_), month(tx.date_), day(tx.date_)), iss.code
But, when I test it connected to SQL SERVER 2017, instead of return 20191204, it's returning 2035. In Oracle and MySQL is working fine.
Anyone has any idea why is this happen? I've tried different ways, like use + instead of CONCAT but the result is the same.
I've also tried to extract them for separate (without concat), and they have been returning correct. The problem is, I need to group them by the complete date.
And just for the record, the field is declared as datetime2 in DDBB
How about simply adding them, instead of using CONCAT.
(year(tx.date_)*10000 + month(tx.date_)*100 + day(tx.date_)*1) AS datenum
Thus, try this:
SELECT
CAST((year(tx.date_)*10000 + month(tx.date_)*100 + day(tx.date_)*1) AS string) AS datenum,
iss.code
FROM tx_ tx
JOIN issuer_ iss
ON tx.id_issuer = iss.id
GROUP BY year(tx.date_), month(tx.date_), day(tx.date_), iss.code
Thanks for the hint Gert Arnold gave me. I just didn't realize that the query was adding like if they were numbers in MSSQL.
Finally, I manage to make it work in the 4 RDBMS casting to string first
SELECT
CONCAT(CAST(year(tx.date_) AS string), CAST(month(tx.date_) AS string), CAST(day(tx.date_) AS string)),
iss.code
FROM
tx_ tx
JOIN
issuer_ iss
ON
tx.id_issuer = iss.id
GROUP BY
CONCAT(year(tx.date_), month(tx.date_), day(tx.date_)), iss.code
I tried also casting to TEXT, but it throws exception in MySQL
Why use concat() to begin with?
Assuming Hibernate takes care of converting the non-standard year(), month() and day() functions, then the following should work on any DBMS
SELECT year(tx.date_), month(tx.date_), day(tx.date_), iss.code
FROM tx_ tx
JOIN issuer_ iss ON tx.id_issuer = iss.id
GROUP BY year(tx.date_), month(tx.date_), day(tx.date_), iss.code

Is there a way to shorten my SQL Server code

I'm quite new to SQL server and basically I have this query that uses two table and I was wondering if there's an easy way to shorten the code, the repeating of the table names looks pretty bad.
SELECT
dbo.atbl_Sales_OrdersLines.OrderID, dbo.atbl_Sales_OrdersLines.Created,
dbo.atbl_Sales_OrdersLines.CreatedBy, dbo.atbl_Sales_OrdersLines.Updated,
dbo.atbl_Sales_OrdersLines.UpdatedBy,
dbo.atbl_Sales_OrdersLines.CUT, dbo.atbl_Sales_OrdersLines.CDL,
dbo.atbl_Sales_OrdersLines.Domain, dbo.atbl_Sales_OrdersLines.ProductID,
dbo.atbl_Sales_OrdersLines.Amount, dbo.atbl_Sales_Products.ProductName,
dbo.atbl_Sales_Products.Supplier, dbo.atbl_Sales_Products.Quantity AS TotalQuantity,
dbo.atbl_Sales_Products.Price, dbo.atbl_Sales_OrdersLines.PrimKey
FROM
dbo.atbl_Sales_OrdersLines
INNER JOIN
dbo.atbl_Sales_Products ON dbo.atbl_Sales_OrdersLines.ProductID = dbo.atbl_Sales_Products.ProductID
There has to be an easier way to do this. Thank you.
Use tables alias to shorten that code:
SELECT
ol.OrderID,
ol.Created, ol.CreatedBy, ol.Updated, ol.UpdatedBy,
ol.CUT, ol.CDL,
ol.Domain, ol.ProductID, ol.Amount,
p.ProductName, p.Supplier, p.Quantity AS TotalQuantity, p.Price,
ol.PrimKey
FROM
dbo.atbl_Sales_OrdersLines ol
INNER JOIN
dbo.atbl_Sales_Products p ON ol.ProductID = p.ProductID
The ol and p are table alias that you can choose - I recommend choosing something that is "intuitive", e.g. "ol" for "Order Lines", "p" for "Product" - that makes reading (and understanding) your SQL code much easier

Joining three worksheets in Report Builder query using ODBC connection to spreadsheet

This may be a longshot, but I was trying to join three tabs of a spreadsheet in a report builder 3.0 data source. Since I know I can't join two data sets in a table, I'm trying to format the original datasource query.
It works if I join two tabs; all fields of the two tabs are available. When I add a third, it errors out with
"ERROR [42000] ... Syntax error (missing operator) in query expression"
This works:
SELECT LESigner.Name, AcctSigner.Account
FROM [LESigner$] LESigner
JOIN [SignersByAccount$] AcctSigner
on LESigner.AccountCode = AcctSigner.Account
This does not
SELECT LESigner.Name, AcctSigner.Account, LE.ID
FROM [LESigner$] LESigner
JOIN [SignersByAccount$] AcctSigner
on LESigner.AccountCode = AcctSigner.Account
JOIN [LegalEntity$] LE
on LE.ID = LESigner.ID
I appreciate any thoughts or advice.
other relevant facts:
Report builder 3.0
Excel 2013
Thanks,
-bc
Try putting parenthesis around JOINs.
Per: Syntax error (missing operator) in query expression
SELECT LESigner.Name, AcctSigner.Account, LE.ID
FROM ([LESigner$] LESigner
JOIN [SignersByAccount$] AcctSigner
on LESigner.AccountCode = AcctSigner.Account )
JOIN [LegalEntity$] LE
on LE.ID = LESigner.ID

microsoft sql select query from multiple tables

This query is working in mysql but is not working in microsoft sql server management studio 2008, can someone help me out?
SELECT DISTINCT C.firstname,C.lastname,QC.category_name,QR.cid,QR.catid,QR.rhid
FROM cms_question_report QR,
cms_clients C,
cms_questioncategory QC ,
cms_reporthistory RH
WHERE C.id=QR.cid
AND QR.rhid=RH.id
AND QR.catid='3'
AND QR.catid=QC.id
I am getting the error: Invalid object name cms_question_report
SELECT DISTINCT C.firstname,C.lastname,QC.category_name,QR.cid,QR.catid,QR.rhid
FROM cms_question_report QR
left join cms_clients C
on C.id=QR.cid
left join cms_questioncategory QC
on QR.catid=QC.id
and QR.catid='3'
left join cms_reporthistory RH
on QR.rhid=RH.id
I think this should do
specify Normally it happens when you have specific schema and you don't specify it for example:
Replace dbo. with your schema and/or type your database name
SELECT DISTINCT C.firstname,C.lastname,QC.category_name,QR.cid,QR.catid,QR.rhid
FROM databasename.dbo.cms_question_report QR,
databasename.dbo.cms_clients C,
databasename.dbo.cms_questioncategory QC ,
databasename.dbo.cms_reporthistory RH
WHERE C.id=QR.cid
AND QR.rhid=RH.id
AND QR.catid='3'
AND QR.catid=QC.id

How to join on spatial functions in Peewee?

I'm struggling with figuring out how to join a table in Peewee based on a fairly common table postgis pattern. I need to join a table based on a postgis function (st_contains). I imagine it would look something like:
Station.select().join(Location, on=fn.ST_Intersects(Station.geom, Location.geom)).where(Location.name == 'Ravenswood')
The above query, if supported, would return all Stations in the Location named Ravenswood. The equivalent SQL would be:
SELECT station.name, station.district, station.line
FROM station INNER JOIN location ON ST_Intersects(station.geom, loc.geom)
WHERE location.name = 'Ravenswood';
Unfortunately my experiments all seem to end with this abbreviated traceback:
File "/Users/j.../python2.7/site-packages/peewee.py", line 1555, in generate_joins
left_field = field.to_field
AttributeError: 'NoneType' object has no attribute 'to_field'
Does peewee support this? I can't find it in the documentation.
Peewee's join generation checked for expressions but not function calls. I have fixed this bug in 61034c5 (released in v2.6.1). If you use peewee master you should be able to run the query now.

Resources