oracle - Java persistence - getSingleResult() did not retrieve any entities -
i testing jpa project follows:
try { emfactory = persistence.createentitymanagerfactory(persistence_unit_name); entitymanager em = emfactory.createentitymanager(); query qry = em.createquery("select t testcon t"); system.out.println(qry); testcon tcon = (testcon) qry.getsingleresult(); //qry.getsingleresult(); rslt = tcon.getb(); if (rslt == null || rslt.equals("")) rslt = "could not result"; system.out.println(rslt); //new showmsg(rslt); } catch (exception ex) { system.out.println(ex); //new showmsg("exception occured"); }
the output is:
javax.persistence.noresultexception: getsingleresult() did not retrieve entities.
the qry string is:
ejbqueryimpl(readallquery(referenceclass=testcon sql="select a, b c##test.testcon"))
when test directly in sqlplus:
select a, b c##test.testcon
it gives me correct result. can not find out program fails.
create transaction before executing query.
try { emfactory = persistence.createentitymanagerfactory(persistence_unit_name); entitymanager em = emfactory.createentitymanager(); em.gettransaction().begin(); //added query qry = em.createquery("select t testcon t"); system.out.println(qry); //need use getresultlist() if table has > 1 row list<testcon> tcon = (list<testcon>) qry.getresultlist(); for(testcon rslt:tcon){ rslt = tcon.getb(); if (rslt == null || rslt.equals("")){ rslt = "could not result"; } system.out.println(rslt); //new showmsg(rslt); } entitymanager.gettransaction().commit(); //added entitymanager.close(); //added } catch (exception ex) { system.out.println(ex); //new showmsg("exception occured"); }
Comments
Post a Comment