在休眠查询中搜索项目(select 两个表)

search item in hibernate query (select two tables)

我的代码有问题。我有一个视图 jsp,我可以在其中查看所有项目。

当我尝试搜索姓名时。它会循环或有冗余。我不知道为什么。看起来像这样。

顺便说一句,我有两个表和一个外键 product[pid] 到 stock[pid]

public class Product {

@Id
@Column(name="p_id")
private String pid;

@Column(name="p_name")
private String p_name;


@Column(name="c_name")
private String c_name;

@Column(name="b_name")
private String b_name; 
//SETTERS & GETTERS

public class Stock {

    @Id
    @Column(name="s_id")
    private int sid;

    @Column(name="p_id")
    private String pid;

    @Column(name="s_quantity")
    private String squantity;

    @Column(name="s_price")
    private String sprice;

    @Column(name="s_cost")
    private String cost;
//SETTERS AND GETTERS

@控制器

@RequestMapping(method = RequestMethod.POST, value = "/searchItem")
public String searchItem(HttpServletRequest request, ModelMap map, 
        @RequestParam(value = "page", required = false) Integer page,
        @RequestParam(value = "size", required = false) Integer size ) {


    String searchProductName = request.getParameter("productName");
    String cat = request.getParameter("category");
    String bran = request.getParameter("brand");

    Product searchProduct = new Product();


    searchProduct.setP_name(searchProductName);
    searchProduct.setC_name(cat);
    searchProduct.setB_name(bran);

    int pageSize = (size != null && size != 0) ? size : 25;
    int firstResultIndex = (page != null && page > 0) ? (page - 1) * pageSize : 0;
    List<Product> productList = catService.getUsers(searchProduct, firstResultIndex, pageSize);
    map.addAttribute("productList", productList);

    List<Category> cList = catService.getCat();
    map.addAttribute("cList", cList);
    List<Brand> bList = catService.getBrand();
    map.addAttribute("bList", bList);

    return "new/list";

}

@DaoImpl

@SuppressWarnings("unchecked")
@Override
public List<Product> getUsers(Product searchProduct, int startPage, int maxResults) {
    EntityManager entityManager = transactionManager.getEntityManagerFactory().createEntityManager();
Session session = entityManager.unwrap(Session.class);

SQLQuery query = session.createSQLQuery("FROM product,stock");
        boolean paramExists = false;

    if (!StringUtility.isStringNullOrEmpty(searchProduct.getC_name())&&!StringUtility.isStringNullOrEmpty(searchProduct.getB_name()))  {
        //sqlQuerySB.append(" product.c_name LIKE :category AND product.b_name LIKE :brand");
     query = session.createSQLQuery("FROM product,stock WHERE product.c_name LIKE :category AND product.b_name LIKE :brand");
        paramExists = true;
    }


    if (!StringUtility.isStringNullOrEmpty(searchProduct.getP_name())) {

query = session.createSQLQuery("SELECT product.p_name,product.c_name,product.b_name,stock.s_quantity,stock.s_price,stock.s_cost FROM product,stock WHERE product.p_name LIKE :productName");

query.setParameter("productName", "%" + searchProduct.getP_name() + "%");
            paramExists = true;

    }

    if (!StringUtility.isStringNullOrEmpty(searchProduct.getC_name())) {

        query = session.createSQLQuery("SELECT product.p_name,product.c_name,product.b_name,stock.s_quantity,stock.s_price,stock.s_cost FROM product,stock WHERE product.c_name LIKE :category ");
        query.setParameter("category", "" + searchProduct.getC_name() + "");
            paramExists = true;

    }

    query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);

    List<Product> productList = query.list();

    if (entityManager.isOpen())
        entityManager.close();

    return productList;
}

也许我的 DAOimpl 中有一个关于我如何查询两个表的大问题.. 需要一些 help/advice。 ty.

您没有指定产品和库存之间的连接条件:

SELECT 
    product.p_name,
    product.c_name,
    product.b_name,
    stock.s_quantity,
    stock.s_price,
    stock.s_cost 
FROM product, stock 
WHERE product.p_name LIKE :productName

在这种情况下,它将 return 每个产品(带有名称)和库存(完整 table 因为它没有任何条件)的组合。

尝试指定加入条件:

SELECT 
    product.p_name,
    product.c_name,
    product.b_name,
    stock.s_quantity,
    stock.s_price,
    stock.s_cost 
FROM product, stock 
WHERE 
product.pid = stock.pid
product.p_name LIKE :productName