sagar limbu
sagar limbu

Reputation: 1262

how to fetch last column of the selected table using hibernate?

i am trying to fetch the id of last column in descending order.

the query which returns last column is

select id from(select id from challan 
    order by id desc) where ROWNUM=1;

now i am trying to do same thing using hibernate.

public long getIdOnChallanTable() {
        session = sessionFactory.openSession();
        trans = session.beginTransaction();

        Query<Object[]> query = session.createNativeQuery("select id 
from(select id from challan order by id desc) where ROWNUM=1;");

        Long value = 0L;
        List<Object[]> list = query.getResultList();
        for ( Object lst : list){
            Object[] objects =(Object[]) lst;
           value=(Long)(objects[0]);
        }
        return value;

    }

and the error is:

2017-07-26 12:37:36 [http-nio-7080-exec-1] WARN :: SQL Error: 911, SQLState: 22019
2017-07-26 12:37:36 [http-nio-7080-exec-1] ERROR:: ORA-00911: invalid character

update error javax.persistence.PersistenceException: 
org.hibernate.exception.SQLGrammarException: could not extract ResultSet

Upvotes: 0

Views: 61

Answers (1)

Arnold Galovics
Arnold Galovics

Reputation: 3416

You don't need the semicolon at the end of the query and please use proper whitespacing. In the FROM clause, you don't have the whitespace between the subquery and the FROM keyword.

Note: don't forget to commit/rollback the transaction at the end and handle the exceptions as well. I hope this was just a sketch to show us the problem and not a code from a real world application.

Upvotes: 1

Related Questions