【发布时间】: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