Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

355
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!