0

I have a class which tries to get a record from the db . I see that the Query in the repository is annotated with @Transactional(readOnly = true) When I try to do personRepository.findPersonWithName(); I get 0 records but get a record when I use findByOccupationName. Note that when I run the query on the db I do records with the given person name and When I remove the @Transactional(readOnly = true) the query returns records . Can someone pls help me understand why this is happening ?

@Component
@RequiredArgsConstructor
public class Person {
  @Autowired private Ops op;
  private final PersonRepository personRepository;

  @SneakyThrows
  public Object doTest() {
    personRepository.findPersonWithName();
  }
}

@Repository
public interface PersonRepository {
  @Transactional(readOnly = true)
  @Query(
      value = "SELECT e from #{#entityName} e where  e.name = ?1 )
  Optional<Object> findPersonWithName(String name);


 @Query("SELECT e from #{#entityName} e where e.Occupation = ?1)
  Optional<DataSourceEntity> findByOccupationName(String occupationName);
}
dojacat
  • 27
  • 8
  • https://stackoverflow.com/questions/1614139/spring-transactional-read-only-propagation – S. Anushan Jul 12 '21 at 02:24
  • R as above link. – S. Anushan Jul 12 '21 at 02:25
  • I dint understand how this is related to the issue i am facing , could you pls explain ? – dojacat Jul 12 '21 at 02:34
  • 1
    @dojacat frankly speaking readonly=true should not have anything with the issue you are facing. So you need to debug and check why and what is happening with it. Enable hibernate logs and check what query it is passing to the DB – Lucia Jul 12 '21 at 04:58

0 Answers0