2

I have such a query :

String hql ="FROM Appointment A WHERE " 
                +"(UNIX_TIMESTAMP(A.dateOfFirst) <= "+time+" AND ( (A.dateOfLast IS NULL) OR (UNIX_TIMESTAMP(A.dateOfLast) >= "+time+")))" 
                +"AND( "+time+" < UNIX_TIMESTAMP(A.dateOfFirst)+86400 OR" // Jeśli data jest datą początkową 
                +"(DAYOFMONTH(A.dateOfFirst) = "+day+" AND A.MontRepetition = TRUE) " miesiąca
                +" OR (  MOD( "+time/86400+"- (to_days(A.dateOfFirst)-719528) , A.repetition) =0 ) "
            +")";

after inserting the date 13.09.2015

    FROM Appointment A WHERE (UNIX_TIMESTAMP(A.dateOfFirst) <= 1442141610 AND ( (A.dateOfLast IS NULL) 
OR (UNIX_TIMESTAMP(A.dateOfLast) >= 1442141610)))AND( 1442141610 < UNIX_TIMESTAMP(A.dateOfFirst)+86400 
    OR(DAYOFMONTH(A.dateOfFirst) = 13 AND A.MonthRepetition = TRUE)  OR (  MOD( 16691- (to_days(A.dateOfFirst)-719528) , A.repetition) =0 ) )

This query should return me this column :

 id    name     dateOFFirst   DateOfLast   repetition  MonthRepetition 
'20', 'test6', '2015-09-12', '2015-09-25',    '1',           '0'

But its not.

When i'm trying only this part :

     FROM Appointment A WHERE UNIX_TIMESTAMP(A.dateOfFirst) <= 1442141610 
AND ( (A.dateOfLast IS NULL) OR (UNIX_TIMESTAMP(A.dateOfLast) >= 1442141610) )

It dont return this column either

But in MySQL Workbench this SQL :

SELECT * FROM calendar.appointments WHERE (UNIX_TIMESTAMP(date_of_first) <= 1442141610 AND ( date_of_last IS NULL OR UNIX_TIMESTAMP(date_of_last) > 1442141610))

Return that column.

I don't now why this hql returns only rows whith null in "DateOfLast"

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Mateusz Mańka
  • 848
  • 1
  • 6
  • 18
  • 3
    [Enable query log in Hibernate](http://stackoverflow.com/questions/1710476/print-query-string-in-hibernate-with-parameter-values) and study the SQL query Hibernate creates based on your HQL query. And two remarks: try to avoid cobbling queries together using string concatenation, it's messy, error-prone and can lead to security issues, use parameterized queries instead. Second, a query returns a record or records, each made up of columns. – fvu Sep 13 '15 at 11:18
  • BTW, such concatenation of variables into SQL/HQL/JPQL queries is good candidate for SQL injection vulnerability. Avoid it and pass variables always via parameters into JPA/Hibernate/PreparedStatement APIs – luboskrnac Sep 13 '15 at 12:25

0 Answers0