Here is the code:
这是代码:
public Users login(String username) {
Users user=null;
try {
String hql="select user.name,user.password from Users user where user.name=:name";
Query query = session.createQuery(hql);
query.setString("name", username);
user=(Users) query.list().get(0);
} catch (HibernateException e) {
e.printStackTrace();
}
return user;
}
Error:
错误:
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.xl.entity.Users
at com.xl.impl.HouseDaoImpl.login(HouseDaoImpl.java:51)
at com.xl.biz.HouseBiz.login(HouseBiz.java:25)
at com.xl.Servlet.UserServlet.doGet(UserServlet.java:25)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)
(Translations courtesy Google Translate)
(翻译由谷歌翻译)
4 个解决方案
#1
3
If you need the Users object, edit your query to be like
如果您需要用户对象,请编辑您的查询。
String hql="from Users user where user.name=:name";
Otherwise the result will be an array of objects so you need to either use a transformer or simply cast it to object[]:
否则,结果将是一个对象数组,因此您需要使用transformer或简单地将其转换为对象[]:
String hql="select user.password from Users user where user.name=:name";
Query query = session.createQuery(hql);
query.setString("name", username);
Object[] result =(Object[]) query.list().get(0);
String password = result[0];
#2
1
You can create another constructor for Users
as public Users(String name, String password)
and change query to
您可以为用户创建另一个构造函数,作为公共用户(字符串名称、字符串密码)和更改查询。
String hql="select new yourpath.Users(user.name,user.password) from Users user
where user.name=:name";
#3
1
You need a ResultTransformer backed on root entity in this manner
您需要以这种方式在根实体上支持的ResultTransformer。
Query query = session.createQuery(hql);
query.setResultTransformer(RootEntityResultTransformer.INSTANCE);
query.list()
Using a projection (the select list) will produce a result of Object[]
type: the ResultTransformer
will create a User
object and maps raw Object[]
to correct User
's property
使用投影(选择列表)将产生对象[]类型的结果:ResultTransformer将创建一个用户对象并映射原始对象[]来纠正用户的属性。
#4
-1
Since query.list()
returns List
, and here you are casting the content of the list to type User
, Now, as exception says Object can not be cast to User
. Please make sure, are you getting the content in form of user.
由于query.list()返回列表,这里您正在将列表的内容转换为类型用户,现在,作为异常表示对象不能被转换为用户。请确保你的内容是以用户的形式出现的。
You can make a check:
你可以做一个检查:
if(query.list() instanceOf User){
// can cast to user.
}else{
// can not cast to User.
}
#1
3
If you need the Users object, edit your query to be like
如果您需要用户对象,请编辑您的查询。
String hql="from Users user where user.name=:name";
Otherwise the result will be an array of objects so you need to either use a transformer or simply cast it to object[]:
否则,结果将是一个对象数组,因此您需要使用transformer或简单地将其转换为对象[]:
String hql="select user.password from Users user where user.name=:name";
Query query = session.createQuery(hql);
query.setString("name", username);
Object[] result =(Object[]) query.list().get(0);
String password = result[0];
#2
1
You can create another constructor for Users
as public Users(String name, String password)
and change query to
您可以为用户创建另一个构造函数,作为公共用户(字符串名称、字符串密码)和更改查询。
String hql="select new yourpath.Users(user.name,user.password) from Users user
where user.name=:name";
#3
1
You need a ResultTransformer backed on root entity in this manner
您需要以这种方式在根实体上支持的ResultTransformer。
Query query = session.createQuery(hql);
query.setResultTransformer(RootEntityResultTransformer.INSTANCE);
query.list()
Using a projection (the select list) will produce a result of Object[]
type: the ResultTransformer
will create a User
object and maps raw Object[]
to correct User
's property
使用投影(选择列表)将产生对象[]类型的结果:ResultTransformer将创建一个用户对象并映射原始对象[]来纠正用户的属性。
#4
-1
Since query.list()
returns List
, and here you are casting the content of the list to type User
, Now, as exception says Object can not be cast to User
. Please make sure, are you getting the content in form of user.
由于query.list()返回列表,这里您正在将列表的内容转换为类型用户,现在,作为异常表示对象不能被转换为用户。请确保你的内容是以用户的形式出现的。
You can make a check:
你可以做一个检查:
if(query.list() instanceOf User){
// can cast to user.
}else{
// can not cast to User.
}