我是初学者,使用Hibernate和Criteria。我正试图从数据库中获取有关“所有具有相同类别参考的产品”的信息。这些是我拥有的实体以及我尝试在DAO中创建的方法。
产品
@Entity
public class Product {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer ref_Product;
private String name_Product;
private float price;
private String description;
private Date last_Update;
//--------------------------------//
@ManyToOne(cascade=CascadeType.ALL)
private Category category;
public Category getCategory(){
return category;
}
public Integer getIdCategory(){
return category.getRef_Category();
}
public void setCategory(Category cat){
this.category=cat;
}
//--------------------------------//
CATEGORY
@Entity
public class Category {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer ref_Category;
private String name_Category;
public int getRef_Category() {
return ref_Category;
}
public void setRef_Category(int ref_Category) {
this.ref_Category = ref_Category;
}
public String getName_Category() {
return name_Category;
}
public void setName_Category(String name_Category) {
this.name_Category = name_Category;
}
public Category() {
}
public Category(String name_Category) {
super();
this.name_Category = name_Category;
}
}
按类别ID查找所有产品
@SuppressWarnings("unchecked")
public List<Product> findAllByCat(Integer id) {
Criteria criteria= getCurrentSession().createCriteria(Product.class);
criteria.add(Restrictions.eq("?????", id));
List<Product> list = (List<Product>) criteria.list();
return list;
}
我应该在限制的第一个参数中放入什么来获得我想要的东西?
TEST JSP PAGE
<%@page import="org.apache.jasper.tagplugins.jstl.core.ForEach"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="java.util.*" %>
<%@ page import="com.e_com.model.*" %>
<%@ page import="Service.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Category Page</title>
</head>
<body>
<h1 align="center">Category Page</h1>
<%
ProductService productService = new ProductService();
List<Product> listProduct= productService.findAllByCat(1);
System.out.println(listProduct);
%>
<table border="1">
<tr>
<th>Reference</th>
<th>Name</th>
<th>Description</th>
<th>Last Update</th>
<th>Price</th>
<th>Buy</th>
</tr>
<%
for(Product p:listProduct){%>
<tr>
<td><%out.println(p.getRef_Product());%></td>
<td><%out.println(p.getName_Product());%></td>
<td><%out.println(p.getDescription());%></td>
<td><%out.println(p.getLast_Update());%></td>
<td><%out.println(p.getPrice());%></td>
<td align="center"><a href="Cart?id=<%= p.getRef_Product() %>&action= ordernow">Order Now</a></td>
</tr>
<%}
%>
</table>
以下是与ProductService相关的代码
public List<Product> findAllByCat(Integer id) {
productDao.openCurrentSession();
List<Product> products = productDao.findAllByCat(id);
productDao.closeCurrentSession();
return products;
}
答案 0 :(得分:3)
首先,您需要为产品和类别关联创建别名。然后在类别别名上添加必要的限制。
Criteria criteria= getCurrentSession().createCriteria(Product.class, "product");
criteria. createAlias("product.category","category");
criteria.add(Restrictions.eq("category.ref_Category", id));
// Below line ensures only distinct products are retrieved, no duplicates.
// Useful in cases where OneToMany is configured EAGER or FETCH is JOIN.
// You may or may not need this depending on your configuration.
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
List<Product> list = (List<Product>) criteria.list();