如何使用hibernate从多个表查询中获取数据?

时间:2013-06-03 07:27:31

标签: java hibernate spring-mvc

我正在开发一个使用Spring& amp;的简单应用程序。休眠。 IDE:Eclipse

我想要显示学生的日程安排。

时间表使用学生,科目,考试和&班级表。

时间表包含:学生姓名,学习名称,科目,课程,考试,时间,时间和时间。日期。

我已经写了一个查询(使用> 1表),它会返回一条记录,&此记录将显示为计划。

查询没问题(我在控制台上打印了对象)。

我从.jsp获取记录时从我的.jsp获取NumberFormatException(列表从Controller发送)

1)这是从jsp列表中检索多个表值的正确方法吗?如果这个错误,请提出正确的建议。

2)以下代码的任何部分中的任何其他更改,都可以满足我的需求?

3)任何其他做同样事情的方法.. ??

例外:

    exception

org.apache.jasper.JasperException: An exception occurred processing JSP page /pages/studentschedule.jsp at line 301

298:                </tr>
299:                <c:forEach items="${scheduleList}" var="var">
300:                <tr>
301:                    <td><b>${var.firstname }</b></td>
302:                    <td><b>${var.roll }</b></td>
303:                    <td><b>${var.subjectname }</b></td>
304:                    <td><b>${var.classname }</b></td>


Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470)
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:722)
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:262)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1265)
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1016)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:965)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:859)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:883)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:781)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

root cause

java.lang.NumberFormatException: For input string: "firstname"
java.lang.NumberFormatException.forInputString(Unknown Source)
java.lang.Integer.parseInt(Unknown Source)
java.lang.Integer.parseInt(Unknown Source)
javax.el.ArrayELResolver.coerce(ArrayELResolver.java:166)
javax.el.ArrayELResolver.getValue(ArrayELResolver.java:46)
javax.el.CompositeELResolver.getValue(CompositeELResolver.java:67)
org.apache.el.parser.AstValue.getValue(AstValue.java:169)
org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:189)
org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:985)
org.apache.jsp.pages.studentschedule_jsp._jspx_meth_c_005fforEach_005f0(studentschedule_jsp.java:459)
org.apache.jsp.pages.studentschedule_jsp._jspService(studentschedule_jsp.java:364)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
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:722)
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:262)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1265)
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1016)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:965)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:859)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:883)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:781)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.25 logs.

查询+代码如下。

AdminStudentDaoImpl.java

@Override
public List<Student> getStudentSchedule(int a) {
    // TODO Auto-generated method stub

    List<Student> u=hibernatetemplate.find("select stud.firstname,stud.roll,sub.subjectname,cls.classname,exam.exam_name,exam.from_time,exam.to_time,exam.exam_date from Student stud,Subject sub,Classs cls,Exam exam where exam.classs=cls.id and exam.subject=sub.id and sub.classs=cls.id and stud.classs=cls.id and stud.roll="+a);

    return u;   
}

域名(表格)如下:

学生

@Entity
@Table(name="student")
public class Student {

@Id
@GeneratedValue
@Column(name="id")
private int id;
@Column(name="roll_no")
private int roll;
@Column(name="firstname")
private String firstname;
@Column(name="lastname")
private String lastname;
@Column(name="dob")
private String dob;
@Column(name="email")
private String email;
@Column(name="contactno")
private String contactno;
@ManyToOne
private Classs classs;

    Getters & Setters....... 
    }

@Entity
@Table(name="class")

public class Classs {

@Id
@GeneratedValue
@Column(name="id")
private int id;
    @Column(name="classname")
private String classname;
    Getters & Setters.....
}

考试

@Entity
@Table(name="exam")
public class Exam {

@Id
@GeneratedValue
@Column(name="id")
private int id;
@Column(name="exam_name")
private String exam_name;

@Column(name="exam_date")
private String exam_date;

@Column(name="from_time")
private String from_time;

@Column(name="to_time")
private String to_time;

@ManyToOne
private Subject subject;

@ManyToOne
private Classs classs;

    Getters & Setters.....
}

主题

@Entity
@Table(name="subject")
public class Subject {

    @Id
    @GeneratedValue

    @Column(name="id")
    private int id;

    @Column(name="subjectname")
    private String subjectname;

    @ManyToOne
    private Classs classs;

            Getters & Setters.....
}

在Controller中,我编写了这样的代码:

@RequestMapping(value="schedule" ,method=RequestMethod.GET)
public ModelAndView schedule(@ModelAttribute("attribute") Student student,BindingResult result,HttpSession session)throws Exception
{
    int roll=Integer.parseInt((String) session.getAttribute("username"));
    System.out.println("roll no is========="+roll);
    List<Student> stud = adminstudentdao.getStudentSchedule(roll);
    System.out.println("list is========="+stud);
    ModelMap map=new ModelMap();
    map.addAttribute("scheduleList",stud);
    return new ModelAndView("studentschedule",map);
}

JSP

<table border="0" width="100%" cellpadding="0" cellspacing="0" id="schedule-table">
            <tr>

                <th class="table-header-repeat line-left minwidth-1"><a href="">Student name</th>
                <th class="table-header-repeat line-left minwidth-1"><a href="">Roll number</th>
                <th class="table-header-repeat line-left minwidth-1"><a href="">Subject</th>
                <th class="table-header-repeat line-left minwidth-1"><a href="">Class</th>
                <th class="table-header-repeat line-left minwidth-1"><a href="">exam</a></th>
                <th class="table-header-repeat line-left minwidth-1"><a href="">from time</th>
                <th class="table-header-repeat line-left minwidth-1"><a href="">to time</th>
                <th class="table-header-repeat line-left minwidth-1"><a href="">date</th>

            </tr>
             <c:if test="${empty scheduleList}">
                   <tr>
                         <td colspan="8"><b>No Results found</b></td>
                   </tr>
             </c:if>
            <c:forEach items="${scheduleList}" var="var">
            <tr>
                <td><b>${var.firstname }</b></td>
                <td><b>${var.roll }</b></td>
                <td><b>${var.subjectname }</b></td>
                <td><b>${var.classname }</b></td>
                <td><b>${var.exam_name }</b></td>
                <td><b>${var.from_time }</b></td>
                <td><b>${var.to_time }</b></td>
                <td><b>${var.exam_date }</b></td>


            </tr>
            </c:forEach>

            </table>

1 个答案:

答案 0 :(得分:10)

我建议您使用Hibernate Native SQL。为此,您可以将AdminStudentDaoImpl.java修改为:

public List getStudentSchedule(int a) {
     Session sessionHb = session.openSession();
     String sql = "select stud.firstname,stud.roll,sub.subjectname,cls.classname,exam.exam_name,exam.from_time,exam.to_time,exam.exam_date from Student stud,Subject sub,Classs cls,Exam exam where exam.classs=cls.id and exam.subject=sub.id and sub.classs=cls.id and stud.classs=cls.id and stud.roll= :roll";
     SQLQuery query = sessionHb.createSQLQuery(sql);
     query.setParameter("roll", a);
     query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
     return query.list();
}

我希望它会有所帮助:)