16

Is possible with Hibernate do this?

select A.something, B.something, C.something, D.something , E.*  
      from  A  
      LEFT OUTER JOIN B on A.id = B.id_fk  
      LEFT OUTER JOIN C ON B.id = C.id_fk  
      LEFT OUTER JOIN D ON C.id = D.id_fk  
      LEFT OUTER JOIN E ON A.abc = E.abc;  

This query works fine in SQL but gives below Exception in Hibernate:

org.hibernate.hql.ast.QuerySyntaxException: expecting IDENT, found '*' near line 1

2 Answers 2

27

you don't need the "*". hibernate is turning objects , * is meaningless as its for pointing the columns in SQL. When you want the E , it turns the E object.

SELECT E FROM E

this is the HQL for SELECT * FROM E

even "FROM E" works , when you're calling session.createQuery() .

7
  • i understand that, but i also need to select other columns from other tables as well as mentioned in the question: select A.something, B.something, C.something, D.something , E.*
    – sunny_dev
    Apr 6, 2012 at 8:59
  • you cannot select columns , you can only select attributes defined in the entity class of your object Apr 6, 2012 at 9:00
  • i totally agree with what you are saying. Please let me know if it is posible to retrieve few attributes from diffrent table and all attributes from a single table, all this using a single HQL query. Meaning is it possible to achieve what my SQL query does in my mentioned question? Do you mean to say I should write as below: select A.something, B.something, C.something, D.something , E from A LEFT OUTER JOIN B on A.id = B.id_fk LEFT OUTER JOIN C ON B.id = C.id_fk LEFT OUTER JOIN D ON C.id = D.id_fk LEFT OUTER JOIN E ON A.abc = E.abc; This new query is not giving exception but gives NULL for E
    – sunny_dev
    Apr 6, 2012 at 9:34
  • i say , in your hql in your question , replace "E" with "E.*" Apr 6, 2012 at 9:38
  • 1
    hey, i already said that i tried using "E" but it gives NULL for E.
    – sunny_dev
    Apr 6, 2012 at 10:32
0

Query in HQL is differ - SQL --

Select * from A ;

HQL --

from A

SQL --

select b,c ,d from A

HQL --

select b,c,d from A

org.hibernate.Query query = createQuery("select b FROM aaaa");
        query.setReadOnly(true);
        return (List<Vector<aaaa>>)query.list();

https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.