【问题标题】:How to make restrictions with hibernate criteria?如何使用休眠标准进行限制?
【发布时间】:2016-04-01 00:57:36
【问题描述】:

我是使用 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;
        }

【问题讨论】:

  • 请将代码复制并粘贴到问题中,而不是添加图像。有两个原因 1) 指向外部网站的链接最终会过期,从而使问题对其他人毫无用处,以及 2) 如果人们可以在必要时将您的代码的 sn-ps 复制到答案中,那么提供帮助会容易得多。
  • @KLibby 我放了代码 ;)
  • 使用 ORM 框架,标记 [java-ee] 仍然使用维护不友好的 Scriptlet。有什么原因吗?

标签: hibernate jakarta-ee hibernate-criteria


【解决方案1】:

首先,您需要为产品以及类别关联创建别名。然后对类别别名添加必要的限制。

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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-16
    • 2011-05-07
    • 1970-01-01
    • 2011-10-23
    • 1970-01-01
    • 2010-11-26
    • 1970-01-01
    • 2011-10-09
    相关资源
    最近更新 更多