Analysis and Solutions for 'No converter found capable of converting from type' in Spring Data JPA

Nov 20, 2025 · Programming · 12 views · 7.8

Keywords: Spring Data JPA | Type Conversion | Projection Interfaces | @Query Annotation | Converter Configuration

Abstract: This article provides an in-depth analysis of the 'No converter found capable of converting from type' exception in Spring Data JPA, focusing on type conversion issues between entity classes and projection classes. Through comparison of different solutions including manual conversion, constructor invocation via @Query annotation, and Spring Data projection interfaces, complete code examples and best practice recommendations are provided. The article also incorporates experience with MapStruct extension libraries to supplement configuration points for type converters, helping developers thoroughly resolve such conversion exceptions.

Problem Background and Exception Analysis

During Spring Data JPA development, type conversion exceptions frequently occur, particularly when Repository method return types differ from entity types. From the provided stack trace, the exception occurs in the GenericConversionService.handleConverterNotFound method, indicating that Spring cannot find a converter from referencedata.ABDeadlineType to referencedata.DeadlineType.

Code Structure Analysis

Analyzing the provided class definitions reveals several key issues:

// Entity class definition
@Data
@Entity
@Table(name = "deadline_type")
@AllArgsConstructor
@NoArgsConstructor
public class ABDeadlineType {
    private @Id String id;
    private String code;
}

// Projection class definition
@Data
public class DeadlineType extends DefaultIdAndText {
    @Value("#{target.id}")
    String id;

    @Value("#{target.code}")
    String text;
}

// Repository interface definition
public interface DeadlineTypeRepository extends JpaRepository<ABDeadlineType, Long> {
    List<DeadlineType> findAllSummarizedBy();
}

The main issue here is that the Repository method declares a return type of List<DeadlineType>, but the actual entity type is ABDeadlineType. Spring Data expects to automatically perform this conversion but lacks the appropriate converter configuration.

Solution One: Return Entity Type and Manual Conversion

The simplest solution is to modify the Repository method to directly return the entity type, then perform manual conversion in the service layer:

public interface ABDeadlineTypeRepository extends JpaRepository<ABDeadlineType, Long> {
    List<ABDeadlineType> findAllSummarizedBy();
}

// Service layer conversion
@Service
public class DeadlineTypeService {
    
    @Autowired
    private ABDeadlineTypeRepository repository;
    
    public List<DeadlineType> findAllSummarized() {
        List<ABDeadlineType> entities = repository.findAllSummarizedBy();
        return entities.stream()
            .map(this::convertToDeadlineType)
            .collect(Collectors.toList());
    }
    
    private DeadlineType convertToDeadlineType(ABDeadlineType entity) {
        return new DeadlineType(entity.getId(), entity.getCode());
    }
}

Solution Two: Constructor Invocation Using @Query Annotation

Spring Data JPA supports directly calling the target class constructor in the @Query annotation. This approach is more elegant and offers better performance:

public interface DeadlineTypeRepository extends JpaRepository<ABDeadlineType, Long> {

    @Query("select new referencedata.DeadlineType(a.id, a.code) from ABDeadlineType a")
    List<DeadlineType> findAllSummarizedBy();
}

// Ensure DeadlineType has corresponding constructor
@Data
public class DeadlineType extends DefaultIdAndText {
    
    public DeadlineType(String id, String code) {
        super(id, code);
    }
    
    // Other methods remain unchanged
}

Solution Three: Using Spring Data Projection Interfaces

Spring Data provides a projection mechanism that allows defining interfaces to project specific properties of entity classes. This approach is more type-safe and easier to maintain:

// Projection interface definition
public interface DeadlineType {
    String getId();    
    String getText();
}

// Repository interface using projection
public interface DeadlineTypeRepository extends JpaRepository<ABDeadlineType, Long> {
    List<DeadlineType> findAllSummarizedBy();
}

If SpEL expressions are needed in the projection interface, @Value annotations can be added:

public interface DeadlineType {
    @Value("#{target.id}")
    String getId();
    
    @Value("#{target.code}")
    String getText();
}

In-depth Analysis of Type Converter Configuration

Referencing discussions about MapStruct extension libraries, we can understand how Spring ConversionService works. When using custom converters, it's essential to ensure they are properly registered in the Spring context.

For MapStruct converters, the correct configuration approach should be:

@Mapper
public interface DeadlineTypeMapper extends Converter<ABDeadlineType, DeadlineType> {
    
    @Override
    DeadlineType convert(ABDeadlineType source);
}

// Configuration class to ensure converter scanning
@Configuration
public class ConverterConfig {
    
    @Bean
    public ConversionService conversionService() {
        DefaultConversionService service = new DefaultConversionService();
        // Add custom converter
        service.addConverter(new DeadlineTypeMapperImpl());
        return service;
    }
}

Best Practice Recommendations

Based on the above analysis, we recommend the following best practices:

  1. Prefer Projection Interfaces: For simple property mapping, projection interfaces are the most concise and type-safe solution.
  2. Use @Query Constructor for Complex Conversions: When complex logic or custom constructors are needed, use the @Query annotation to call constructors.
  3. Consider Performance for Batch Processing: For large data conversions, consider completing mapping at the database level to avoid extensive object conversion in the application layer.
  4. Unify Conversion Strategies: Maintain consistent conversion strategies throughout the project, avoiding mixing multiple conversion approaches.

Common Pitfalls and Debugging Techniques

When resolving type conversion issues, be aware of these common pitfalls:

By understanding Spring Data's type conversion mechanism and appropriately selecting conversion strategies, developers can effectively avoid the 'No converter found capable of converting from type' exception, improving code maintainability and performance.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.