Repository
定义一个根据类型查询考题的接口
@Repositorypublic interface ExamQuestionDao extends JpaRepository<ExamQuestion, Integer> {/*** 根据考题类型查询考题** @param questionType {@link com.lixin.newworld.domain.enums.QuestionType} 考题类型* @return {@link List<ExamQuestion>} 考题*/Stream<ExamQuestion> findAllByType(QuestionType questionType);}
Client
简单写了一个单元测试来调用
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)public class ExamQuestionDaoTests {@Autowiredprivate ExamQuestionDao examQuestionDao;@Testpublic void testJPA() {long count = examQuestionDao.findAllByType(QuestionType.JUDGE).filter(q -> q.getType() != QuestionType.JUDGE).count();assert count == 0;}}
Error
Duang,翻车了!抛出以下异常👇
org.springframework.dao.InvalidDataAccessApiUsageException: You’re trying to execute a streaming query method without a surrounding transaction that keeps the connection open so that the Stream can actually be consumed. Make sure the code consuming the stream uses @Transactional or any other way of declaring a (read-only) transaction.
报错原因是:为了保持 Stream 连接打开,必须保证消费 Stream 的代码处于事务控制!
Fix
根据报错的提示,需要在消费 Stream 代码处添加 @Transactional 注解即可解决上述报错!最终无误代码如下
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)public class ExamQuestionDaoTests {@Autowiredprivate ExamQuestionDao examQuestionDao;@Test@Transactional(readOnly = true)public void testJPA() {long count = examQuestionDao.findAllByType(QuestionType.JUDGE).filter(q -> q.getType() != QuestionType.JUDGE).count();assert count == 0;}}
再执行一次,测试通过,可以开始愉快的玩耍 Stream API 了!
