2014-10-08 59 views
0

我正在嘗試添加CRUD操作的產品。起初我沒有使用ajax.Now更改了字符串方法以適應ajax。當我添加一個產品,它拋出該異常CRUD操作中的ClassCastException

「java.lang.ClassCastException:java.lang.Integer中不能轉換爲com.shop.model.Product」

堆棧指向這些DAO的線和服務分別

return (Product) sessionFactory.getCurrentSession().save(product); 
return productDAO.addProduct(product); 

模型

@Entity 
@Table(name="PRODUCTS") 
public class Product { 
    @Id 
    @Column(name="ID") 
    @GeneratedValue 
    private Integer id; 
    @NotEmpty 
    @Column(name="PRODUCT_NAME") 
    private String productName; 
    @NotNull 
    @Min(1) 
    @Column(name="PRICE") 
    private BigDecimal price; 
    @NotNull 
    @Min(1) 
    @Column(name="QUANTITY") 
    private int quantity; 

    @NotEmpty 
    @Column(name="description") 
    private String description; 

    @ManyToOne 
    @JoinColumn(name = "categoryId") 
    private Category category; 

// setters and getters 

位指示

@RequestMapping("addproduct.html") 
    public String viewaddProduct(Map model) { 
     Product product = new Product(); 
     model.put("product", product); 
     return "addproduct"; 
    } 

    @RequestMapping(value ="addproduct", method = RequestMethod.POST, 
      produces = MediaType.APPLICATION_JSON_VALUE, 
      consumes = MediaType.APPLICATION_JSON_VALUE) 
    @ResponseBody 
    public Product addProduct(@RequestBody Product product, 
      Map model) { 
      model.put("productList", productService.listProducts()); 
      model.put("categoryList", categoryService.listCategories()); 
      return  productService.addProduct(product); 

ProductDAOImpl

public Product addProduct(Product product) { 
     return (Product) sessionFactory.getCurrentSession().save(product); 
    } 

ProductServiceImpl

@Transactional 
    public Product addProduct(Product product) { 
     return productDAO.addProduct(product); 
    } 

JSP

<script>$(document).ready(function() { 

     $('#Form').submit(function(event) { 

      var productName = $('#productName').val(); 
      var price = $('#price').val(); 
      var quantity = $('#quantity').val(); 
      var json = { "productName" : productName, "price" : price, "quantity": quantity}; 

     $.ajax({ 
      url: $("#Form").attr("action"), 
      data: JSON.stringify(json), 
      type: "POST", 

      beforeSend: function(xhr) { 
       xhr.setRequestHeader("Accept", "application/json"); 
       xhr.setRequestHeader("Content-Type", "application/json"); 
      }, 
      success: function(product) { 
       var respContent = ""; 

       respContent += "<span class='success'>Product was created: ["; 
       respContent += product.productName + " : "; 
       respContent += product.price + " : " ; 
       respContent += product.quantity + "]</span>"; 

       $("#FromResponse").html(respContent);  
      } 
     }); 

     event.preventDefault(); 
     }); 

    });</script> 
<body> 

<div id="FromResponse"></div> 
<h3> Form</h3> 
<FONT color="blue"></FONT> 
<form:form id="Form" action="addproduct.json" commandName="product" method="POST"> 
<table> 
<tr><td>Product Name:<FONT color="red"><form:errors path="productName" /></FONT></td></tr> 
<tr><td><form:input path="productName" /></td></tr> 
<tr><td>Price:<FONT color="red"><form:errors path="price" /></FONT></td></tr> 
<tr><td><form:input path="price" /></td></tr> 
<tr><td>Quantity:<FONT color="red"><form:errors path="quantity" /></FONT></td></tr> 
<tr><td><form:input path="quantity" /></td></tr> 

<tr><td>Category:<FONT color="red"><form:errors path="category.cid" /></FONT></td></tr> 
<tr><td><form:input path="category.cid" /></td></tr> 
<tr><td>Description:<FONT color="red"><form:errors path="description" /></FONT></td></tr> 
<tr><td><form:textarea path="description" cols="65" rows="10"/></td></tr> 

<tr><td><input type="submit" value="Add Products" /></td></tr> 
</table> 
</form:form> 

</body> 
+0

你能提供一流的產品和更多的堆棧跟蹤,請? – Pracede 2014-10-08 12:15:21

+0

請查閱編輯 – user4058411 2014-10-08 12:20:19

回答

2

sessionFactory.getCurrentSession().save(product)java.lang.Integer你是類型轉換它com.shop.model.Product類,因此你所得到的錯誤返回類型的標識符。

public Serializable save(String entityName, Object object) throws HibernateException
堅持給定的瞬態實例,首先分配一個生成的標識符。 (或者如果使用分配的生成器,則使用標識符屬性的當前值。)如果關聯映射到cascade="save-update",此操作將級聯到關聯的實例。
參數:對象 - 一個持久化類的瞬態的實例
返回:所產生的標識符

你可以做什麼..

sessionFactory.getCurrentSession().save(product); 
return product; 
+0

您能告訴我該怎麼做。 – user4058411 2014-10-08 12:51:58

+0

@ user4058411增加了解決方案。 – 2014-10-08 13:52:51