java如何使用Hibernate根据产品名称和价格搜索类别?
我有一个名为Category的实体和另一个名为Product的实体。每个类别可以有多个产品。我有一个搜索表格来搜索类别。表单有三个字段:类别、产品和价格。如果用户输入类别名称,则显示该类别如果选择了产品名称,则将显示该产品的类别,如果选择了价格,则将显示其产品具有所选价格的类别
我想我应该有两个标准声明,一个是基于类别名称查找搜索,另一个是查找名称或价格与标准匹配的产品类别,但我想知道是否还有其他更有效的方法
if(search field category name is provided)
search for all categories that their name is matched with the provided text
add the found categories to the list of results
if(search fields product name and/or price are provided)
search for category of matched products and matched category name if the category name is provided
add the found categories to the list of results
return the results list
实体
@Entity
public class Category {
@Id
@GeneratedValue
private long id;
private long name;
getters and setters
}
@Entity
public class Product {
@Id
@GeneratedValue
private long id;
private String name;
private float price;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "category")
private Category category;
getters and setters
}
# 1 楼答案
研究如何使用地图界面
http://docs.oracle.com/javase/7/docs/api/java/util/Map.html
它允许您设置键和值的映射。您可以将某些产品指定给某些关键值。我不知道这是否回答了你的问题,但这是一个想法