【问题标题】:How to search for categories based on their products name and price using Hibernate?如何使用 Hibernate 根据产品名称和价格搜索类别?
【发布时间】: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
}

【问题讨论】:

    标签: java hibernate


    【解决方案1】:

    研究使用地图界面。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-18
      • 1970-01-01
      • 1970-01-01
      • 2019-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多