好吧,我在hibernate中实现了一个独特的查询。它返回结果。但是,当铸造领域变得互换。因此,它会产生铸造错误。应该是什么解决方案?
作为一个例子,我有数据库,“ProjectAssignment”有三个字段,aid,pid&用户名。我想要这个表中的所有不同的userName数据。我已应用查询:
select distinct userName, aid, pid from ProjectAssignment
而ProjectAssignment.java文件中包含顺序辅助,pid和amp;中的字段。用户名。现在,这里userName是输出中的第一个字段。所以,铸造是不可能的。
另外,查询:
select aid, pid, distinct userName from ProjectAssignment
无效。
同样的正确查询是什么?或者还有什么解决方案?
代码如下:
系统利用率服务Bean方法,我必须检索数据:
public List<ProjectAssignment> getProjectAssignments() {
projectAssignments = ProjectAssignmentHelper.getAllResources(); //Here comes the error
return projectAssignments;
}
ProjectAssignmentHelper
我从哪里获取数据:
package com.hibernate;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
public class ProjectAssignmentHelper {
public static List<ProjectAssignment> getAllResources() {
List<ProjectAssignment> projectMasters;
Session session = HibernateUtil.getSessionFactory().openSession();
Query query = session.createQuery("select distinct aid, pid, userName from ProjectAssignment");
projectMasters = (List<ProjectAssignment>) query.list();
session.close();
return projectMasters;
}
}
Hibernate Data Bean:
package com.hibernate;
public class ProjectAssignment implements java.io.Serializable {
private short aid;
private String pid;
private String userName;
public ProjectAssignment() {
}
public ProjectAssignment(short aid) {
this.aid = aid;
}
public ProjectAssignment(short aid, String pid, String userName) {
this.aid = aid;
this.pid = pid;
this.userName = userName;
}
public short getAid() {
return this.aid;
}
public void setAid(short aid) {
this.aid = aid;
}
public String getPid() {
return this.pid;
}
public void setPid(String pid) {
this.pid = pid;
}
public String getUserName() {
return this.userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
错误:
对于输入字符串:“userName”
java.lang.NumberFormatException: For input string: "userName" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48) at java.lang.Integer.parseInt(Integer.java:447) at java.lang.Integer.parseInt(Integer.java:497) at javax.el.ArrayELResolver.toInteger(ArrayELResolver.java:375) at javax.el.ArrayELResolver.getValue(ArrayELResolver.java:195) at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:175) at com.sun.faces.el.FacesCompositeELResolver.getValue(FacesCompositeELResolver.java:72) at com.sun.el.parser.AstValue.getValue(AstValue.java:116) at com.sun.el.parser.AstValue.getValue(AstValue.java:163) at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:219) at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:102) at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:190) at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:178) at javax.faces.component.UICommand.getValue(UICommand.java:218) at org.primefaces.component.commandlink.CommandLinkRenderer.encodeMarkup(CommandLinkRenderer.java:113) at org.primefaces.component.commandlink.CommandLinkRenderer.encodeEnd(CommandLinkRenderer.java:54) at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:878) at org.primefaces.renderkit.CoreRenderer.renderChild(CoreRenderer.java:70) at org.primefaces.renderkit.CoreRenderer.renderChildren(CoreRenderer.java:54) at org.primefaces.component.datatable.DataTableRenderer.encodeTable(DataTableRenderer.java:525) at org.primefaces.component.datatable.DataTableRenderer.encodeMarkup(DataTableRenderer.java:407) at org.primefaces.component.datatable.DataTableRenderer.encodeEnd(DataTableRenderer.java:193) at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:878) at org.primefaces.renderkit.CoreRenderer.renderChild(CoreRenderer.java:70) at org.primefaces.renderkit.CoreRenderer.renderChildren(CoreRenderer.java:54) at org.primefaces.component.tabview.TabViewRenderer.encodeContents(TabViewRenderer.java:198) at org.primefaces.component.tabview.TabViewRenderer.encodeMarkup(TabViewRenderer.java:130) at org.primefaces.component.tabview.TabViewRenderer.encodeEnd(TabViewRenderer.java:48) at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:878) at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1620) at javax.faces.render.Renderer.encodeChildren(Renderer.java:168) at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:848) at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1613) at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1616) at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1616) at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:380) at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:126) at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:127) at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139) at javax.faces.webapp.FacesServlet.service(FacesServlet.java:313) at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1523) at org.apache.catalina.core.ApplicationDispatcher.doInvoke(ApplicationDispatcher.java:802) at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:664) at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:497) at org.apache.catalina.core.ApplicationDispatcher.doDispatch(ApplicationDispatcher.java:468) at org.apache.catalina.core.ApplicationDispatcher.dispatch(ApplicationDispatcher.java:364) at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:314) at org.apache.jasper.runtime.PageContextImpl.forward(PageContextImpl.java:783) at org.apache.jsp.welcome_jsp._jspService(welcome_jsp.java from :59) at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:109) at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:406) at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:483) at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:373) at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1523) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188) at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641) at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97) at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185) at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:332) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:233) at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:165) at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791) at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693) at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954) at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170) at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135) at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102) at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88) at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76) at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53) at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57) at com.sun.grizzly.ContextTask.run(ContextTask.java:69) at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330) at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309) at java.lang.Thread.run(Thread.java:619)
答案 0 :(得分:3)
Query query = session.createQuery("select distinct aid, pid, userName from ProjectAssignment");
实际上,query.list()将返回包含Array of Objects作为每个元素的ArrayList实例。并且数组中的每个元素都包含每列的值。
e.g。 query.list()。get(0)将返回一个包含三个对象的数组
array[0] = value of aid
array[1] = value of pid
array[2] = value of userName
因此,List<Object[]>
无法转换为List<ProjectAssignment>
一种可能的解决方案是实施ResultTransformer
像这样的东西.........首先告诉Hibernate使用什么变压器
query.setResultTransformer(new ProjectAssignmentTransformer());
现在实施
class ProjectAssignmentTransformer implements ResultTransformer {
@Override
public Object transformTuple(Object[] tuples, String[] aliases) {
Object aid = tuples[0];
Object pid = tuples[1];
Object userName = tuples[2];
/* construct ProjectAssignment instance using appropriate args*/
return new ProjectAssignment (aid, pid, (String) userName);
}
@Override
public List transformList(List collection) {
List<ProjectAssignment> assignments = new ArrayList<ProjectAssignment>();
for(Object o : collection)
{
assignments.add((ProjectAssignment)o);
}
return assignments;
}
}
在此之后,您将能够投射到List<ProjectAssignment>
。
希望这有帮助!
-SE
答案 1 :(得分:2)
首先,如果您使用如下的投影:
select distinct aid, pid, userName from ProjectAssignment
结果会得到Object[]
(或List<Object[]>
),而不是List<ProjectAssignment>
。如果您想在使用投影时获得强类型对象,则应使用select NEW
表达式(假设您提供了正确的构造函数):
select new com.hibernate.ProjectAssignment(...) from ProjectAssignment.
其次,我没有得到你真正想要的东西。从简单的英文描述中,查询将是:
select distinct userName from ProjectAssignment
但我不确定这是你的期望。也许如果你用简单的英语澄清预期的结果是什么,我可以帮助进一步。
第三,目前的问题与Hibernate和铸造无关:
java.lang.NumberFormatException: For input string: "userName"
您只是尝试将不是数字的字符串格式化为数字。
答案 2 :(得分:0)
为什么要使用HQL?
Hibernate的想法是使用实体而不是表。理想情况下,您可以加载实体并遍历它们以获取所需的字段。
我建议您对该查询使用Hibernate Criteria和Criterion。
Criteria crit = session.createCriteria(ProjectAssignment.class);
List projectAssignments = crit.list();
然后遍历列表,获取所需的字段。