简体   繁体   中英

Spring JPA repository Converter not found

I have an entity class UserModel.java

@Entity
@Table
@Data
@EqualsAndHashCode( of = { "id" } )
@ToString( of = { "id" } )
public class UserModel {

@Id
@GeneratedValue( strategy = GenerationType.AUTO )
@Column( name = "u_id" )
private Long id;

@Column( name = "u_first_name" )
private String firstName;

@Column( name = "u_last_name" )
private String lastName;

@Column( name = "u_email_id" )
private String emailId;

@Column( name = "u_password" )
private String password;

@Column( name = "u_mobile_number" )
private String mobileNumber;

@Column( name = "u_created_at" )
private Calendar createdAt;

@Column( name = "u_is_active" )
private Boolean isActive;

@Column( name = "u_reason" )
private String reason;


}

I need to fetch some statistics based on the values contained in each column. So I created a JPQL query and the result I am mapping to one more Entity class UserStatisticsModel.java

@Data
@Entity
public class UserStatisticsModel {

@Id
@Column
private Integer id;

@Column
private Integer activeUsers;

@Column
private Integer suspendedUsers;

@Column
private Integer removedUser;
}

I created a repository class to execute the query.

public interface UserStatisticsRepository extends JpaRepository<UserStatisticsModel, Integer> {

@Query( " Select sum(case when u.isActive is true then 1 else 0 end) as activeUsers, "
        + " sum(case when u.isSuspended is true then 1 else 0 end) as suspendedUsers, "
        + " sum(case when u.isActive is false then 1 else 0 end) as removedUser"
        + " from UserModel u " + " where u.accountStatus <>5" )
UserStatisticsModel getUserStatistics();

}

But on executing the query I am getting an exception. Although as per logs query is getting executed and the values are perfect, but mapping is failing.

Caused by: org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.Integer] to type [com.highpeak.tlp.datastore.model.UserStatisticsModel]
at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:324)
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:206)
at org.springframework.core.convert.support.ArrayToObjectConverter.convert(ArrayToObjectConverter.java:66)
at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:37)
... 125 common frames omitted

Can anyone explain what is this error and how to fix it?

Available Solution:

  1. I can create those fields in UserModel.java but I do not want to create those fields in that class.

  2. I can also create a native query and it will work

UPDATE If I change the return type of the query to List<Integer> , I am not getting any exceptions and I am getting proper result. But why can not I map those Integers to the fields of my @Entity class UserStatisticsmodel.java

Your query is the issue. You say Select 1 as id but you want to return a UserStatisticsModel . So you'd have to fix your query or change the method signature to return an Integer instead.

正如@Babajide M. Moibi 所提到的,尝试将您的查询更改为: Select 1 as id , sum(case when u.isActive is true then 1 else 0 end) as activeUsers ....

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