简体   繁体   中英

Failed to convert from type [java.util.LinkedHashSet<?>] to type [java.util.Set<java.lang.Long>]

I'm currently updating my project from Java 8 with Springboot 1.5.9.RELEASE to Java 11 with Springboot 2.2.7.RELEASE . I was able to make it run although when I was doing the testing, one query of it does not work anymore. It's throwing a Failed to convert from type [java.util.LinkedHashSet<?>] to type [java.util.Set<java.lang.Long>]... nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [project.Param] to type [java.lang.Long] . The project also builds perfectly and does not have any warnings of deprecated items on the classes stated below.

This is how I call it in my service:

Set<Long> ids = ParamRepository.findIdByCampaign(campaign);

Param.java

@Entity
@Table(name = "PARAM")
public class Param implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "ID_PARAM", updatable = false, nullable = false)
    private Long id;

    @ManyToOne(cascade = CascadeType.MERGE, fetch = FetchType.LAZY)
    @JoinColumn(name = "ID_CAMPAIGN",  nullable = false)
    private Campaign campaign;

ParamRepository.java

@Repository 
public interface ParamRepository extends JpaRepository<Param, Long> {
        Set<Long> findIdByCampaign(Campaign campaign);
    }

I've tried changing the JpaRepository<Param, Long> to JpaRepository<Param, Serializable> since, besides the findByCampaign query, it also involves other native queries. Although it doesn't make sense since it was working before so I'm quite not sure what to check next.

Any other thoughts on why the error occurs or to what I should check further? Thank you in advance!

findIdByCampaign will return Collection of Entity means Param not Long , you should select the id only to get Set of Long.

To select only id, a way can be using JPQL with @Query annotation.

@Query("Select p.id from Param p where p.campaign = :campaign")
Set<Long> findIdByCampaign(@Param("campaign") Campaign campaign);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM