从mysql连接两个表并在一个JSP列表中显示信息[重复]
Posted
技术标签:
【中文标题】从mysql连接两个表并在一个JSP列表中显示信息[重复]【英文标题】:Joining two tables from mysql and displaying info in one JSP listings [duplicate] 【发布时间】:2015-09-08 08:01:11 【问题描述】:我有问题,可能是我的逻辑问题。 我想在一个列表中使用hibernate hql在mysql中的两个表中显示信息。我有两个实体,客户和项目。 客户可以有多个项目,但项目只能分配给一个客户。
所以我的 Projects 实体中有 clientsIDclients 来连接到客户。
我想列出所有具有这种格式的项目:
项目ID |客户名称 |项目名称 |项目描述 |项目开始日期 |等等……
所以我正在尝试显示客户名称而不是 clientsIDclients
在 hql 中,我设法得到了我需要的东西,但我不知道如何将这些信息放入我的 jsp 表中。
JSP sn-p:
<div class="table-responsive">
<table class="table table-hover">
<thead>
<tr>
<th>ID</th>
<th>Client ID</th>
<th>Name</th>
<th>Description</th>
<th>Status</th>
<th>Priority</th>
<th>Date Started</th>
<th>Date End</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<c:forEach var="user" items="$test" varStatus="status">
<tr>
<td>$status.index+1</td>
<td>$user.clName</td>
<td>$user.projName</td>
<td>$user.projDescription</td>
<td>$user.projDatestarted</td>
<td>$user.projDateend</td>
<td>$user.projPrice</td>
<td><a href="edit?id=$user.idProjects" class="btn btn-primary btn-sm"><span class="glyphicon glyphicon-edit"></span> Edit</a>
<a href="delete?id=$user.idProjects" class="btn btn-danger btn-sm"><span class="glyphicon glyphicon-remove"></span> Delete</a></td>
</tr>
</c:forEach>
</tbody>
</table>
ProjectDAOImpl.java:
public ProjectsDAOImpl(SessionFactory sessionFactory)
this.sessionFactory = sessionFactory;
@Override
@Transactional
public List<Projects> list()
String hql = "select projects.idProjects as ProjectID, clients.clName as ClientName, projects.projName as ProjectName," +
" projects.projDescription as ProjectDescription, projects.projPriority as Priority, projects.projStatus as Status, projects.projDatestarted as DateStarted, projects.projDateend as DateEnd," +
" projects.projPrice as Price from Clients clients, Projects projects where clients.idClients = projects.clientsIdClients";
Query query = sessionFactory.getCurrentSession().createQuery(hql);
@SuppressWarnings("unchecked")
List<Projects> listProject = (List<Projects>) query.list();
return listProject;
和 ProjectsController.java:
@RequestMapping("/")
public ModelAndView handleRequest() throws Exception
List<Projects> listProjects = projectsDao.list();
ModelAndView model = new ModelAndView("ProjectList");
model.addObject("projectList", listProjects);
return model;
而我的 tomcat 错误是
java.lang.NumberFormatException: For input string: "ProjectID"
java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
java.lang.Integer.parseInt(Integer.java:492)
java.lang.Integer.parseInt(Integer.java:527)
javax.el.ArrayELResolver.coerce(ArrayELResolver.java:159)
javax.el.ArrayELResolver.getValue(ArrayELResolver.java:45)
org.apache.jasper.el.JasperELResolver.getValue(JasperELResolver.java:104)
org.apache.el.parser.AstValue.getValue(AstValue.java:183)
org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:185)
org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:1026)
org.apache.jsp.WEB_002dINF.views.ProjectList_jsp._jspx_meth_c_005fforEach_005f0(ProjectList_jsp.java:137)
org.apache.jsp.WEB_002dINF.views.ProjectList_jsp._jspService(ProjectList_jsp.java:88)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:209)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:267)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1217)
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1005)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:952)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
pastebin 中的完整错误日志:http://pastebin.com/Md4R1uCe
所以,我的问题是,如何从 hql 访问数据并在 JSP 中显示,我迷失在这些数据流中。
谢谢
编辑:
Projects.java
package rs.ftn.ais.model;
import com.sun.xml.internal.bind.v2.model.core.ID;
import rs.ftn.ais.model.ProjectsPK;
import javax.persistence.*;
import java.sql.Date;
/**
* Created by Goran on 6/19/2015.
*/
@Entity
@Table(name = "projects", schema = "", catalog = "freelancepm_db")
@IdClass(ProjectsPK.class)
public class Projects
private int idProjects;
private String projName;
private String projDescription;
private String projStatus;
private String projPriority;
private Date projDatestarted;
private Date projDateend;
private String projPrice;
private int clientsIdClients;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "idPROJECTS", nullable = false, insertable = true, updatable = true)
public int getIdProjects()
return idProjects;
public void setIdProjects(int idProjects)
this.idProjects = idProjects;
@Basic
@Column(name = "PROJ_NAME", nullable = false, insertable = true, updatable = true, length = 45)
public String getProjName()
return projName;
public void setProjName(String projName)
this.projName = projName;
@Basic
@Column(name = "PROJ_DESCRIPTION", nullable = true, insertable = true, updatable = true, length = 45)
public String getProjDescription()
return projDescription;
public void setProjDescription(String projDescription)
this.projDescription = projDescription;
@Basic
@Column(name = "PROJ_STATUS", nullable = true, insertable = true, updatable = true, length = 45)
public String getProjStatus()
return projStatus;
public void setProjStatus(String projStatus)
this.projStatus = projStatus;
@Basic
@Column(name = "PROJ_PRIORITY", nullable = true, insertable = true, updatable = true, length = 45)
public String getProjPriority()
return projPriority;
public void setProjPriority(String projPriority)
this.projPriority = projPriority;
@Basic
@Column(name = "PROJ_DATESTARTED", nullable = true, insertable = true, updatable = true)
public Date getProjDatestarted()
return projDatestarted;
public void setProjDatestarted(Date projDatestarted)
this.projDatestarted = projDatestarted;
@Basic
@Column(name = "PROJ_DATEEND", nullable = true, insertable = true, updatable = true)
public Date getProjDateend()
return projDateend;
public void setProjDateend(Date projDateend)
this.projDateend = projDateend;
@Basic
@Column(name = "PROJ_PRICE", nullable = false, insertable = true, updatable = true, length = 45)
public String getProjPrice()
return projPrice;
public void setProjPrice(String projPrice)
this.projPrice = projPrice;
@Id
@Column(name = "CLIENTS_idCLIENTS", nullable = false, insertable = true, updatable = true)
public int getClientsIdClients()
return clientsIdClients;
public void setClientsIdClients(int clientsIdClients)
this.clientsIdClients = clientsIdClients;
@Override
public boolean equals(Object o)
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Projects that = (Projects) o;
if (clientsIdClients != that.clientsIdClients) return false;
if (idProjects != that.idProjects) return false;
if (projDateend != null ? !projDateend.equals(that.projDateend) : that.projDateend != null) return false;
if (projDatestarted != null ? !projDatestarted.equals(that.projDatestarted) : that.projDatestarted != null)
return false;
if (projDescription != null ? !projDescription.equals(that.projDescription) : that.projDescription != null)
return false;
if (projName != null ? !projName.equals(that.projName) : that.projName != null) return false;
if (projPrice != null ? !projPrice.equals(that.projPrice) : that.projPrice != null) return false;
if (projPriority != null ? !projPriority.equals(that.projPriority) : that.projPriority != null) return false;
if (projStatus != null ? !projStatus.equals(that.projStatus) : that.projStatus != null) return false;
return true;
@Override
public int hashCode()
int result = idProjects;
result = 31 * result + (projName != null ? projName.hashCode() : 0);
result = 31 * result + (projDescription != null ? projDescription.hashCode() : 0);
result = 31 * result + (projStatus != null ? projStatus.hashCode() : 0);
result = 31 * result + (projPriority != null ? projPriority.hashCode() : 0);
result = 31 * result + (projDatestarted != null ? projDatestarted.hashCode() : 0);
result = 31 * result + (projDateend != null ? projDateend.hashCode() : 0);
result = 31 * result + (projPrice != null ? projPrice.hashCode() : 0);
result = 31 * result + clientsIdClients;
return result;
Branislav - idProjects 是 int
redflair - 抱歉,这里是 Projects.java
编辑 2
我使用 ProjectsFormatted.java 扩展了 Projects.java,因此我可以使用客户名称而不是客户 ID 创建对象。
ProjectsFormatted.java 公共类 ProjectsFormatted 扩展项目
private String clName;
public String getClName()
return clName;
public void setClName(String clName)
this.clName = clName;
ProjectsFormattedDAO.java
package rs.ftn.ais.dao;
import org.springframework.web.servlet.ModelAndView;
import rs.ftn.ais.model.ProjectsFormatted;
import java.util.List;
public interface ProjectsFormattedDAO extends ProjectsDAO
public List<ProjectsFormatted> listProjects();
ProjectsFormattedDAOImpl.java
public class ProjectsFormattedDAOImpl extends ProjectsDAOImpl implements ProjectsFormattedDAO
@Autowired
private SessionFactory sessionFactory;
public ProjectsFormattedDAOImpl()
public ProjectsFormattedDAOImpl(SessionFactory sessionFactory)
this.sessionFactory = sessionFactory;
@Override
@Transactional
public List<ProjectsFormatted> listProjects()
String hql = "select projects.idProjects, clients.clName, projects.projName, projects.projDescription, projects.projPriority, projects.projStatus, projects.projDatestarted, projects.projDateend, projects.projPrice from Clients clients, Projects projects where clients.idClients = projects.clientsIdClients";
Query query = sessionFactory.getCurrentSession().createQuery(hql);
List<ProjectsFormatted> listProjects = (List<ProjectsFormatted>) query.list();
return listProjects;
我的控制器现在看起来像这样:
@RequestMapping("/")
public ModelAndView handleRequest() throws Exception
//List<Projects> listProjects = projectsDao.list();
ModelAndView model = new ModelAndView("ProjectList");
List<ProjectsFormatted> listProjects = projectsFormattedDao.listProjects();
model.addObject("test", listProjects);
return model;
在调试过程中,我看到控制器中的 listProjects 填充了数据,但使用 model.addObject("test", listProjects) 什么也没做,我无法访问 JSP 中的数据。
运行站点后,出现 Tomcat 错误: java.lang.NumberFormatException:对于输入字符串:“clName”
同样,Pastebin 的完整错误日志
【问题讨论】:
你是如何在你的数据库中声明idProjects
的?它是哪种数据类型?
请同时发布您的Projects
对象
【参考方案1】:
在您的 DAO 中,您正在设置一个包含所有对象项目的列表。请通过打印所有 idProjects 检查您是否拥有您所期望的一切。如果不是,那么您在从数据库中提取数据的方式上会遇到一些问题。也许您应该考虑根据您的映射在查询中设置别名。
然后,在您的 JSP 中,您试图访问对象中不存在的字段。在 Projects 类中,我看不到 ProjectID 字段。请使用那里的名称而不是您在查询中选择的别名来访问您的字段。
<td>$user.idProjects</td>
如果仍有错误,请返回
【讨论】:
感谢您指出这一点。我确实尝试在 Projects 对象中填充客户名称,该对象没有相应的字段。因此,我创建了新模型 ProjectsFormatted,它使用字符串字段 clName 扩展了我的 Projects 类。我将用新课程更新我的问题。在调试过程中,我可以看到在控制器中,我可以获得正确的数据,但我无法将其传递给 ModelAndView 对象,因此我无法在我的 jsp 中显示它们。 错误可能不是我想的那个。请您在this thread 上尝试接受的答案。当 Hibernate 获取他的数据时,似乎有一个错误,它没有找到正确的类型。可能需要使用标量。对于我的个人信息,请检查您在数据库中的数据类型。我认为 Hibernate 获取与 db 类型对应的数据。也许有区别以上是关于从mysql连接两个表并在一个JSP列表中显示信息[重复]的主要内容,如果未能解决你的问题,请参考以下文章