【发布时间】:2014-07-18 10:14:36
【问题描述】:
我有一个名为 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
}
【问题讨论】: