如何使用休眠标准进行限制?
How to make restrictions with hibernate criteria?
我是 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;
}
//--------------------------------//
类别
@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;
}
我应该在 Restrictions 的第一个参数中输入什么才能得到我想要的?
测试JSP页面
<%@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;
}
首先您需要为产品和类别关联创建一个别名。然后在类别别名上添加必要的限制。
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();
我是 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;
}
//--------------------------------//
类别
@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;
}
我应该在 Restrictions 的第一个参数中输入什么才能得到我想要的?
测试JSP页面
<%@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;
}
首先您需要为产品和类别关联创建一个别名。然后在类别别名上添加必要的限制。
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();