java.lang.IllegalArgumentException:org.hibernate.hql.internal.ast.QuerySyntaxException:用户未映射[来自用户]

时间:2018-05-06 10:25:46

标签: java hibernate java-ee orm hql

我收到 java.lang.IllegalArgumentException:org.hibernate.hql.internal.ast.QuerySyntaxException:用户未映射[来自用户] 错误。我已经解决了这些问题,但没有帮助我。

org.hibernate.hql.internal.ast.QuerySyntaxException: table is not mapped

Hibernate error - QuerySyntaxException: users is not mapped [from users]

  

数据库表格图片:

数据库表user.jpg

enter image description here

  

UserBean.java

包装模型;

import java.util.ArrayList;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import controller.Login;
import controller.userinfo;
import gfgshfhjdgfhjdhhjfhjd.iModel;

@Entity
@Table(name= "User")
 public class UserBean  implements iModel
 {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="Id")
    private int id; 
    @Column(name="Username")
    private String username ;
    @Column(name="Password")
    private String password ;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public UserBean(){}
    public ArrayList<?> getData() {
        return userinfo.getUser();
    }


 }
  

Userinfo.java

package controller;


import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;

import model.UserBean;
import test.HibernateSessionFactory;


public class Userinfo  {

    public static void saveUser(UserBean user) {
           Session session = HibernateSessionFactory.getSessionFactory().openSession();
           session.beginTransaction();

           session.save(user);

           session.getTransaction().commit();
          }

    public static ArrayList<?> getUser() {
           Session session = HibernateSessionFactory.getSessionFactory().openSession();
           session.beginTransaction();

           List<UserBean> list = (List<UserBean>)session.createQuery("from User").list();
           ArrayList<String> list2= new ArrayList<String>();


           for(int i=0;i<list.size();i++) {
               int tmp = list.get(i).getId();
               StringBuilder sBuilder = new StringBuilder();
               sBuilder.append(tmp);
               list2.add(sBuilder.toString());
               list2.add(list.get(i).getUsername());
               list2.add(list.get(i).getPassword());
           }

           if (list != null) {
            for (int i = 0; i < list.size(); i++) {
             System.out.println("User ID : " + list.get(i).getId());
             System.out.println("User First Name : "+ list.get(i).getUsername());
             System.out.println("User Last Name : "+ list.get(i).getPassword());

           }
           }
           session.getTransaction().commit();
           return list2;
          }

}
  

的hibernate.cfg.xml

<?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>
        <!-- SQL Dialect -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

        <!-- Database Connection Settings -->
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/project</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password"></property>
        <property name="show_sql">true</property>

        <!-- Specifying Session Context -->
        <property name="hibernate.current_session_context_class">org.hibernate.context.internal.ThreadLocalSessionContext</property>
<!--        <property name="hibernate.hbm2ddl.auto">create</property> -->
        <!-- Mapping With Model Class Containing Annotations -->
        <mapping class="model.UserBean" />
    </session-factory>
</hibernate-configuration>
  

错误日志:

May 06, 2018 4:16:04 PM org.hibernate.hql.internal.QueryTranslatorFactoryInitiator initiateService
INFO: HHH000397: Using ASTQueryTranslatorFactory
java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: User is not mapped [from User]
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:133)
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:157)
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:164)
    at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:670)
    at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:103)
    at controller.Userinfo.getUser(Userinfo.java:28)
    at model.UserBean.getData(UserBean.java:55)
    at controller.Login.doGet(Login.java:38)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2430)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2419)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: User is not mapped [from User]
    at org.hibernate.hql.internal.ast.QuerySyntaxException.generateQueryException(QuerySyntaxException.java:79)
    at org.hibernate.QueryException.wrapWithQueryString(QueryException.java:103)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:217)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:141)
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:115)
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
    at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:153)
    at org.hibernate.internal.AbstractSharedSessionContract.getQueryPlan(AbstractSharedSessionContract.java:553)
    at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:662)
    ... 26 more
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: User is not mapped
    at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:169)
    at org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:91)
    at org.hibernate.hql.internal.ast.tree.FromClause.addFromElement(FromClause.java:79)
    at org.hibernate.hql.internal.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:326)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3706)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3595)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:720)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:576)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:313)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:261)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:266)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:189)
    ... 32 more

请帮助..谢谢

2 个答案:

答案 0 :(得分:0)

正如我从您的数据库方案中看到的那样,UserBean.java中的列映射名称不正确(例如@Column(name="Username"),而它应该是小写)。可能与表名相同。

可能是一个原因

第二种可能的选择是你没有向Hibernate表明什么是结果实体。为此,请将createQuery("from User")替换为createQuery("from UserBean", UserBean.class)

答案 1 :(得分:0)

@Table(name = ...)区分(结果SQL)表名,兄弟!

@Entity(name = ...)区分&#34;实体名称&#34;特别是在jpql / hql / criteria查询中 - 默认为类名。 - @ xerx593