【问题标题】:Transmit data into jsp from 2 tables in one query一次查询将数据从2个表传输到jsp
【发布时间】:2013-06-12 13:29:29
【问题描述】:

我有两个表(实体类),需要从两者中选择并将结果传输到 jsp 页面。

我的平衡实体:

 @Entity
    @NamedQuery (name = "findLastFiveTransaction",
    query = "select c, b from Categorietype c, Balance b  where c.user = :user and b.user = :user")
    public class Balance implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private String id;

    private BigDecimal sum;
    @Column(name="Description")
    private String descrip;

类别类型的我的实体:

@Entity
public class Categorietype implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private String id;

    private String name;

    //bi-directional many-to-one association to Balance
    @OneToMany(mappedBy="categorietype")
    private List<Balance> balances;

    //bi-directional many-to-one association to User
    @ManyToOne
    @JoinColumn(name="UserID")
    private User user;

EJB 类中的方法:

 public List getUserTransaction(User user) {
    return em.createNamedQuery("findLastFiveTransaction").setParameter("user", user).getResultList();
    }

我在jsp页面上的代码:

<table border="1">

  <tr>
    <th>Sum</th>
    <th>Descrip</th>
    <th>Name</th>
  </tr>
<c:forEach items="${result}" var="r">
  <tr>
    <td> ${r.sum} </td>
    <td> ${r.descrip} </td>
    <td> ${r.name} </td>
  </tr>
</c:forEach>
</table> 

在这种变体中我收到消息

org.apache.jasper.JasperException:java.lang.NumberFormatException:对于输入字符串:“sum”

我的问题是如何解析传输到jsp页面的数据。我从我的 servlet 传输数据:

List result = balance.getUserTransaction(user);
request.setAttribute("result", result );
request.getRequestDispatcher("finance.jsp").forward(request, response);

我必须如何纠正或告诉我在哪里可以阅读?

【问题讨论】:

  • 请提供有关您正在使用的框架的信息。
  • 我不使用框架。一切都在 Eclipse 中完成。所以只用于 JPA 而没有添加外部。

标签: java select jakarta-ee html-table jpql


【解决方案1】:

我找到了这个问题的解决方案。正确查询

select b from Categorietype c, Balance b  where c.user = :user and b.user = :user

并在 jsp 页面上将 ${r.name} 更改为 r.typecategory.sum

<table border="1">

  <tr>
    <th>Sum</th>
    <th>Descrip</th>
    <th>Name</th>
  </tr>
<c:forEach items="${result}" var="r">
  <tr>
    <td> ${r.sum} </td>
    <td> ${r.descrip} </td>
    <td> ${r.typecategory.name} </td>
  </tr>
</c:forEach>
</table> 

附言 我将查询更改为

select b.id as id, b.sum, b.description, t.name from Balance b join b.typecategory t where b.user = :user and b.typecategory = t and b.status = :status

这样比较正确

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-17
    • 1970-01-01
    • 2014-07-02
    • 2014-03-06
    • 1970-01-01
    • 2017-11-16
    相关资源
    最近更新 更多