问题
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:
I can create those fields in
UserModel.java
but I do not want to create those fields in that class.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
回答1:
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.
来源:https://stackoverflow.com/questions/50177517/spring-jpa-repository-converter-not-found