我有下表
池(id),硬币(id),池_硬币(pool_id,coin_id,可见)
及其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数据方法来获取带有关联硬币的Pool
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);
}
工作正常,但现在我只想加入 pools_coins 表中具有可见性等于true的那些硬币
这是我的本机查询:
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
我使用Postgres的PS
编辑:
这是我尝试过的
@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
我完全需要这个bu,而无需加入 coins2 _