The difference between JPA findById method and getOne method

The difference between JPA findById method and getOne method

Jpa-based CRUD methods are inherited from the interface CrudRepository<T, ID>, and include the following methods:

<S extends T> S save(S entity);
<S extends T> Iterable<S> saveAll(Iterable<S> entities);
Optional<T> findById(ID id);
boolean existsById(ID id);
Iterable<T> findAll();
Iterable<T> findAllById(Iterable<ID> ids);
long count();
void deleteById(ID id);
void delete(T entity);
void deleteAll(Iterable<? extends T> entities);
void deleteAll();

The getOne() method is defined in the JpaRepository interface, and the source code is as follows:

/**
 * Returns a reference to the entity with the given identifier. Depending on how the JPA persistence provider is
 * implemented this is very likely to always return an instance and throw an
 * {@link javax.persistence.EntityNotFoundException} on first access. Some of them will reject invalid identifiers
 * immediately.
 *
 * @param id must not be {@literal null}.
 * @return a reference to the entity with the given identifier.
 * @see EntityManager#getReference(Class, Object) for details on when an exception is thrown.
 */
T getOne(ID id);

Source code found online:

public T getOne(ID id) {
    
    
    Assert.notNull(id, "The given id must not be null!");
    return this.em.getReference(this.getDomainClass(), id);
}

As can be seen from the source code of the getOne method, the getOne method will return a reference to an entity that matches the given id. The method is actually to call the getReference method, which means that the loading strategy is delayed loading, and it is not really performed from the database until the object is called. Query (x_x).

Look at the findById() method:

public Optional<T> findById(ID id) {
    
    
    Assert.notNull(id, "The given id must not be null!");
    Class<T> domainType = this.getDomainClass();
    if (this.metadata == null) {
    
    
        return Optional.ofNullable(this.em.find(domainType, id));
    } else {
    
    
        LockModeType type = this.metadata.getLockModeType();
        Map<String, Object> hints = this.getQueryHints().withFetchGraphs(this.em).asMap();
        return Optional.ofNullable(type == null ? this.em.find(domainType, id, hints) : this.em.find(domainType, id, type, hints));
    }
}

findById actually calls the find method, and it uses immediate loading. When this query is executed, it will be queried from the database immediately, but the findById method returns an Optional, and you need to call the .get() method on this Optional. Get the entities you need.

  • The summary is that the getOne method is lazy loading, and will not query the database until the entity returned by it is called. FindById is loaded immediately, and the main calling method will go to the database to query. The getOne method returns an entity directly, and the findById method returns an Optional. You need to call the .get() method to get the entity.

Guess you like

Origin blog.csdn.net/qq_42026590/article/details/112364798