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?