【问题标题】:Spring + Hibernate JPA QuestionSpring + Hibernate JPA 问题
【发布时间】:2010-01-15 05:12:03
【问题描述】:

我正在尝试将 Hibernate 与 JPA/EntityManager 一起使用来进行数据库活动

现在我收到这个错误,我不知道它是什么意思。

在我拥有这段代码之前,它工作正常。

public class JdbcProductDao extends Dao implements ProductDao {

/** Logger for this class and subclasses */
protected final Log logger = LogFactory.getLog(getClass());

public List<Product> getProductList() {
    logger.info("Getting products!");
    List<Product> products = getSimpleJdbcTemplate().query(
            "select id, description, price from products", 
            new ProductMapper());
    return products;
}

public void saveProduct(Product prod) {
    logger.info("Saving product: " + prod.getDescription());
    int count = getSimpleJdbcTemplate().update(
        "update products set description = :description, price = :price where id = :id",
        new MapSqlParameterSource().addValue("description", prod.getDescription())
            .addValue("price", prod.getPrice())
            .addValue("id", prod.getId()));
    logger.info("Rows affected: " + count);
}

private static class ProductMapper implements ParameterizedRowMapper<Product> {

    public Product mapRow(ResultSet rs, int rowNum) throws SQLException {
        Product prod = new Product();
        prod.setId(rs.getInt("id"));
        prod.setDescription(rs.getString("description"));
        prod.setPrice(new Double(rs.getDouble("price")));
        return prod;
    }

}

}

但是这段代码使用了EntityManager

public class JdbcProductDao implements ProductDao {

/** Logger for this class and subclasses */
//protected final Log logger = LogFactory.getLog(getClass());

@PersistenceContext()
private EntityManager entityManager;

public JdbcProductDao(){

}

public Product getReference(Product product){
    return getEntityManager().getReference(product.getClass(),product.getId());
}

public void persist(Product product){
    getEntityManager().persist(product);
}

public EntityManager getEntityManager(){
    return entityManager;
}

public void setEntityManager(EntityManager entityManager){
    this.entityManager = entityManager;
}

@SuppressWarnings("unchecked")
public List<Product> getProductList(){
    return getEntityManager().createNativeQuery("select id, description, price from products").getResultList();
}

public void saveProduct(Product product){
    getEntityManager().createNativeQuery("update products set description = " + product.getDescription() + " , price = " + product.getPrice() + " where id = " + product.getId());
}

private static class ProductMapper implements ParameterizedRowMapper<Product> {

    public Product mapRow(ResultSet rs, int rowNum) throws SQLException {
        Product prod = new Product();
        prod.setId(rs.getInt("id"));
        prod.setDescription(rs.getString("description"));
        prod.setPrice(new Double(rs.getDouble("price")));
        return prod;
    }

}

}

我得到的错误是 "java.lang.NumberFormatException: For input string: "description"

有没有人经历过类似的事情?

编辑: 堆栈跟踪如下 java.lang.NumberFormatException:对于输入字符串:“描述” java.lang.NumberFormatException.forInputString(NumberFormatException.java:48) java.lang.Integer.parseInt(Integer.java:449) java.lang.Integer.parseInt(Integer.java:499) javax.el.ArrayELResolver.coerce(ArrayELResolver.java:153) javax.el.ArrayELResolver.getValue(ArrayELResolver.java:45) javax.el.CompositeELResolver.getValue(CompositeELResolver.java:54) org.apache.el.parser.AstValue.getValue(AstValue.java:118) org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:186) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:935) org.apache.jsp.WEB_002dINF.jsp.hello_jsp._jspx_meth_c_005fout_005f1(hello_jsp.java:245) org.apache.jsp.WEB_002dINF.jsp.hello_jsp._jspx_meth_c_005fforEach_005f0(hello_jsp.java:210) org.apache.jsp.WEB_002dINF.jsp.hello_jsp._jspService(hello_jsp.java:92) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) javax.servlet.http.HttpServlet.service(HttpServlet.java:717) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267) javax.servlet.http.HttpServlet.service(HttpServlet.java:717) org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:236) org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:257) org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1183) org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:902) org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:807) org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:571) org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:501) javax.servlet.http.HttpServlet.service(HttpServlet.java:617) javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

【问题讨论】:

  • 请发布完整的堆栈跟踪。

标签: database hibernate spring jpa


【解决方案1】:

查看堆栈跟踪 - 与 JPA 无关,您的 JSP 中 &lt;c:out&gt; 标记的属性中有 EL 语法错误。

【讨论】:

    猜你喜欢
    • 2015-11-24
    • 2010-12-27
    • 2016-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-16
    相关资源
    最近更新 更多