less than 1 minute read

Use getById() if you just want to get a reference of the entity, like assigning it to a property of another entity.

Use findById() if you actually want to get the entity.

Because I tried to use getById() to retrieve an entity, but as it uses lazy loading, I got a org.hibernate.LazyInitializationException: could not initialize proxy.

fun getAccount(accountId: String): Account{
  return accountRepository.getById(accountId)
}

So switching to findById() resolves the issue immediately

fun getAccount(accountId: String): Account{
  return accountRepository.findById(accountId).orElseThrow()
}

Initially, I thought it is the same method just that getById() returns an object, but findById() returns an Optional object. But thanks to this article, now I know that getById() is only useful when access to properties of object is not required.