开发者

Entities cannot be cast to java.lang.Double

  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);
   开发者_如何学Go 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"


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜