0

I called CrudRepository#findById two times inside the same method, it displayed in console one select statement. why the second select statement doesn't displayed in the second method call.

here is the service code:

@Service
@RequiredArgsConstructor
public class AccountService {
    private final AccountReporitory accountReporitory;

    public void find() {
        final Account account1 = accountReporitory.findById(1L).get();
        final Account account2 = accountReporitory.findById(1L).get();
    }
}

here is the repository code:

public interface AccountReporitory extends JpaRepository<Account, Long> {
}
2

1 Answer 1

1

When you make the first call to findById(1L) the resulting entity is placed in the session's first level cache. Any succeeding calls to findById() with the same id in the same session will result in the entity manager retrieving the entity from the first level cache instead of performing a database query. This is one of the primary purposes of the first level cache.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.