在本机查询中休眠H + 1(Spring Data)

时间:2019-01-28 05:46:26

标签: java hibernate spring-boot spring-data

我在Postgres中有三个表

create table pool(
id PK
);
create table coin(
id PK
);
create table pools_coins(
id PK,
pool_id references to pool,
coin_id references to coin
);

仅在Pools类中进行映射

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;

现在,我想使用Spring Data获取带有硬币列表的池:

@Query(
            nativeQuery = true,
            value = "SELECT p.*,c.* 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 p.id = :id ;"
    )
    Pool findPoolsWithCoins(@Param("id") long id);

我启用了sql-logging并看到了两个查询

2019-01-28 11:44:52.154 DEBUG 9262 --- [0.1-8080-exec-1] org.hibernate.SQL                        : SELECT p.*,c.* 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 p.id = ? ;
2019-01-28 11:44:52.159 TRACE 9262 --- [0.1-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [BIGINT] - [1]
2019-01-28 11:44:52.186 DEBUG 9262 --- [0.1-8080-exec-1] org.hibernate.SQL                        : select coins0_.pool_id as pool_id1_6_0_, coins0_.coin_id as coin_id2_6_0_, coin1_.id as id1_1_1_, coin1_.ann_url as ann_url2_1_1_, coin1_.collateral_price as collater3_1_1_, coin1_.current_local_release as current_4_1_1_, coin1_.discord_url as discord_5_1_1_, coin1_.explorer_params_class_name as explorer6_1_1_, coin1_.explorer_params_password as explorer7_1_1_, coin1_.explorer_params_url as explorer8_1_1_, coin1_.explorer_params_username as explorer9_1_1_, coin1_.explorer_url as explore10_1_1_, coin1_.first_pos_block as first_p11_1_1_, coin1_.genesis_block_time as genesis12_1_1_, coin1_.indexed as indexed13_1_1_, coin1_.logo_url as logo_ur14_1_1_, coin1_.name as name15_1_1_, coin1_.source_url as source_16_1_1_, coin1_.ticker as ticker17_1_1_, coin1_.twitter_url as twitter18_1_1_, coin1_.url as url19_1_1_, coin1_.visible as visible20_1_1_ from pools_coins coins0_ inner join coins coin1_ on coins0_.coin_id=coin1_.id where coins0_.pool_id=?
2019-01-28 11:44:52.186 TRACE 9262 --- [0.1-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [BIGINT] - [1]

如何修复?

0 个答案:

没有答案