休眠错误-org.hibernate.hql.internal.ast.QuerySyntaxException

Posted

技术标签:

【中文标题】休眠错误-org.hibernate.hql.internal.ast.QuerySyntaxException【英文标题】:Hibernate Error-org.hibernate.hql.internal.ast.QuerySyntaxException 【发布时间】:2014-03-25 17:29:18 【问题描述】:

我正在尝试检索管理员输入的学生详细信息(即显示到学生页面)。但是这样做,我面临一个错误,即

org.apache.jasper.JasperException: An exception occurred processing JSP page /StuGenInfo_Stu.jsp at line 122

119:        sessionn.beginTransaction();
120:        String enroll=Stu_Name.getEnroll();
121:        out.println(enroll);
122:        Query query=sessionn.createQuery("from StuAcadDetails where ENROLLMENT_NUMBER="+enroll );
123:        List<StuAcadDetails> detail=(List<StuAcadDetails>)query.list();
124:        
125:        for(StuAcadDetails stuDetails:detail)


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:728)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
root cause

org.hibernate.hql.internal.ast.QuerySyntaxException: StuAcadDetails is not mapped [from StuAcadDetails where ENROLLMENT_NUMBER=NU/A9/45055]
    org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:180)
    org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:110)
    org.hibernate.hql.internal.ast.tree.FromClause.addFromElement(FromClause.java:93)
    org.hibernate.hql.internal.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:324)
    org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3420)
    org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3309)
    org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:706)
    org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:562)
    org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:299)
    org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:247)
    org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:250)
    org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:185)
    org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:138)
    org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:105)
    org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
    org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:168)
    org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:222)
    org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:200)
    org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1703)
    org.apache.jsp.StuGenInfo_005fStu_jsp._jspService(StuGenInfo_005fStu_jsp.java:203)
    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)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.47 logs.

Apache Tomcat/7.0.47

在我的项目中,管理员输入学生的学术信息,学生获得了登录 ID,它是一个名为 Enrollment_Number 的主键。一旦学生登录,学生应该能够看到有关其学术的所有信息。

我正在尝试通过将注册保存到会话中(保存在登录页面中)并使用此注册(在 StuGenInfo_Stu.jsp 页面中检索注册)从 mysql 数据库中获取相应的记录来实现这一点。配置文件

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
      "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
      "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd" >
<hibernate-configuration>

  <session-factory>
    <!-- Database connection settings -->
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.url">jdbc:mysql://localhost:3306/MBSIS</property>
    <property name="connection.username">root</property>
    <property name="connection.password">admin</property>

    <!-- JDBC connection pool (use the built-in) -->
    <property name="connection.pool_size">1</property>

    <!-- SQL dialect -->
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

    <!-- Enable Hibernate's automatic session context management -->
    <property name="current_session_context_class">thread</property>

    <!-- Disable the second-level cache  -->
    <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">true</property>

    <!-- Drop and re-create the database schema on startup -->
    <property name="hbm2ddl.auto">update</property>
    <mapping class="org.faculty.entity.RegistrationFaculty"/>
    <mapping class="org.faculty.entity.StuAcadDetails"/>
  </session-factory>
</hibernate-configuration>

StuAcad详情

@Table(name="ACADEMICS_DETAILS")
@Entity
public class StuAcadDetails 

    @Id
    @Column(name="ENROLLMENT_NUMBER")
    private String enroll;

    @Column(name="SEMESTER")
    private int semester;

    @Column(name="BRANCH")
    private String branch;

    @Temporal(TemporalType.DATE)
    @Column(name="DATE_OF_JOINING")
    private Date doj;

    @Column(name="ATTDCE_IN_THEORY")
    private int attThry;

    @Column(name="ATTDCE_IN_PRACTICAL")
    private int attPrac;

    @Column(name="PREVIOUS_SEM_PERCENTAGE")
    private float preSemPer;

    @Column(name="OVERALL_AGGREGATE")
    private float overallAggr;

    @Column(name="SESSIONAL_PERCENTAGE")
    private float sessPer;

    @Column(name="PERFORMANCE")
    private String performance;

    /*********************************************************************/
    public String getEnroll() 
        return enroll;
    
    public void setEnroll(String enroll) 
        this.enroll = enroll;
    
    public int getSemester() 
        return semester;
    
    public void setSemester(int semester) 
        this.semester = semester;
    
    public String getBranch() 
        return branch;
    
    public void setBranch(String branch) 
        this.branch = branch;
    
    public Date getDoj() 
        return doj;
    
    public void setDoj(Date doj) 
        this.doj = doj;
    
    public int getAttThry() 
        return attThry;
    
    public void setAttThry(int attThry) 
        this.attThry = attThry;
    
    public int getAttPrac() 
        return attPrac;
    
    public void setAttPrac(int attPrac) 
        this.attPrac = attPrac;
    
    public float getPreSemPer() 
        return preSemPer;
    
    public void setPreSemPer(float preSemPer) 
        this.preSemPer = preSemPer;
    
    public float getOverallAggr() 
        return overallAggr;
    
    public void setOverallAggr(float overallAggr) 
        this.overallAggr = overallAggr;
    
    public float getSessPer() 
        return sessPer;
    
    public void setSessPer(float sessPer) 
        this.sessPer = sessPer;
    
    public String getPerformance() 
        return performance;
    
    public void setPerformance(String performance) 
        this.performance = performance;
       


【问题讨论】:

@bellabax 是的..你想让我也分享那个代码吗? 【参考方案1】:

NU/A9/45055 是数字吗?或者应该是一个字符串?在此文本周围加上 '',或者更好的是使用参数

Query query=sessionn.createQuery("from org.faculty.entity.StuAcadDetails where enroll=:enrollNo").setParameter("enrollNo",enroll).list()

PS:检查上面的编辑,ENROLLMENT_NUMBER 必须更改为enroll;和类名 也是完全合格的。


试试这个标准;相当于你的查询

List<StuAcadDetails> detail=(List<StuAcadDetails>) sessionn.createCriteria(StuAcadDetails.class)
  .add(Restrictions.idEq(enroll))
.list()

【讨论】:

@bellabax-yes..它是一个字符串。我试过你说的,但堆栈跟踪仍然没有变化。 StuAcadDetails 用@Entity 注释吗? 我已经编辑了我的问题并在上面添加了 StuAcadDetails 的代码..看看它。 错误已解决...但为什么它通过使用 Criteria API 而不是通过 HQL 查询工作?? SessionFactoryHelper.requireClassPersister 查找名为StuAcadDetails 的实体,但实体的真实名称是org.faculty.entity.StuAcadDetails,所以你必须写完整的类名,而不仅仅是简单的名字!检查我的编辑,查询也应该有效!【参考方案2】:

您在查询中缺少别名

试试这个:

Query query=sessionn.createQuery("from StuAcadDetails sad where sad.ENROLLMENT_NUMBER="+enroll );

【讨论】:

@Shoaib-还是同样的问题..因为这里的变量登记是字符串类型..我的语法是正确的【参考方案3】:

    如果你仔细看看这里:StuAcadDetails 没有映射,我想它会很清楚。问题是 Hibernate 找不到 StuAcadDetails 实体。 Java 类和属性区分大小写。你必须检查这个。

    而且,我认为这会有所不同,您应该删除以下行:

在 xml 页面中删除此 线程

替换这一行:

 <property name="current_session_context_class">org.hibernate.context.internal.ThreadLocalSessionContext</property>

【讨论】:

@jmail-我照你说的做了..但仍然面临同样的错误。 @Tushar2098,你检查我的第一个条件了吗。 是的,我也检查过..一切都很好。 StuAcadDetails 已正确映射并且拼写也正确。参考我上面提到的配置代码。

以上是关于休眠错误-org.hibernate.hql.internal.ast.QuerySyntaxException的主要内容,如果未能解决你的问题,请参考以下文章

选择值时出现休眠错误

休眠错误,如何解决?

休眠命名查询不知道错误

[休眠]错误:找不到实体类:

休眠 JPA 错误

我在@OneToMany 休眠映射中遇到错误?