Estoy trabajando en un proyecto con Spring Web MVC
y Hibernate
, () en el cual quiero obtener una lista de productos por categoría.
Products.java
package model.pojo;
public class Products implements java.io.Serializable {
private Integer prodId;
private byte[] prodPhoto;
private String prodName;
private int categoryId;
private int prodTypeId;
private int brandId;
private float price;
public Products() {
}
public Products(byte[] prodPhoto, String prodName, int prodTypeId, int brandId, float price) {
this.prodPhoto = prodPhoto;
this.prodName = prodName;
this.prodTypeId = prodTypeId;
this.brandId = brandId;
this.price = price;
}
public Integer getProdId() {
return this.prodId;
}
public void setProdId(Integer prodId) {
this.prodId = prodId;
}
public byte[] getProdPhoto() {
return this.prodPhoto;
}
public void setProdPhoto(byte[] prodPhoto) {
this.prodPhoto = prodPhoto;
}
public String getProdName() {
return this.prodName;
}
public void setProdName(String prodName) {
this.prodName = prodName;
}
public int getCategoryId() {
return categoryId;
}
public void setCategoryId(int categoryId) {
this.categoryId = categoryId;
}
public int getProdTypeId() {
return this.prodTypeId;
}
public void setProdTypeId(int prodTypeId) {
this.prodTypeId = prodTypeId;
}
public int getBrandId() {
return this.brandId;
}
public void setBrandId(int brandId) {
this.brandId = brandId;
}
public float getPrice() {
return this.price;
}
public void setPrice(float price) {
this.price = price;
}
}
Products.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated Apr 25, 2019 1:18:18 AM by Hibernate Tools 4.3.1 -->
<hibernate-mapping>
<class name="model.pojo.Products" table="products" catalog="houseofbeauty" optimistic-lock="version">
<id name="prodId" type="java.lang.Integer">
<column name="prod_id" />
<generator class="identity" />
</id>
<property name="prodPhoto" type="binary">
<column name="prod_photo" not-null="true" />
</property>
<property name="prodName" type="string">
<column name="prod_name" length="100" not-null="true" />
</property>
<property name="categoryId" type="int">
<column name="category_id" />
</property>
<property name="prodTypeId" type="int">
<column name="prod_type_id" not-null="true" />
</property>
<property name="brandId" type="int">
<column name="brand_id" not-null="true" />
</property>
<property name="price" type="float">
<column name="price" precision="12" scale="0" not-null="true" />
</property>
</class>
</hibernate-mapping>
ProductDao.java
package dao;
import java.util.List;
import model.pojo.Products;
import model.util.HibernateUtil;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
public class ProductDao {
public static List<Products> ListProductByCat(int category_id) {
List<Products> products = null;
try {
Session session = HibernateUtil.getSessionFactory().openSession();
Query query = session.createQuery("select prod_id, prod_name, price from products where category_id = " + category_id);
products = query.list();
session.close();
}
catch (HibernateException ex) {
ex.printStackTrace();
}
return products;
}
}
CatalogController.java
package controller;
import dao.CategoryDao;
import dao.ProductDao;
import dao.ProductTypeDao;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import model.pojo.Categories;
import model.pojo.ProductTypes;
import model.pojo.Products;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class MenuController {
ModelAndView mav = new ModelAndView();
@RequestMapping(value="/catalog/{category}", method = RequestMethod.GET)
public ModelAndView ListProductByCat(HttpServletRequest request) {
int id = Integer.parseInt(request.getParameter("id"));
List<Products> products = ProductDao.ListProductByCat(id);
mav.addObject("products", products);
mav.setViewName("catalog");
return mav;
}
}
catalog.jsp
...
<div class="row catalog">
<c:forEach items="${products}" var="product">
<div class="col-md-3 col-sm-4 col">
<div class="product-block">
<div class="product-item">
<a href="#"><img src="${pageContext.request.contextPath}/displayImages?id=<c:out value="${product.prod_id}"/>" class="img-responsive"/></a>
<a href="#"><p><c:out value="${product.prod_name}"/></p></a>
<p class="price"><c:out value="${product.price}"/> руб.</p>
</div>
<div class="product-item-more">
<button><i class="fa fa-shopping-cart"></i>Купить</button>
</div>
</div>
</div>
</c:forEach>
</div>
....
Haciendo de ésta manera obtengo este error:
Severe: org.hibernate.hql.internal.ast.QuerySyntaxException: products is not mapped [select prod_id, prod_name, price from products where category_id = 1]
at org.hibernate.hql.internal.ast.QuerySyntaxException.generateQueryException(QuerySyntaxException.java:96)
at org.hibernate.QueryException.wrapWithQueryString(QueryException.java:120)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:234)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:158)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:126)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:88)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:190)
at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:301)
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236)
at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1796)
at dao.ProductDao.ListProductByCat(ProductDao.java:32)
at controller.MenuController.ListProductByCat(MenuController.java:27)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:214)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:690)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:344)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
at org.glassfish.tyrus.servlet.TyrusServletFilter.doFilter(TyrusServletFilter.java:305)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:316)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:416)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:283)
at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:283)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:536)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:591)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571)
at java.lang.Thread.run(Thread.java:748)
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: products is not mapped
at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:189)
at org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:109)
at org.hibernate.hql.internal.ast.tree.FromClause.addFromElement(FromClause.java:95)
at org.hibernate.hql.internal.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:331)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3633)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3522)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:706)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:562)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:299)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:247)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:278)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:206)
... 59 more
¿Cómo puedo imprimir la lista de productos obteniendo el valor de category_id?
Muchas gracias de antemano.