Bonjour à tous 👋
J'essaye vainement depuis quelques jours d'utiliser un stream dans un service REST. Seulement j'ai toujours l'erreur :
J'ai bien mis annoter ma méthode en Transactional, mais ça n'a pas résolu mon problème.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2 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.] with root cause 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.
Je suis à la recherche d'une solution à mon problème.
Voici ma configuration :
Srping-boot : 2.0.0.RELEASE
Java : 1.8.0_152
Et le code de mon controller :
Et celui de mon interface de DAO :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 @Controller public class SiteController { private static final Logger log = LoggerFactory.getLogger(SiteController.class); @Autowired private SiteRepository repo; @RequestMapping("/site") @Transactional(readOnly = true) ResponseEntity<Collection<Ticket>> printTicketsOfSite(@RequestParam(value = "address") String siteAddress) { if (siteAddress == null) return new ResponseEntity<>(HttpStatus.BAD_REQUEST); if (siteAddress.isEmpty()) return new ResponseEntity<>(HttpStatus.BAD_REQUEST); Collection<Ticket> tickets = new HashSet<>(); try (Stream<Site> sites = repo.getSiteByAddressAndTickets(siteAddress)) { sites.forEach(site -> { tickets.addAll(site.getTickets()); }); } if (tickets.isEmpty()) return new ResponseEntity<>(HttpStatus.NOT_FOUND); return new ResponseEntity<>(tickets, HttpStatus.FOUND); } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2 @Query("select site from Site site left join fetch site.tickets") Stream<Site> getSiteByAddressAndTickets (String address);
Partager