How to return maximum identifier column via Hibernate, access to Oracle DB?

Since it seems that HQL (createQuery) does not support scalar queries, I used a raw query through createSQLQuery:

        session.beginTransaction();
        Query query = session.createSQLQuery("select nvl(max(id), 0) + 1  from empty_table_for_now");

        session.getTransaction().commit();
        List listL = query.list();
        Iterator iterator = listL.iterator();
        while (iterator.hasNext()) {
            LOG.info("Infinite loop. This is disgusting!");
        }           

The query itself is not interrupted, but it looks like it returns a list with an infinite number of results? It makes no sense ... Any idea why I get this?

Even better, is there a better, more elegant way to get the intended scalar without getting confused with whoever supports my "list" code (which should always contain one element)?

+4
source share
2 answers

, ( ).

Iterator.next()

while (iterator.hasNext()) {
    Object nextElement = iterator.next();
    LOG.info("Next element is: " + nextElement);
} 

, ResultSet.next() , . javadocs.

+5

Oracle DB .

, , Hibernate .

FYI: SQL Server - MySQL/Maria DB - PostgreSQL - , , PostgreSQL

: .

+4

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


All Articles