[英]java.lang.ClassCastException: java.lang.String cannot be cast to com.security.model.UserRequest
Am retriving UserRequest
object from DB using JPA Query as following. 使用JPA Query从数据库中检索
UserRequest
对象,如下所示。
@Query("SELECT req.supervisorEmail from User u, UserRequest as req WHERE u.username = req.userRequestName and req.userRequestRole= '4' and u.active=true")
public List<UserRequest> getSupervisorEmailIds();
Am having the UserRequest
POJO class as follows. 如下具有
UserRequest
POJO类。
public class UserRequest implements Identifiable<Long>, LazilyLoadable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@JsonIgnore
private Long id;
@Column(name = "supervisor_email")
private String supervisorEmail;
@Column(name="user_request_name")
private String userRequestName;
@Column(name="user_request_role")
private String userRequestRole;
}
when am trying to iterating over the UserRequest
object am getting the ClassCastException
for the below code. 当尝试遍历
UserRequest
对象时,获取以下代码的ClassCastException
。
List<UserRequest> supervisorEmailIds = userService.getSupervisorEmailIds();
for(UserRequest s: supervisorEmailIds) { // throwing exception on this line.
if(s!=null) {
System.out.println("Printing -->"+s.getSupervisorEmail());
}
System.out.println("Null error");
}
Following is the error am getting. 以下是错误越来越严重。
2019-02-22 00:34:26,719 [http-9191-1] ERROR com.cat.pscs.api.controller.BaseController: 83 - Unhandled exception while processing request for URL : http://localhost:9191/security/users/get-supervisor-emailids with exception : java.lang.String cannot be cast to com.cat.pscs.security.model.UserRequest
java.lang.ClassCastException: java.lang.String cannot be cast to com.cat.pscs.security.model.UserRequest at com.cat.pscs.security.controller.UserController.getSupervisorEmailIds(UserController.java:320) java.lang.ClassCastException:java.lang.String无法在com.cat.pscs.security.controller.UserController.getSupervisorEmailIds(UserController.java:320)上转换为com.cat.pscs.security.model.UserRequest
Are you sure that below line returns UserRequestList? 您确定下面的行返回UserRequestList吗? I think it returns list of strings
我认为它返回字符串列表
List<UserRequest> supervisorEmailIds = userService.getSupervisorEmailIds();
just change your sql query that returns list of UserRequest object . 只需更改返回UserRequest对象列表的sql查询即可。
Your query returns list of String
, while you expect to get list of UserRequest
. 您的查询返回
String
列表,而您希望获得UserRequest
列表。 Changing query as follow should do the job: 如下更改查询应该可以完成这项工作:
@Query("SELECT req from User u, UserRequest as req WHERE u.username = req.userRequestName and req.userRequestRole= '4' and u.active=true")
Inside @Query
you wrote 在您写的
@Query
内部
SELECT req.supervisorEmail from User u, UserRequest as req...
I suppose supervisorEmail
is a VARCHAR
or CHAR
table column. 我想
supervisorEmail
是一个VARCHAR
或CHAR
表列。
You're extracting only a single String
column. 您仅提取单个
String
列。
If you want UserRequest
(s), you need 如果需要
UserRequest
,则需要
SELECT req from User u, UserRequest as req...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.