如何使用Hibernate根据产品名称和价格搜索类别?

时间:2014-05-29 01:21:11

标签: 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 个答案:

答案 0 :(得分:0)

使用地图界面。

http://docs.oracle.com/javase/7/docs/api/java/util/Map.html

它允许您设置键和值的映射。您可以将某些产品分配给某些键值。我不知道这是否能回答你的问题,但这是一个想法。