Hql, условие по объединенной коллекции - PullRequest
0 голосов
/ 07 апреля 2019

У меня есть следующие таблицы

пул (id), монета (id), pool_to_coin (pool_id, coin_id, visible)

и их сопоставления java

    @Entity
    @Table(name = "pools")
    @Data
    public class Pool extends Identifiable {

        @ManyToMany(fetch = FetchType.LAZY)
        @JoinTable(name = "pools_coins",
                joinColumns = {@JoinColumn(name = "pool_id")},
                inverseJoinColumns = {@JoinColumn(name = "coin_id")})
        private List<Coin> coins;

    }


@Entity
@Table(name = "coins")
public class Coin extends Identifiable {

    private String name;
}

@Entity
@Table(name = "pools_coins")
@Data
public class PoolToCoin {
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "pool_id", updatable = false, insertable = false)
    private Pool pool;

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "coin_id", insertable = false, updatable = false)
    private Coin coin;

    @Column(name = "visibility")
    private boolean visibility;

    @EmbeddedId//as I don't have pk in pools_coins table , I use embedded id
    @AttributeOverrides({
            @AttributeOverride(name = "poolId", column = @Column(name = "pool_id")),
            @AttributeOverride(name = "coinId", column = @Column(name = "coin_id"))
    })
    private PoolToCoinId id;


}

Я хочу получить Пул со связанными монетами, используя следующий метод данных Spring

public interface PoolRepository extends JpaRepository<Pool, Long

    @Query(value = "SELECT p FROM Pool p LEFT JOIN FETCH p.coins coin WHERE p.id = :id")
    Pool findPoolsWithCoins(@Param("id") long id);
}

И он работает нормально, но теперь я хочу объединить только те монеты, которые имеют видимость, равную trueв pools_coins таблица

Это мой собственный запрос:

select p.id , c.id from pools p inner join pools_coins pc on p.id = pc.pool_id inner join 
coins c on c.id = pc.coin_id where pc.visibility = true

Как преобразовать его в HQL

PS Iиспользуйте Postgres

РЕДАКТИРОВАТЬ:

Это то, что я пытался

@Query(value = "SELECT distinct p " +
            "FROM PoolToCoin pc " +
            "LEFT JOIN pc.pool p " +
            "LEFT JOIN fetch p.coins c " +
            "WHERE p.id = :id " +
            "AND pc.visibility = true and p.visible = true")
    Pool findPoolsWithVisibleCoins(@Param("id") long id);

И это шоу журнала SQL

select
        distinct pool1_.id as id1_5_0_,
        coin3_.id as id1_1_1_,
        pool1_.description as descript2_5_0_,
        pool1_.logo as logo3_5_0_,
        pool1_.name as name4_5_0_,
        pool1_.picked as picked5_5_0_,
        pool1_.visible as visible6_5_0_,
        pool1_.web_site_url as web_site7_5_0_,
        coin3_.ann_url as ann_url2_1_1_,
        coin3_.collateral_price as collater3_1_1_,
        coin3_.current_local_release as current_4_1_1_,
        coin3_.discord_url as discord_5_1_1_,
        coin3_.explorer_params_class_name as explorer6_1_1_,
        coin3_.explorer_params_password as explorer7_1_1_,
        coin3_.explorer_params_url as explorer8_1_1_,
        coin3_.explorer_params_username as explorer9_1_1_,
        coin3_.explorer_url as explore10_1_1_,
        coin3_.first_pos_block as first_p11_1_1_,
        coin3_.genesis_block_time as genesis12_1_1_,
        coin3_.indexed as indexed13_1_1_,
        coin3_.logo_url as logo_ur14_1_1_,
        coin3_.name as name15_1_1_,
        coin3_.source_url as source_16_1_1_,
        coin3_.ticker as ticker17_1_1_,
        coin3_.twitter_url as twitter18_1_1_,
        coin3_.url as url19_1_1_,
        coin3_.visible as visible20_1_1_,
        coins2_.pool_id as pool_id2_6_0__,
        coins2_.coin_id as coin_id1_6_0__ 
    from
        pools_coins pooltocoin0_ 
    left outer join
        pools pool1_ 
            on pooltocoin0_.pool_id=pool1_.id 
    left outer join
        pools_coins coins2_ 
            on pool1_.id=coins2_.pool_id 
    left outer join
        coins coin3_ 
            on coins2_.coin_id=coin3_.id 
    where
        pool1_.id=? 
        and pooltocoin0_.visibility=true 
        and pool1_.visible=true

Мне нужен именно этотбу без объединения на coins2_

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...