如何使用hibernate标准进行限制?
我是使用Hibernate和Criteria的初学者。我试图设定一个条件,从数据库中获取关于“所有具有相同类别参考的产品”的信息。这些是我拥有的实体和我试图在DAO中创建的方法 产品如何使用hibernate标准进行限制?,hibernate,jakarta-ee,hibernate-criteria,Hibernate,Jakarta Ee,Hibernate Criteria,我是使用Hibernate和Criteria的初学者。我试图设定一个条件,从数据库中获取关于“所有具有相同类别参考的产品”的信息。这些是我拥有的实体和我试图在DAO中创建的方法 产品 @Entity public class Product { @Id @GeneratedValue(strategy=GenerationType.AUTO) private Integer ref_Product; private String name_Product; private float pri
@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;
}
@SuppressWarnings(“未选中”)
公共列表findAllByCat(整数id){
Criteria=getCurrentSession().createCriteria(Product.class);
标准。添加(限制。等式(“???????”,id));
List=(List)条件。List();
退货清单;
}
我应该在限制的第一个参数中输入什么才能得到我想要的
测试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;
}
公共列表findAllByCat(整数id){
productDao.openCurrentSession();
列表产品=productDao.findAllByCat(id);
productDao.closeCurrentSession();
退货产品;
}
首先,您需要为产品以及类别关联创建别名。然后对类别别名添加必要的限制
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();
Criteria=getCurrentSession().createCriteria(Product.class,“产品”);
标准createAlias(“产品类别”、“类别”);
标准.添加(限制条件eq(“类别参考类别”,id));
//下一行确保只检索不同的产品,而不检索重复的产品。
//在OneToMany配置为“急切”或FETCH为“加入”的情况下非常有用。
//根据您的配置,您可能需要,也可能不需要。
criteria.setResultTransformer(criteria.DISTINCT\u ROOT\u实体);
List=(List)条件。List();
请将代码复制并粘贴到问题中,而不是添加图像。有两个原因:1)到外部网站的链接最终会过期,使问题对其他人毫无用处;2)如果人们可以在必要时将代码片段复制到答案中,那么帮助就会容易得多。@KLibby我把代码放进去了;)使用ORM框架,标记[JavaEE]
仍然使用不利于维护的脚本。有什么原因吗?