hibernate中多对一映射的缓存工作原理



我有两个实体站和公司。站与公司有多对一映射。当我试图获取基于公司的电台列表时。它总是击中数据库。下面是我的站点实体类

@Table(name = "station")
@Setter
@Getter
@Builder
@Cacheable
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Station  {
public Station() {}
@Id
@SequenceGenerator(name = "mySeqGen", sequenceName = "station_seq",allocationSize = 1)
@GeneratedValue(generator = "mySeqGen")
@Column(name = "station_id ")
private Long id;
Double latitude;
Double longitude;
@ManyToOne(fetch = FetchType.LAZY , cascade = CascadeType.ALL)
@JoinColumn(name = "company_id" , unique =true)
@Fetch(value = FetchMode.JOIN)

下面是我基于公司查找电台列表的代码

@Override
public List<StationDTO> findStations(final FindStationRequestDTO findStationRequestDTO) {
var company =  companyRepository.findById(findStationRequestDTO.getRequestCompanyId());
var stationList =  stationRepository.findByCompany(company);
return stationList.stream().map( station -> stationMapper.convertToStationDto(station , new StationDTO())).collect(Collectors.toList());

}

缓存正在为companyRepository.findById(findStationRequestDTO.getRequestCompanyId())工作;

public interface  StationRepository extends JpaRepository<Station, Long> {
@QueryHints({
@QueryHint(name = HINT_CACHEABLE, value = "true")
})
List<Station> findByCompany(final Optional<Company> company);
}

添加QueryHints使其工作

最新更新