【问题标题】:Query for OneToOne mapping is taking more than 8 seconds by using ms sql server使用 ms sql server 查询 OneToOne 映射需要 8 秒以上
【发布时间】:2019-11-30 07:25:06
【问题描述】:

我正在使用 MS SQL 服务器和 jpa 存储库。

我想加入 2 个表并获取前 20 列,我在 Entity 类中使​​用了 Annotation。

我正在使用 Jpa Repository 的 findAll(Pageable),它生成了 10 次查询并花费了 10 秒,这是巨大的。

如果我在 Db 中搜索相同的内容,则需要 277 毫秒。

@Getter
@Setter
@ToString
@Entity
@Table(name = "TVSource")
public class MyTelevisionSource {
    @Id
    private Long SourceId;

    @Column(columnDefinition = "nvarchar2 (2000)")
    private String LongName;

    @Column(columnDefinition = "nvarchar2 (2000)")
    private String DisplayName;

    @OneToOne(fetch=FetchType.LAZY)
    @JoinColumn(name = "SourceId")
    private RCMSource rcmSource;  

}


@Getter
@Setter
@ToString
@Entity
@Table(name = "Source")
public class RCMSource {

    @Id
    private Long SourceId;

    @Column(columnDefinition = "nvarchar2 (2000)")
    private String SourceName;

}


@Service
public class TelevisionSourceService {

    @Autowired
    private TelevisionSourceRepository televisionSourceRepo;

    public List<MyTelevisionSource> getTelevisionSource(){

        Pageable pageable = PageRequest.of(0, 10);
        Page<MyTelevisionSource> tvSource =  televisionSourceRepo.findAll(pageable);

        System.out.println(tvSource.getContent());

        return tvSource.getContent();
}


public interface TelevisionSourceRepository extends JpaRepository<MyTelevisionSource, Long> {

    Page<MyTelevisionSource> findAll(Pageable pageable);

}

我正在使用如下自定义查询

@Query("select s.sourceid, s.sourceName from Source s inner join s.TelevisionSource t where t.sourceid = :sourceid")

Page<MyTelevisionSource> findAll(Pageable pageable);

但它给出错误,说

原因:org.hibernate.hql.internal.ast.QuerySyntaxException: Source is not mapped [SELECT s.sourceid, s.sourceName, t.TvsourceLongName FROM Source as s INNER JOIN TelevisionSource as t ON s.sourceid = t .sourceid]

不知道...为什么要给予

【问题讨论】:

    标签: sql hibernate jpa spring-data-jpa


    【解决方案1】:

    你真的需要懒惰(你的 OneToOne 注释的FetchType.LAZY)吗?因此,您有 1 个检索电视源的主要查询,然后有 10 个检索 RCMsources 的查询。 如果你设置FetchType.EAGER 它应该生成一个带有左连接的查询

    【讨论】:

    • 我也试过下面的,@OneToOne(fetch=FetchType.EAGER) @JoinColumn(name = "SourceId") private RCMSource rcmSource;但没有变化......现在它也产生了 11 个查询,如你所说。我错过了什么吗?
    • 我正在使用 MS SQL 服务器
    • 我可能忘记了 stg:在 rcmSource 字段上添加 @Fetch(FetchMode.JOIN) 应该强制加入
    • 我正在使用如下自定义查询 @Query("select s.sourceid, s.sourceName from Source s inner join s.TelevisionSource t where t.sourceid = :sourceid") Page findAll (可分页可分页);但它给出错误,说引起:org.hibernate.hql.internal.ast.QuerySyntaxException:源未映射[SELECT s.sourceid,s.sourceName,t.TvsourceLongName FROM Source as s INNER JOIN TelevisionSource as t ON s。 sourceid = t.sourceid] 不知道...为什么要给出
    • 您在这里混合了不同的问题。除了 pageable 之外没有其他参数的 findAll() 方法如何使用 :sourceid 参数执行查询???在 HQL/查询中,您必须使用实体名称,而不是表名称,因此 RCMSource 而不是 Source
    猜你喜欢
    • 2014-11-28
    • 1970-01-01
    • 2014-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-04
    • 2015-04-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多