Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return a single result object from hibernate query?

Tags:

hibernate

Does a query execution always have to return a list ? How do I replace the code below if I am sure it will only return a single object ?

@Override
    public List<DocInfo> findAllByDocId(String docId)
    {
        Query q = getCurrentSession().createQuery("from DocInfo item where item.id = :docId");
        q.setString("docId", docId);
        List<DocInfo> docInfoList = q.list();
        return docInfoList;
    }
like image 623
Phoenix Avatar asked Sep 14 '12 15:09

Phoenix


1 Answers

You can use Query#uniqueResult() with Hibernate if I am not wrong. I think that is what you are looking for. In this case, you have to handle the NonUniqueResultException in your code if there is more than one row returned from your query.

like image 104
Guido Avatar answered Nov 11 '22 17:11

Guido