Search code examples
javamysqlhibernatehql

Convert SQL into HQL where clause


this should be easy, but my app throws constant error, and I'm new to Hibernate.

I am trying to have simple HQL query in my web app using Hibernate.

I want to execute the following SQL query:

SELECT * FROM deal WHERE deal_status='A' OR WHERE deal_status='O';

HQL does not seem to work with or clause, here my current HQL statements in my web app:

FROM deal d where d.deal_status='O' or d.deal_status='A' order by d.id

Thanks for any Hibernate query help

Regards Alex


Solution

    • The HQL is based on the name of fields of your entity not of your class, and the Class Names
    • c.id - c is not defined

    I strongly assume that the fields in Deal (Classname first letter upper case) are not deal_status but status, then the query must be like this. (I skipped the c.id stuff, because I have no idea what you mean by this.)

     SELECT d FROM Deal d WHERE d.status='O' or d.status=`A`
    

    Notice the upper D from Deal

    And this will only work if Deal.status is a String or something like that, but not if it is a Enum. One way to handle an Enum is this:

    Query query = session.createQuery(
        "SELECT d FROM Deal d WHERE d.status=:o or d.status=:a");
    query.setParameter("a", MyEnum.A);
    query.setParameter("o", MyEnum.O);