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

356
Visualizações
Spring Boot: Disable fetch of PersistentBag programatically

I have a entity in Spring Boot which looks like that:

@Entity
@Table(name = "race_class")
@Getter @Setter
public class RaceClass
{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer dbid;

    private String name;

    private String colorHexcode;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "raceClass", cascade = CascadeType.ALL)
    private List<Entry> entries;
}

In my service I already have one method, which fetches all the entries of the raceclass additionally. Which looks like that:

public List<RaceClassDto> getClassesEntries( int eventDbid )
{
    return this.raceClassRepo.findByEventDbid( eventDbid )
        .stream()
        .map( c -> addEntriesToRaceClass( eventDbid, c ) )
        .map( raceClassMapper :: mapToDto )
        .collect( Collectors.toList() );
}

The mapper looks like that:

public RaceClassDto mapToDto( RaceClass raceClass )
{
    return modelMapper.map ( raceClass, RaceClassDto.class );
}

Now, I want to have a second method, which doesn't fetch the entries automatically. But exactly here is my problem. The new method would look like this:

public List<RaceClassDto> getClasses( int eventDbid )
{
    return this.raceClassRepo.findByEventDbid( eventDbid )
            .stream()
            .map( raceClassMapper :: mapToDto )
            .collect( Collectors.toList() );
}

But the entries will be fetched nevertheless, because getEntries() will be called in the DTO-Mapper, which executes the select queries automatically and fills the entries container. How is it possible to programmatically disable the automatic synchronisation of the PersistentBag only for the getClassesEntries() method?

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

0

I think you cannot programmatically disable the relationship resolution.

A better approach will be to define a new method in your mapper, name it mapToDTOWithoutResolvingEntries, and do not resolve the relationship in that method.

For your comments, if you are using ModelMapper, in your specific use case, maybe you can skip the entries property of your DTO.

Please, try something like the following:

public RaceClassDto mapToDTOWithoutResolvingEntries( RaceClass raceClass) {
    return modelMapperWithoutResolvingEntries
      .map ( raceClass, RaceClassDto.class );
}

Where modelMapperWithoutResolvingEntries is the same modelMapper you defined before with the following mapping added:

ModelMapper modelMapperWithoutResolvingEntries = new ModelMapper();
modelMapperWithoutResolvingEntries
    .addMappings(mapper -> mapper.skip(RaceClassDto::setEntries));

You can use create a TypeMap instead, it should behave the same.

You have other alternatives. For instance, you can provide your own PropertyMap (please, see the relevant javadoc and how they overwrite the configure method), or maybe even a Converter.

over 4 years ago · Santiago Trujillo Relatório

0

I believe modelMapper is coming from the ModelMapper library, so in this case you could tell it to skip mapping the entries, as addressed in this question.

Otherwise, you can probably copy the data from the JPA loaded object into another object excluding those lazily fetched data.

over 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