Convert SQL to HQL

I am trying to convert the following SQL query to HQL and have several problems. Direct string-to-string conversion doesn't work, I'm wondering if I should use Inner Join in HQL?

        SELECT (UNIX_TIMESTAMP(cosc1.change_date) - UNIX_TIMESTAMP(cosc2.change_date)) 
        FROM customer_order_state_change cosc1  
        LEFT JOIN customer_order_state cos1_new on cosc1.new_state_id = cos1_new.customer_order_state_id  
        LEFT JOIN customer_order_state cos1_old on cosc1.old_state_id = cos1_old.customer_order_state_id  
        LEFT JOIN customer_order_state_change cosc2 on cosc2.customer_order_id = cosc1.customer_order_id  
        LEFT JOIN customer_order_state cos2_new on cosc2.new_state_id = cos2_new.customer_order_state_id  
        LEFT JOIN customer_order_state cos2_old on cosc2.old_state_id = cos2_old.customer_order_state_id 
        WHERE cos1_new.name = "state1" AND  cos2_new.name = "state2" and cosc2.change_date < "2008-11-06 09:00" 
AND cosc2.change_date > "2008-11-06 06:00" GROUP BY cosc1.change_date, cosc2.change_date ;

The request returns the time in seconds between state changes for a sales order.

Names and status dates are dynamically inserted into the query.

Edit: Just tried this

"SELECT (UNIX_TIMESTAMP(cosc1.changeDate) - UNIX_TIMESTAMP(cosc2.changeDate))" + 
        " FROM" + 
        " " + CustomerOrderStateChange.class.getName() + " as cosc1" +
        " INNER JOIN " + CustomerOrderStateChange.class.getName() +  " as cosc2" +
        " WHERE cosc1.newState.name = ?" +
        " AND cosc1.order.id = cosc2.order.id" + 
        " AND cosc2.newState.name = ?" +
        " AND cosc2.changeDate < ?" +
        " AND cosc2.changeDate > ?" + 
        " GROUP BY cosc1.changeDate, cosc2.changeDate";

and the resulting exception is “external or full join must be accompanied by a path expression”

+3
source share
2 answers

HQL, , , Foo Bar Foo.bar Bar, from Foo f inner join f.bar as b . , HQL ( ).

, Hibernate ( ) SQL- session.createSQLQuery(...).

+6

SQL PreparedStatement, , Hibernate session.createSQLQuery() .

0

Source: https://habr.com/ru/post/1699185/


All Articles