views:

1524

answers:

3

I'm using oracle10g database and eclipselink, I need to obtain the last inserted key from the table so i've created this query

javax.persistence.Query q =   
                    em.createQuery("SELECT nvl(MAX(c.myAtt),0) " +   
                        "from myTable as c");   
             return Integer.parseInt(q.getSingleResult().toString());   `

But when the table is empy(sometimes it might get empty) i'm getting ILEGAL ARGUMENT EXCEPTION, cause: JPQL Exception, detail: "An exception occurred while creating a query in EntityManager". What i'm doing wrong?

+1  A: 
ChssPly76
A: 

First of all, thank you for your answer. I just got some feedback, the information i need to retrieve is not the pk after all. if plan to do something like this:

`public int getLastInserted() throws Exception{

   try{
        javax.persistence.Query q =
               em.createQuery("select max(c.authNumber) from Autorizaciones as c");

       if(q.getSingleResult() != null){
           return Integer.parseInt(q.getSingleResult().toString());
       }
       else
           return 0;

    }catch(Exception e){
        e.printStackTrace();
        throw new Exception();
    }
}` please tell me why do you consider this a BAD THING?. could you suggest a better solutions. Thanks, Jorge.
jbendahan
A: 

in the meanwhile somebody else could have inserted something in Autorizaciones and then you receive the wrong id

Robar