开发者

Spring + Hibernate JPA Question

开发者 https://www.devze.com 2022-12-17 13:22 出处:网络
I\'m trying to use Hibernate with JPA/EntityManager to do database activities Right now I\'m getting this error and I have no idea what it means.

I'm trying to use Hibernate with JPA/EntityManager to do database activities

Right now I'm getting this error and I have no idea what it means.

Before I had this code and it works fine.

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 = :descrip开发者_如何学JAVAtion, 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;
    }

}

}

But this code using 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;
    }

}

}

The error I get is "java.lang.NumberFormatException: For input string: "description"

Has anybody experienced something similar to this before?

Edit: The stack trace is below java.lang.NumberFormatException: For input string: "description" 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)


Look at the stacktrace - there is nothing to do with JPA, you have EL syntax error in the attributes of <c:out> tag in you JSP.

0

精彩评论

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