简体   繁体   English

如何使用 HQL 将 hibernate 中的所有行 select 与额外表一起使用

[英]How to select all rows in hibernate with extra table with the use of HQL

Update 2更新 2

Query:询问:

select distinct p.name, u.cookie_id
    from product p
    left join user_product up on p.id = up.product_id
    left join user u on up.user_id = u.id
    where p.active = true
    and
    (
       u.cookie_id = '4c2fe5b2-73fe-4b28-baa6-23db0512114c'
           or
       not (exists (
         select p1.id
         from user_product up1, product p1
         where p1.id = up1.product_id
       ))
    )

Output: Output: 在此处输入图像描述

how should be:应该如何: 在此处输入图像描述

Update 1更新 1

I wrote the code with the use of stream() for the better understanding of the problem:为了更好地理解问题,我使用stream()编写了代码:

        List<Product> productList = productRepository.findAllByActiveTrue();
        productList = productList.stream().map(item -> {
            if(item.getUserProducts() == null) return item;

            List<UserProduct> userProductList = new ArrayList<>();
            for (UserProduct userProduct : item.getUserProducts()) {
                if(userProduct.getUser().getCookieId().equals(cookieId)){
                    userProductList.add(userProduct);
                }
            }
            item.setUserProducts(userProductList);
            return item;
        }).collect(Collectors.toList());

I would like to know how correctly select data from the tables.我想知道表格中 select 数据的正确性。 I want to get all products.我想获得所有产品。

But to get all products is easy.但是要获得所有产品很容易。 I also need to include in the output data (UserProducts and User) if there's a connection between Product and User by CookieId.如果产品和用户之间通过 CookieId 建立连接,我还需要包含在 output 数据(用户产品和用户)中。 In other words, I want to show all products with User and UserProducts (if possible) and exclude the relation Product-UserProduct-User if User's cookieId doesn't match the cookieId from the query.换句话说,如果用户的 cookieId 与查询中的 cookieId 不匹配,我想显示所有具有 User 和 UserProducts 的产品(如果可能)并排除关系 Product-UserProduct-User 。

I am trying the following query, but it returns me only products that has the connection between User and the product, not all products.我正在尝试以下查询,但它只返回用户与产品之间有联系的产品,而不是所有产品。

@Query("from Product pr join fetch pr.userProducts up left join fetch up.user u where pr.active = true and u.cookieId = :cookieId")
    List<Product> getAllProductsByCookieId(UUID cookieId);

My database looks like this:我的数据库如下所示: 数据库

Visualisation of the idea:想法的可视化: 在此处输入图像描述

SQL-queries to generate tables:用于生成表的 SQL 查询:

product-table产品表

create table if not exists product
(
    id bigint auto_increment
        primary key,
    active bit not null,
    image_path varchar(255) null,
    name varchar(255) null,
    price double not null,
    unit_number double null,
    unit_type varchar(255) null
);

userProduct-table用户产品表

create table if not exists user_product
(
    quantity int null,
    product_id bigint not null,
    user_id bigint not null,
    primary key (product_id, user_id),
    constraint FKnw43wab2rt35jmofmpbhkibco
        foreign key (product_id) references product (id),
    constraint FKq5o2e33vlwpfc2k1mredtia6p
        foreign key (user_id) references user (id)
);

user-table用户表

create table if not exists user
(
    id bigint auto_increment
        primary key,
    cookie_id varchar(255) null
);
  1. Your requirement is not possible without scaler objects in JPA.如果没有 JPA 中的缩放器对象,您的要求是不可能的。 ie JPA cannot give you a Product object where p.getUserProducts() contains only some UserProducts .即 JPA 不能给你一个Product object 其中p.getUserProducts()只包含一些UserProducts

  2. You have to use the native query option or any other option where you retrieve the columns and provide a mapper as to how to create the object.您必须使用本机查询选项或任何其他选项来检索列并提供有关如何创建 object 的映射器。 You can use the following sql query.您可以使用以下 sql 查询。

    select p.product, u.cookie_id 
    from product p 
    left join user_product up on p.id = up.product_id 
    left join user u on up.user_id = u.id and u.cookie_id = '?1'
    where p.active = true
    group by p.product, u.cookie_id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM