简体   繁体   中英

How to make restrictions with hibernate criteria?

I am beginner in using Hibernate and Criteria. I'm trying to make a condition to get information from database about " ALL PRODUCTS THAT HAVE THE SAME CATEGORY REFERENCE". These are the entities that I have and the method that I'm trying to create in DAO.

PRODUCT

@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;
}

}

FIND ALL PRODUCTS BY 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;
   }

What should I put in the first parameter of Restrictions to get what I want?

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>

Here is the code that concerns ProductService

   public List<Product> findAllByCat(Integer id) {
            productDao.openCurrentSession();
            List<Product> products = productDao.findAllByCat(id);
            productDao.closeCurrentSession();
            return products;
        }

First you need to create an alias for product as well as the category association. Then add the necessary restriction on category alias.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM