views:

396

answers:

2
  public static List<Transaction1> debitSourceAccBalance (Integer youraccinput, Integer toaccinput, String recname, Double amtsender) {
    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();
    Query q = em.createQuery("SELECT t from Transaction1 t where t.fromAccNo =:youraccinput AND t.toAccNo =:toaccinput AND t.name =:recname AND t.amount >=:amtsender");
    q.setParameter("youraccinput", youraccinput);
    q.setParameter("toaccinput", toaccinput);
    q.setParameter("recname", recname);
    q.setParameter("amtsender", amtsender);
    Double currbal = (Double)q.getSingleResult();

    amtsender = currbal - amtsender;
    Query u = em.createQuery("UPDATE Transaction1 t SET t.amount =:amtsender WHERE t.fromAccNo =:youraccinput AND t.toAccNo =:toaccinput");
    u.setParameter("youraccinput", youraccinput);
    u.setParameter("toaccinput", toaccinput);
    u.setParameter("amtsender", amtsender);

    u.executeUpdate();
    return q.getResultList();

Servlet:

              Integer youraccinput = Integer.parseInt(request.getParameter("fromAccNo"));
              Integer toaccinput = Integer.parseInt(request.getParameter("toAccNo"));
              String recname = request.getParameter("name");
              Double amtsender = Double.parseDouble(request.getParameter("amount"));


        List<Transaction1> results = Transaction1.debitSourceAccBalance(youraccinput, toaccinput, recname, amtsender);
                       results.listIterator();
                       request.getRequestDispatcher("ListSingleTransaction").forward(request, response);

why am i getting this error message?? please help to fix!!

"Entities.Transaction1 cannot be cast to java.lang.Double"

+1  A: 

It looks like this line

Double currbal = (Double)q.getSingleResult();

is attempting to cast a Transaction1 to Double and that isn't legal. A Transaction1 isn't a Double.

Transaction1 is the type that you're getting from your query. Presumably, that Transaction1 has the value that you're looking for as one of its fields. Looking at your query, it has at least the fields fromAccNo, toAccNo, name, and amount. It probably has a field called currbal or something similar with the value that you're looking for. Whatever it is you're trying to get out of the query, you're going to have to get it out of the object that you got from the query. The result itself is not the value that you're looking for but rather contains that value. The result will represent a row in the table, not just one column in that row.

Jonathan M Davis
How do i fix it?
kobrakai
yes, you are completely correct, but i still dont know what to fix the code.
kobrakai
thank you for the understanding, this help me to understand now.thanks again.
kobrakai
+1  A: 

The cast error is at

Double currbal = (Double)q.getSingleResult();

You don't even know why you're casting it?

As a quick fix to help you get pass this one, how to fix this is very simple. Just change it to:

Transaction1 transaction = (Transaction1) q.getSingleResult();
Double currbal = transaction.getCurrentBalance();  // method name assumed

Your JPA query is returning an entity object, not a single primitive value, so you can't cast it to double because your query did not select a double - it selected a Transaction1 object, hence you need to cast the single result from the query into whatever you selected back.

Read up more on JPA.

aberrant80
This works flawlessly, thanks a lot, i really do need to more on JPA, i thought the logic seemed fine to me, as I learned from my lecturer that I just need to use it and dont need to know how, i guess i misunderstood. thus, its the code i copied from him.
kobrakai
You're learning JPA as a student? Good to get a head start. Cheers.
aberrant80