[英]Data fetching using Spring Data JPA Stream
下面是我的 controller
public class TestController {
@PersistenceContext
EntityManager entityManager;
@Autowired
ProductAltRepository productAltRepository;
@GetMapping("/findAll")
@Transactional(readOnly = true)
public void findAll() {
Stream<ProductAltRelEntity> productAltRelEntities = productAltRepository.findAllProductAlts();
List<ProductAltRelEntity> productAlts = Lists.newArrayList();
productAltRelEntities.forEach(x -> {
productAlts.add(x);
entityManager.detach(x);
});
}
这是存储库
@Repository
@Transactional
public interface ProductAltRepository
extends JpaRepository<ProductAltRelEntity, Long>, JpaSpecificationExecutor<ProductAltRelEntity>{
@QueryHints(value = { @QueryHint(name = HINT_FETCH_SIZE, value = "" + Integer.MIN_VALUE),
@QueryHint(name = HINT_CACHEABLE, value = "false"), @QueryHint(name = HINT_READONLY, value = "true"), })
@Query("SELECT p FROM ProductAltRelEntity p")
public Stream<ProductAltRelEntity> findAllProductAlts();
}
要求是findAll(),分页给OOM,所以我想用Stream,但是得到异常。
[WARN] org.hibernate.engine.jdbc.spi.SqlExceptionHelper - SQL Error: 17068, SQLState: 99999
2019-11-15 12:30:31.468 错误 7484 --- [nio-8082-exec-1] ohengine.jdbc.spi.SqlExceptionHelper:调用中的参数无效:setFetchSize [ERROR] org.ZCB1F0208EEBF5074C6EF1ZE9 jdbc.spi.SqlExceptionHelper - Invalid argument(s) in call: setFetchSize 2019-11-15 12:30:31.793 ERROR 7484 --- [nio-8082-exec-1] c.seddeGlobalExceptionHandler: Unhandled Exception: org.springframework. orm.jpa.JpaSystemException:无法使用滚动执行查询; nested exception is org.hibernate.exception.GenericJDBCException: could not execute query using scroll Caused by: org.hibernate.exception.GenericJDBCException: could not execute query using scroll Caused by: java.sql.SQLException: Invalid argument(s) in call :setFetchSize
查看您在 Medium 中的源文章。
我认为文章中给出的方法与您的方法有很大不同。 您应该首先打开Stateless Session 。 我不认为使用 spring-data 中提供的findAll()
会使用它,因为它会从数据库中获取所有记录。
修复这将是一个好的开始。
BUG 可能的根本原因:
我看到您正在使用@QueryHints
您的HINT_FETCH_SIZE不正确。 它不能与 Integer.MIN_VALUE 一起使用,因为值等于 -2^31,这是一个负值。
阅读错误日志,您需要将流式代码放在 try 块中,即
try(Stream<ProductAltRelEntity> productAltRelEntities = productAltRepository.findAllProductAlts()){
List<ProductAltRelEntity> productAlts = Lists.newArrayList();
productAltRelEntities.forEach(x -> {
productAlts.add(x);
entityManager.detach(x);
});
} catch(Exception ex) {ex.printStackTrace();}
我注意到的另一件事是您不需要将 @Transactional 放入 ProductAltRepository 中,因为您已经将它放入了 TestController 中。
为了stream的结果我们需要满足三个条件:
Forward-only resultset
Read-only statement
Fetch-size set to Integer.MIN_VALUE
Forward-only 似乎已经由 Spring Data 设置,所以我们不必对此做任何特别的事情。 您的代码示例已经在 TestController 中有 @Transactional(readOnly = true) 注释,足以满足第二个条件,因此 @QueryHint(name = HINT_READONLY, value = "true") 没有用。 fetch-size 似乎很有用。
以下是我的一个项目的摘录:
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import static org.hibernate.jpa.QueryHints.HINT_FETCH_SIZE;
public interface IssueRepo extends JpaRepository<Issue, Long>, JpaSpecificationExecutor<Issue> {
@QueryHints(value = @QueryHint(name = HINT_FETCH_SIZE, value = "" + Integer.MIN_VALUE))
@Query("select t from Issue t ")
Stream<Issue> getAll();
}
Controller
public class ManageIssue {
@GetMapping("all/issues")
@Transactional(readOnly = true)
public String getAll() {
System.out.println("Processing Streams ...");
try(Stream<Issue> issuesList = issueRepo.getAll()){
issuesList.forEach(issue -> {
System.out.println(issue.getId()+" Issue "+issue.getTitle());
});
} catch(Exception ex) {ex.printStackTrace();}
return "All";
}
}
让 JPA 帮助您进行分页,为什么要付出这么多额外的努力?
Page<ProductAltRelEntity> productAltRelEntity= productAltRepository
.findAll(PageRequest.of(page - 1, 10, Sort.by("createdon").descending()));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.