Home > OS >  No converter found capable of converting from type
No converter found capable of converting from type

Time:09-22

org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [com.skywarelabs.runwayreporting.api.modules.asset.Asset] to type [com.skywarelabs.runwayreporting.api.modules.asset.AssetPageView]

I have two repository methods which do essentialy the same thing. I'm expecting them to return an object of <Page>. If I call findPageByAerodrome, this works correctly. However, If I call findPageByFilter, I get the error below. ..the reason I want to use findPageByFilter is that I wish to extend this with additional filtering parameters.

''' public interface AssetRepository extends PageCrudRepository<Asset, Long> {

@Query("SELECT a FROM Asset a WHERE "  
        "(a.aerodrome = :aerodrome)")
<T> Page<T> findPageByFilter(@Param("aerodrome") Aerodrome aerodrome,
                             Pageable pageable, Class<T> type);

<T> Page<T> findPageByAerodrome(@Param("aerodrome") Aerodrome aerodrome,
                                Pageable pageable, Class<T> type);

,,,

CodePudding user response:

Use this instead

<T> Page<T> findAll(Specification s, Pageable pageable, Class<T> type);

and pass a custom Specification where you create your equality filter like this:

<T> Page<T> findPageByFilter(@Param("aerodrome") Aerodrome aerodrome,
                             Pageable pageable, Class<T> type) {
  return findAll(
    new Specification() {
      @Override
      public Predicate toPredicate(Root<T> root, CriteriaQuery query, CriteriaBuilder cb) {
        return cb.equal(root.get("aerodrome"), aerodrome);
      }
    },
    pageable,
    type
  );
}

CodePudding user response:

Figured it out. I need to add an entity specific constructor.

''' public AssetPageView(Asset asset) { this ( asset.getId(), asset.getAssetType(), asset.getAerodrome(), asset.getName(), asset.getAreaOfOperationsLoc(), asset.getDisplayOrder() ); }

public AssetPageView(Long id, AssetType assetType, Aerodrome aerodrome, String name,
                     String areaOfOperationsLoc, Long displayOrder) {
    this.id = id;
    this.assetType = assetType.getName();
    this.aerodrome = aerodrome.getCallSign();
    this.name = name;
    this.areaOfOperationsLoc = areaOfOperationsLoc;
    this.displayOrder = displayOrder;
}

'''

  • Related