Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

408
Visualizações
Spring Data JPA repository methods overloading

For example, I have a book JpaRepository. Book has a field called Name, the book repository has a method findOneByName (as the jpa repository method naming convention). But I need two different versions of findOneByName to use in different use cases. One version is lock annotated, the other is lock-free. Like this:

public interface BookRepository extends JpaRepository<BookDAO, Long> {

   @Lock(LockModeType.READ)
   BookDAO findOneByName( String name );

   BookDAO findOneByName( String name );
}

Is it possible to achieve this in Spring? If so, how to distinguish the two methods when calling them. If not, is there another way to do it while still using the Spring JPA repository interfaces (like findOneBy***).

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

According to reference we can name query methods with these prefixes: find…By, read…By, query…By, count…By, and get…By.

So methods BookDAO findByName(String name) and BookDAO getByName(String name) will do the same thing.

about 4 years ago · Santiago Trujillo Relatório

0

I dont know if it can be done your way. But i would create different methods

 public interface BookRepository extends JpaRepository<BookDAO, Long> {

   @Lock(LockModeType.READ)
   @Query("select b from Book b where b.name = :name")
   BookDAO findOneByNameForRead( String name );

   BookDAO findOneByName( String name );

}

or you can create methods in your service layer instead of using spring jparepository to handle locking. and use it across where it is needed to be updated, and all read methods marked as @Transactional(readOnly = true)

@PersistenceContext
private EntityManager em;
...

public  Book findOneBookForUpdate(String id) {

    Book book = em.find(Book.class, id);
    if (book != null) {
        em.lock(book, LockModeType.PESSIMISTIC_WRITE);
    }

    return book;
}
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda