1

I am getting an error when i am trying to display few contents of a table using named query in hibernate. I have tried looking for answers, with no success. The code is listed below.

<sql-query name="ActiveCustomers">
<return alias="cts" class="Customer"/>
    SELECT 
        cts.cid AS {cts.cid},
        cts.cname AS {cts.cname},
        cts.email AS {cts.email},           
        cts.status {cts.status}
    FROM Customers cts
    WHERE cts.status=:st
</sql-query>

From the client side I am invoking it as shown below:

SessionFactory sf=CHibernateUtil.getSessionFactory();
        Session session=sf.openSession();
        tx=session.beginTransaction();

        list=session.getNamedQuery("ActiveCustomers").setString("st","Active").list();
        for(Customer c:list){
            System.out.println(c);
        }

        tx.commit();
        session.close();

But I am getting this error:

Hibernate:

    SELECT 
        cts.cid AS cid0_,
        cts.cname AS cname0_0_,
        cts.email AS email0_0_,         
        cts.status status0_0_
    FROM Customers cts
    WHERE cts.status=?



org.hibernate.exception.GenericJDBCException: could not execute query
at org.hibernate.exception.ErrorCodeConverter.handledNonSpecificException(ErrorCodeConverter.java:92)
.
.
 Caused by: java.sql.SQLException: Column 'city0_0_' not found.

But If i add all the columns of the table then it works. But that's not my requirement. My requirement is to display only cid,cname,email and status.

2 Answers 2

1

Finally figured it out.

When we are using the below syntax we need to specify the entire class variables and the return type is also an object type or the class type.

<sql-query name="ActiveCustomers">
<return alias="cts" class="Customer"/>
SELECT 
    cts.cid AS {cts.cid},
    cts.cname AS {cts.cname},
    cts.email AS {cts.email},           
    cts.status {cts.status}
FROM Customers cts
WHERE cts.status=:st
</sql-query>

On the other hand when we want to select few or specific columns then we need to use a different tag called

<return scalar ... 

here the return type is object array and from that object array we need to print out the values. Now to meet our requirement the above query can be re-written as follows:

Also note the difference in the syntax of select statements.

<sql-query name="ActiveCustomers">
<return-scalar column="cid" type="string"/>
<return-scalar column="cname" type="string"/>
<return-scalar column="email" type="string"/>
<return-scalar column="status" type="string"/>

    SELECT 
        cts.cid AS cid,
        cts.cname AS cname,
        cts.email AS email,         
        cts.status status
    FROM Customers cts
    WHERE cts.status=:st
</sql-query>

The change in the client side is how we read the data ..

 for(Object obj[]:aclist){
            for(Object o:obj){
                System.out.println(o);
            }
        }
Sign up to request clarification or add additional context in comments.

2 Comments

user2900314, I am not clear on what exact change you made o fix the issue, you said we need to use a different tag called what is that different tag? Also you have provided 2 <sql-query> in your answer out of which 1 is matching with your question. Can you please provide more details on how you fixed this issue?
@Chaitanya There was some issues in editing.. I have fixed it now, have a look at it now, and let me know if you need any more clarifications. The first <sql-query> is were we had issues and the second one is corrected one.
0

may be in below you are missing AS ( cts.status AS {cts.status} )

<sql-query name="ActiveCustomers">
<return alias="cts" class="Customer"/>

SELECT
cts.cid AS {cts.cid},
cts.cname AS {cts.cname},
cts.email AS {cts.email},

cts.status {cts.status}
FROM Customers cts
WHERE cts.status=:st </sql-query>

2 Comments

No that does not work, the entire query has to be rewritten. Please read my answer that has been listed above.
Yes. You have to use scalar here as well.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.