Search code examples
springhibernatespring-mvcspring-boothibernate-mapping

I want to fetch my product details in database but it throws excepion org.hibernate.hql.internal.ast.QuerySyntaxException: Product is not mapped


I'm keep getting an exception. I've tried to solve this problem for a few days now...

Please help me...

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.hql.internal.ast.QuerySyntaxException: Product is not mapped [from Product]
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:981)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:860)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:845)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)

This is my ProductController

package com.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.model.Categories;
import com.model.Product;
import com.service.ProductService;

@Controller
public class ProductController {

@Autowired
private ProductService productService;

// Getters and Setters

public ProductService getProductService() {
    return productService;
}

public void setProductService(ProductService productService) {
    this.productService = productService;
}

// Request Mapping

@RequestMapping("/getAllProducts")
public ModelAndView getAllProducts() {
    List<Product> products = productService.getAllProducts();
    return new ModelAndView("productList", "products", products);
}

@RequestMapping("getProductById/{productId}")
public ModelAndView getProductById(@PathVariable(value = "productId") String productId) {
    Product product = productService.getProductById(productId);
    return new ModelAndView("productPage", "productObj", product);
}

@RequestMapping("/delete/{productId}")
public String deleteProduct(@PathVariable(value = "productId") String productId) {
    productService.deleteProduct(productId);
    return "redirect:/getAllProducts";
}

@RequestMapping(value = "/admin/product/addProduct", method = RequestMethod.GET)
public String getProductForm(Model model) {
    Product product = new Product();
    Categories category = new Categories();
    category.setCategoryId("1");
    product.setProductCategory(category);
    model.addAttribute("productFormObj", product);
    return "productForm";
}

@RequestMapping(value = "/admin/product/addProduct", method = RequestMethod.POST)
public String addProduct(@ModelAttribute(value = "productFormObj") Product product) {
    productService.addProduct(product);
    return "redirect:/getAllProducts";
}

}

This is my ProductClass

package com.model;

import java.util.Locale.Category;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name = "product")
public class Product {

@Id
@Column
@GeneratedValue(strategy = GenerationType.AUTO)
private String productId;
@Column
private String productDescription;
@Column
private String productManufacturer;
@Column
private String productName;
@Column
private double productPrice;
@Column(name="stockunit")
private String unitStock;
@ManyToOne
@JoinColumn(name="categoryId")
private Categories productCategory;

//  Getters and Setter

public String getProductId() {
    return productId;
}

public Categories getProductCategory() {
    return productCategory;
}

public String getProductDescription() {
    return productDescription;
}

public String getProductManufacturer() {
    return productManufacturer;
}

public String getProductName() {
    return productName;
}

public double getProductPrice() {
    return productPrice;
}

public String getUnitStock() {
    return unitStock;
}

public void setProductId(String productId) {
    this.productId = productId;
}

public void setProductCategory(Categories category) {
    this.productCategory = category;
}

public void setProductDescription(String productDescription) {
    this.productDescription = productDescription;
}

public void setProductManufacturer(String productManufacturer) {
    this.productManufacturer = productManufacturer;
}

public void setProductName(String productName) {
    this.productName = productName;
}

public void setProductPrice(double productPrice) {
    this.productPrice = productPrice;
}

public void setUnitStock(String unitStock) {
    this.unitStock = unitStock;
}

//Constructors
public Product(String productId, Categories productCategory, String       productDescription, String productManufacturer,
        String productName, double productPrice, String unitStock) {
    super();
    this.productId = productId;
    this.productCategory = productCategory;
    this.productDescription = productDescription;
    this.productManufacturer = productManufacturer;
    this.productName = productName;
    this.productPrice = productPrice;
    this.unitStock = unitStock;
}
public Product(){

}   
}

This is my application Context

<!-- for Entity Classes annotated Classes package -->
    <property name="packagesToScan">
        <list>
            <value>com.model.Product</value>
            <value>com.model.Categories</value>
        </list>
    </property>
</bean>

My Category Class

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name = "categories")
public class Categories {

@Id
private String categoryId;
@Column
private String Categories;
@OneToMany(mappedBy = "categories")
private List<Product> product;

//And Respective Getters and Setters

ProductList page

<tbody>
            <c:forEach items="${products}" var="prod">
            <tr>
            <td>${prod.productId}</td>
            <td>${prod.productCategory}</td>
            <td>${prod.productName}</td>
            <td>${prod.productPrice}</td>
            <td>${prod.unitStock}</td>
            <td>${prod.productDescription}</td>
            <td>${prod.productManufacturer}</td>
            <td>
            <a href="getProductById/${prod.productId}"><span   class="glyphicon glyphicon-info"></span></a>
            <a href="delete/${prod.productId}"><span class="glyphicon glyphicon-trash"></span></a>
            </td>
            </tr>
            </c:forEach>

Solution

  • After a long analyse i have found the solution for this problem

    I have modified some code in my daoImpl. this is older code.

    List<Product> products = session.createQuery("from Product").list();
    

    I have changed this to

    List<Product> products = session.createCriteria(Product.class).list();
    

    And some changes in appication Context as @v.ladynev said :

    <property name="packagesToScan">
      <list>
        <value>com.model</value>
      </list>
    </property>