找不到Spring JPA储存库转换器 [英] Spring JPA repository Converter not found

查看:218
本文介绍了找不到Spring JPA储存库转换器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实体类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;


}

我需要根据每列中包含的值获取一些统计信息.所以我创建了一个JPQL查询,并将结果映射到另一个EntityUserStatisticsModel.java

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?

可用的解决方案:

  1. 我可以在UserModel.java中创建这些字段,但是我不想 在该类中创建这些字段.

  1. 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

更新 如果我将查询的返回类型更改为List<Integer>,则不会遇到任何异常,并且会得到正确的结果.但是为什么不能将那些Integer映射到我的@EntityUserStatisticsmodel.java

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

推荐答案

您的查询就是问题所在.您说Select 1 as id,但要返回UserStatisticsModel.因此,您必须修复查询或更改方法签名以返回Integer.

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.

这篇关于找不到Spring JPA储存库转换器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆