I'm running into an issue where my query method is in a foreach loop, and each time I'm passing in a different parameter to retrieve different information. However, after the FIRST iteration of the loop, the query data gets cached (I think) and returns the same data for subsequent loops.
Here is my code:
@Transactional(readOnly = true) public List<InitiativeReport> getInitiativeReports() throws Exception { try { List<InitiativeReport> ir = new ArrayList<InitiativeReport>(); List<Initiative> in = initiativeRepository.findAll(); for(Initiative i : in) { i.getTheme().getId(); // lazy initialize InitiativeReport report = new InitiativeReport(); report.setId(i.getId()); report.setInitiativeId(i.getInitiativeId()); report.setName(i.getName()); report.setTheme(i.getTheme()); // this is the call to the query, which is cached after the first iteration List<InitiativeProfileQuestion> q = initiativeProfileQuestionRepository.getQuestionsAndAnswerLogs(i.getInitiativeId()); report.setQuestions(q); ir.add(report); } return ir; } catch (Exception e) { throw new Exception(e); } Here is my repository interface:
public interface InitiativeProfileQuestionRepository extends JpaRepository<InitiativeProfileQuestion, Long> { @Query("select distinct q from InitiativeProfileQuestion q " + "left join fetch q.answers " + "left join fetch q.answerLogs al " + "where al.initiative.initiativeId = ?1 " + "and al.revision = al.initiative.revision + "order by q.question asc") public List<InitiativeProfileQuestion> getQuestionsAndAnswerLogs(String initiativeId); } Here is my application.yml file:
spring: datasource: dataSourceClassName: com.mysql.jdbc.jdbc2.optional.MysqlDataSource url: jdbc:mysql://localhost/testdb username: root password: XXXXXXXXX driverClassName: com.mysql.jdbc.Driver testOnBorrow: true validationQuery: SELECT 1 jpa: database-platform: org.hibernate.dialect.MySQLInnoDBDialect database: MYSQL openInView: false show_sql: true generate-ddl: false hibernate: ddl-auto: none naming-strategy: org.hibernate.cfg.EJB3NamingStrategy The issue is very similar to a post I found here: Native Query (JPA ) not reset and return the same old result
However, that user is using EntityManager and I have no implementation for EntityManager in my application- I'm letting JPA do all the work and only have query annotations.
Any assistance would be appreciated!