org.hibernate.hql.internal.ast.QuerySyntaxException:未映射用户

时间:2016-04-11 21:41:01

标签: hibernate hibernate-mapping

我是Hibernate的新手。

我正在尝试使用Hibernate创建在线用户调查,使用Oracle 10g数据库连接。在解决了一系列问题之后,我一直坚持这个问题:

  

HTTP状态500 - org.hibernate.hql.internal.ast.QuerySyntaxException:未映射用户[从用户用户中选择max(users.id)]

这是UserDAO.java

public class UserDAO {
    Session session = HibernateUtil.getSessionFactory().openSession();
    public void save(User user){
        session.beginTransaction();
        session.save(user);
        session.getTransaction().commit();
        session.close();
    }
    public Integer getId (){
        String hql = "select max(users.id) from User users";
        Query query = session.createQuery(hql);
        List<Integer> results = query.list();
        Integer userId = 1;
                if (results.get(0) != null ) {
               userId = results.get(0) + 1;
                }
                return userId;
    }
} 

这是User.java:

@Entity
@Table(name="User")
public class User {


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

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


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

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

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

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


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

    @Id
    @Column(name="email")
    private String email;

    @Column(name="date")
    private Date todayDate;

    @Column(name="semdate")
    private Date semDate;

    // ...
}

这是HibernateUtil.java:

public class HibernateUtil {
     private static SessionFactory sessionFactory ;
     static {
        Configuration configuration = new Configuration().configure();
        StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder()
                                      .applySettings(configuration.getProperties());
        sessionFactory = configuration.buildSessionFactory(builder.build());
     }
    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}  

这是hibernate.cfg.xml:

 <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    <hibernate-configuration>
     <session-factory>
      <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
      <property name="hibernate.connection.url">jdbc:oracle:thin:@apollo.vse.gmu.edu:1521:ite10g</property>
      <property name="hibernate.connection.username">name</property>
      <property name="hibernate.connection.password">password</property>
      <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
      <property name="hibernate.default_schema">name</property>
      <property name="show_sql">true</property>

    </session-factory>
    </hibernate-configuration>

编辑:

我做了这里很棒的人提出的建议,即将映射类添加到xml文件中。这确实消除了原始错误。但现在,我面临一些其他错误: HTTP状态500 - org.hibernate.exception.GenericJDBCException:无法提取ResultSet

完整的堆栈跟踪在这里:

type Exception report

message org.hibernate.exception.GenericJDBCException: could not extract ResultSet

description The server encountered an internal error that prevented it from fulfilling this request.

exception
javax.servlet.ServletException: org.hibernate.exception.GenericJDBCException: could not extract ResultSet
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:659)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)


root cause
javax.faces.el.EvaluationException: org.hibernate.exception.GenericJDBCException: could not extract ResultSet
    javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:101)
    com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
    javax.faces.component.UICommand.broadcast(UICommand.java:315)
    javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
    javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
    com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)


root cause
org.hibernate.exception.GenericJDBCException: could not extract ResultSet
    org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:54)
    org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
    org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)
    org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:89)
    org.hibernate.loader.Loader.getResultSet(Loader.java:2065)
    org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1862)
    org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1838)
    org.hibernate.loader.Loader.doQuery(Loader.java:909)
    org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:354)
    org.hibernate.loader.Loader.doList(Loader.java:2553)
    org.hibernate.loader.Loader.doList(Loader.java:2539)
    org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2369)
    org.hibernate.loader.Loader.list(Loader.java:2364)
    org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:496)
    org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:387)
    org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:231)
    org.hibernate.internal.SessionImpl.list(SessionImpl.java:1264)
    org.hibernate.internal.QueryImpl.list(QueryImpl.java:103)
    com.pratikshinde.swe645.UserDAO.getId(UserDAO.java:18)
    com.pratikshinde.swe645.StudentService.saveUser(StudentService.java:361)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:497)
    org.apache.el.parser.AstValue.invoke(AstValue.java:247)
    org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:267)
    com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
    javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
    com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
    javax.faces.component.UICommand.broadcast(UICommand.java:315)
    javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
    javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
    com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)


root cause
java.sql.SQLException: Io exception: Size Data Unit (SDU) mismatch
    oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
    oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146)
    oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:256)
    oracle.jdbc.driver.T4CPreparedStatement.executeForDescribe(T4CPreparedStatement.java:828)
    oracle.jdbc.driver.OracleStatement.executeMaybeDescribe(OracleStatement.java:1050)
    oracle.jdbc.driver.T4CPreparedStatement.executeMaybeDescribe(T4CPreparedStatement.java:853)
    oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1155)
    oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3414)
    oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:3459)
    org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:80)
    org.hibernate.loader.Loader.getResultSet(Loader.java:2065)
    org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1862)
    org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1838)
    org.hibernate.loader.Loader.doQuery(Loader.java:909)
    org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:354)
    org.hibernate.loader.Loader.doList(Loader.java:2553)
    org.hibernate.loader.Loader.doList(Loader.java:2539)
    org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2369)
    org.hibernate.loader.Loader.list(Loader.java:2364)
    org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:496)
    org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:387)
    org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:231)
    org.hibernate.internal.SessionImpl.list(SessionImpl.java:1264)
    org.hibernate.internal.QueryImpl.list(QueryImpl.java:103)
    com.pratikshinde.swe645.UserDAO.getId(UserDAO.java:18)
    com.pratikshinde.swe645.StudentService.saveUser(StudentService.java:361)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:497)
    org.apache.el.parser.AstValue.invoke(AstValue.java:247)
    org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:267)
    com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
    javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
    com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
    javax.faces.component.UICommand.broadcast(UICommand.java:315)
    javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
    javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
    com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

请再帮忙一下!万分感谢!

3 个答案:

答案 0 :(得分:1)

<session-factory> </session-factory>

中的hibernate.cfg.xml之间添加此内容
<mapping class ="com.demo.User"/>  

你错过了映射实体类

答案 1 :(得分:1)

您需要添加@kakashiHatake建议

<mapping class ="com.demo.User"/>

如果您使用Hibernate 5,则需要以这种方式配置会话工厂

sessionFactory = new Configuration().configure().buildSessionFactory();

答案 2 :(得分:0)

更改行:

Configuration configuration = new Configuration().configure();

到:

Configuration configuration = new Configuration()
                                         .addPackage("com.demo")
                                         .addAnnotatedClass(User.class)
                                         // add all other classes you want
                                         .configure();

编辑:请删除托管bean @Entity上的StudentService